ClickHouse/src/Processors/Formats/Impl/ConstantExpressionTemplate.h

94 lines
4.0 KiB
C++
Raw Normal View History

#pragma once
#include <Core/Block.h>
#include <Formats/FormatSettings.h>
#include <Parsers/TokenIterator.h>
namespace DB
{
2019-09-04 16:54:20 +00:00
struct LiteralInfo;
2019-09-11 19:55:28 +00:00
using LiteralsInfo = std::vector<LiteralInfo>;
struct SpecialParserType;
2019-09-04 16:54:20 +00:00
class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
2019-09-04 16:54:20 +00:00
/// Deduces template of an expression by replacing literals with dummy columns.
/// It allows to parse and evaluate similar expressions without using heavy IParsers and ExpressionAnalyzer.
/// Using ConstantExpressionTemplate for one expression is slower then evaluateConstantExpression(...),
/// but it's significantly faster for batch of expressions
2019-09-13 17:40:48 +00:00
class ConstantExpressionTemplate : boost::noncopyable
{
2019-09-13 17:40:48 +00:00
struct TemplateStructure : boost::noncopyable
2019-09-11 19:55:28 +00:00
{
TemplateStructure(LiteralsInfo & replaced_literals, TokenIterator expression_begin, TokenIterator expression_end,
ASTPtr & expr, const IDataType & result_type, bool null_as_default_, ContextPtr context);
2019-09-11 19:55:28 +00:00
2019-10-16 19:52:00 +00:00
static void addNodesToCastResult(const IDataType & result_column_type, ASTPtr & expr, bool null_as_default);
static size_t getTemplateHash(const ASTPtr & expression, const LiteralsInfo & replaced_literals,
const DataTypePtr & result_column_type, bool null_as_default, const String & salt);
2019-09-11 19:55:28 +00:00
String result_column_name;
std::vector<String> tokens;
std::vector<size_t> token_after_literal_idx;
Block literals;
ExpressionActionsPtr actions_on_literals;
2021-03-09 14:46:52 +00:00
Serializations serializations;
2019-09-11 19:55:28 +00:00
std::vector<SpecialParserType> special_parser;
2019-10-16 19:52:00 +00:00
bool null_as_default;
2019-09-11 19:55:28 +00:00
};
public:
2019-09-13 17:40:48 +00:00
using TemplateStructurePtr = std::shared_ptr<const TemplateStructure>;
2019-09-11 19:55:28 +00:00
2019-09-13 17:40:48 +00:00
class Cache : boost::noncopyable
{
std::unordered_map<size_t, TemplateStructurePtr> cache;
const size_t max_size;
public:
explicit Cache(size_t max_size_ = 4096) : max_size(max_size_) {}
2019-10-02 17:51:00 +00:00
/// Deduce template of expression of type result_column_type and add it to cache (or use template from cache)
2019-09-13 17:40:48 +00:00
TemplateStructurePtr getFromCacheOrConstruct(const DataTypePtr & result_column_type,
2019-10-16 19:52:00 +00:00
bool null_as_default,
2019-09-13 17:40:48 +00:00
TokenIterator expression_begin,
TokenIterator expression_end,
const ASTPtr & expression_,
ContextPtr context,
2019-09-13 17:40:48 +00:00
bool * found_in_cache = nullptr,
const String & salt = {});
};
explicit ConstantExpressionTemplate(const TemplateStructurePtr & structure_)
: structure(structure_), columns(structure->literals.cloneEmptyColumns()) {}
2019-09-11 19:55:28 +00:00
2019-09-04 16:54:20 +00:00
/// Read expression from istr, assert it has the same structure and the same types of literals (template matches)
/// and parse literals into temporary columns
bool parseExpression(ReadBuffer & istr, const FormatSettings & format_settings, const Settings & settings);
2019-10-16 19:52:00 +00:00
/// Evaluate batch of expressions were parsed using template.
/// If template was deduced with null_as_default == true, set bits in nulls for NULL values in column_idx, starting from offset.
ColumnPtr evaluateAll(BlockMissingValues & nulls, size_t column_idx, const DataTypePtr & expected_type, size_t offset = 0);
size_t rowsCount() const { return rows_count; }
private:
bool tryParseExpression(ReadBuffer & istr, const FormatSettings & format_settings, size_t & cur_column, const Settings & settings);
bool parseLiteralAndAssertType(ReadBuffer & istr, const IDataType * type, size_t column_idx, const Settings & settings);
private:
2019-09-11 19:55:28 +00:00
TemplateStructurePtr structure;
MutableColumns columns;
2019-05-18 03:21:39 +00:00
/// For expressions without literals (e.g. "now()")
size_t rows_count = 0;
};
}