| 1 | #pragma once |
| 2 | |
| 3 | #include <Core/Block.h> |
| 4 | #include <Core/Field.h> |
| 5 | #include <Parsers/IAST.h> |
| 6 | #include <Parsers/IParser.h> |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <optional> |
| 10 | |
| 11 | |
| 12 | namespace DB |
| 13 | { |
| 14 | |
| 15 | class Context; |
| 16 | class ExpressionActions; |
| 17 | class IDataType; |
| 18 | |
| 19 | using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>; |
| 20 | |
| 21 | /** Evaluate constant expression and its type. |
| 22 | * Used in rare cases - for elements of set for IN, for data to INSERT. |
| 23 | * Throws exception if it's not a constant expression. |
| 24 | * Quite suboptimal. |
| 25 | */ |
| 26 | std::pair<Field, std::shared_ptr<const IDataType>> evaluateConstantExpression(const ASTPtr & node, const Context & context); |
| 27 | |
| 28 | |
| 29 | /** Evaluate constant expression and returns ASTLiteral with its value. |
| 30 | */ |
| 31 | ASTPtr evaluateConstantExpressionAsLiteral(const ASTPtr & node, const Context & context); |
| 32 | |
| 33 | |
| 34 | /** Evaluate constant expression and returns ASTLiteral with its value. |
| 35 | * Also, if AST is identifier, then return string literal with its name. |
| 36 | * Useful in places where some name may be specified as identifier, or as result of a constant expression. |
| 37 | */ |
| 38 | ASTPtr evaluateConstantExpressionOrIdentifierAsLiteral(const ASTPtr & node, const Context & context); |
| 39 | |
| 40 | /** Try to fold condition to countable set of constant values. |
| 41 | * @param condition a condition that we try to fold. |
| 42 | * @param target_expr expression evaluated over a set of constants. |
| 43 | * @return optional blocks each with a single row and a single column for target expression, |
| 44 | * or empty blocks if condition is always false, |
| 45 | * or nothing if condition can't be folded to a set of constants. |
| 46 | */ |
| 47 | std::optional<Blocks> evaluateExpressionOverConstantCondition(const ASTPtr & condition, const ExpressionActionsPtr & target_expr); |
| 48 | |
| 49 | } |
| 50 | |