| 1 | #pragma once |
| 2 | |
| 3 | #include <Core/Block.h> |
| 4 | #include <Interpreters/ExpressionActions.h> |
| 5 | #include <Formats/FormatSettings.h> |
| 6 | #include <Parsers/TokenIterator.h> |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | struct LiteralInfo; |
| 12 | using LiteralsInfo = std::vector<LiteralInfo>; |
| 13 | struct SpecialParserType; |
| 14 | |
| 15 | /// Deduces template of an expression by replacing literals with dummy columns. |
| 16 | /// It allows to parse and evaluate similar expressions without using heavy IParsers and ExpressionAnalyzer. |
| 17 | /// Using ConstantExpressionTemplate for one expression is slower then evaluateConstantExpression(...), |
| 18 | /// but it's significantly faster for batch of expressions |
| 19 | class ConstantExpressionTemplate : boost::noncopyable |
| 20 | { |
| 21 | struct TemplateStructure : boost::noncopyable |
| 22 | { |
| 23 | TemplateStructure(LiteralsInfo & replaced_literals, TokenIterator expression_begin, TokenIterator expression_end, |
| 24 | ASTPtr & expr, const IDataType & result_type, bool null_as_default_, const Context & context); |
| 25 | |
| 26 | static void addNodesToCastResult(const IDataType & result_column_type, ASTPtr & expr, bool null_as_default); |
| 27 | static size_t getTemplateHash(const ASTPtr & expression, const LiteralsInfo & replaced_literals, |
| 28 | const DataTypePtr & result_column_type, bool null_as_default, const String & salt); |
| 29 | |
| 30 | String result_column_name; |
| 31 | |
| 32 | std::vector<String> tokens; |
| 33 | std::vector<size_t> token_after_literal_idx; |
| 34 | |
| 35 | Block literals; |
| 36 | ExpressionActionsPtr actions_on_literals; |
| 37 | |
| 38 | std::vector<SpecialParserType> special_parser; |
| 39 | bool null_as_default; |
| 40 | }; |
| 41 | |
| 42 | public: |
| 43 | using TemplateStructurePtr = std::shared_ptr<const TemplateStructure>; |
| 44 | |
| 45 | class Cache : boost::noncopyable |
| 46 | { |
| 47 | std::unordered_map<size_t, TemplateStructurePtr> cache; |
| 48 | const size_t max_size; |
| 49 | |
| 50 | public: |
| 51 | explicit Cache(size_t max_size_ = 4096) : max_size(max_size_) {} |
| 52 | |
| 53 | /// Deduce template of expression of type result_column_type and add it to cache (or use template from cache) |
| 54 | TemplateStructurePtr getFromCacheOrConstruct(const DataTypePtr & result_column_type, |
| 55 | bool null_as_default, |
| 56 | TokenIterator expression_begin, |
| 57 | TokenIterator expression_end, |
| 58 | const ASTPtr & expression_, |
| 59 | const Context & context, |
| 60 | bool * found_in_cache = nullptr, |
| 61 | const String & salt = {}); |
| 62 | }; |
| 63 | |
| 64 | explicit ConstantExpressionTemplate(const TemplateStructurePtr & structure_) |
| 65 | : structure(structure_), columns(structure->literals.cloneEmptyColumns()) {} |
| 66 | |
| 67 | /// Read expression from istr, assert it has the same structure and the same types of literals (template matches) |
| 68 | /// and parse literals into temporary columns |
| 69 | bool parseExpression(ReadBuffer & istr, const FormatSettings & settings); |
| 70 | |
| 71 | /// Evaluate batch of expressions were parsed using template. |
| 72 | /// If template was deduced with null_as_default == true, set bits in nulls for NULL values in column_idx, starting from offset. |
| 73 | ColumnPtr evaluateAll(BlockMissingValues & nulls, size_t column_idx, size_t offset = 0); |
| 74 | |
| 75 | size_t rowsCount() const { return rows_count; } |
| 76 | |
| 77 | private: |
| 78 | bool tryParseExpression(ReadBuffer & istr, const FormatSettings & settings, size_t & cur_column); |
| 79 | bool parseLiteralAndAssertType(ReadBuffer & istr, const IDataType * type, size_t column_idx); |
| 80 | |
| 81 | private: |
| 82 | TemplateStructurePtr structure; |
| 83 | MutableColumns columns; |
| 84 | |
| 85 | /// For expressions without literals (e.g. "now()") |
| 86 | size_t rows_count = 0; |
| 87 | |
| 88 | }; |
| 89 | |
| 90 | } |
| 91 | |