2016-02-13 06:37:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Core/Block.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Field.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2023-08-10 18:41:57 +00:00
|
|
|
#include <Interpreters/ActionsDAG.h>
|
2017-09-17 18:49:43 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2018-12-19 13:57:55 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
class ExpressionActions;
|
2017-02-16 22:05:48 +00:00
|
|
|
class IDataType;
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
|
2017-08-04 15:54:00 +00:00
|
|
|
|
2023-09-05 11:59:11 +00:00
|
|
|
using EvaluateConstantExpressionResult = std::pair<Field, std::shared_ptr<const IDataType>>;
|
|
|
|
|
2017-02-17 20:27:32 +00:00
|
|
|
/** Evaluate constant expression and its type.
|
2016-08-25 17:23:29 +00:00
|
|
|
* Used in rare cases - for elements of set for IN, for data to INSERT.
|
2019-10-09 19:58:50 +00:00
|
|
|
* Throws exception if it's not a constant expression.
|
2016-08-25 17:23:29 +00:00
|
|
|
* Quite suboptimal.
|
2016-02-13 06:37:19 +00:00
|
|
|
*/
|
2023-09-05 11:59:11 +00:00
|
|
|
EvaluateConstantExpressionResult evaluateConstantExpression(const ASTPtr & node, const ContextPtr & context);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-09-05 11:59:11 +00:00
|
|
|
std::optional<EvaluateConstantExpressionResult> tryEvaluateConstantExpression(const ASTPtr & node, const ContextPtr & context);
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
/** Evaluate constant expression and returns ASTLiteral with its value.
|
2016-08-25 17:23:29 +00:00
|
|
|
*/
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionAsLiteral(const ASTPtr & node, const ContextPtr & context);
|
2016-08-25 17:23:29 +00:00
|
|
|
|
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
/** Evaluate constant expression and returns ASTLiteral with its value.
|
2016-08-25 17:23:29 +00:00
|
|
|
* Also, if AST is identifier, then return string literal with its name.
|
|
|
|
* Useful in places where some name may be specified as identifier, or as result of a constant expression.
|
|
|
|
*/
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionOrIdentifierAsLiteral(const ASTPtr & node, const ContextPtr & context);
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2020-02-19 18:58:29 +00:00
|
|
|
/** The same as evaluateConstantExpressionOrIdentifierAsLiteral(...),
|
|
|
|
* but if result is an empty string, replace it with current database name
|
|
|
|
* or default database name.
|
|
|
|
*/
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionForDatabaseName(const ASTPtr & node, const ContextPtr & context);
|
2020-02-19 18:58:29 +00:00
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
/** Try to fold condition to countable set of constant values.
|
2020-03-21 19:49:26 +00:00
|
|
|
* @param node a condition that we try to fold.
|
2018-12-19 12:38:13 +00:00
|
|
|
* @param target_expr expression evaluated over a set of constants.
|
2021-03-08 07:05:56 +00:00
|
|
|
* @param limit limit for number of values
|
2018-12-19 12:38:13 +00:00
|
|
|
* @return optional blocks each with a single row and a single column for target expression,
|
|
|
|
* or empty blocks if condition is always false,
|
|
|
|
* or nothing if condition can't be folded to a set of constants.
|
|
|
|
*/
|
2021-03-08 07:05:56 +00:00
|
|
|
std::optional<Blocks> evaluateExpressionOverConstantCondition(const ASTPtr & node, const ExpressionActionsPtr & target_expr, size_t & limit);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2023-08-10 18:41:57 +00:00
|
|
|
using ConstantVariants = std::vector<ColumnsWithTypeAndName>;
|
|
|
|
|
|
|
|
/// max_elements is a hint
|
|
|
|
std::optional<ConstantVariants> evaluateExpressionOverConstantCondition(
|
|
|
|
const ActionsDAG::Node * predicate,
|
|
|
|
const ActionsDAG::NodeRawConstPtrs & expr,
|
|
|
|
const ContextPtr & context,
|
|
|
|
size_t max_elements);
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|