#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int LOGICAL_ERROR; extern const int BAD_ARGUMENTS; } std::pair> evaluateConstantExpression(const ASTPtr & node, const Context & context) { NamesAndTypesList source_columns = {{ "_dummy", std::make_shared() }}; auto query = node->clone(); auto syntax_result = SyntaxAnalyzer(context, {}).analyze(query, source_columns, {}); ExpressionActionsPtr expr_for_constant_folding = ExpressionAnalyzer(node, syntax_result, context, source_columns).getConstActions(); /// There must be at least one column in the block so that it knows the number of rows. Block block_with_constants{{ ColumnConst::create(ColumnUInt8::create(1, 0), 1), std::make_shared(), "_dummy" }}; expr_for_constant_folding->execute(block_with_constants); if (!block_with_constants || block_with_constants.rows() == 0) throw Exception("Logical error: empty block after evaluation of constant expression for IN or VALUES", ErrorCodes::LOGICAL_ERROR); String name = node->getColumnName(); if (!block_with_constants.has(name)) throw Exception("Element of set in IN or VALUES is not a constant expression: " + name, ErrorCodes::BAD_ARGUMENTS); const ColumnWithTypeAndName & result = block_with_constants.getByName(name); const IColumn & result_column = *result.column; if (!result_column.isColumnConst()) throw Exception("Element of set in IN or VALUES is not a constant expression: " + name, ErrorCodes::BAD_ARGUMENTS); return std::make_pair(result_column[0], result.type); } ASTPtr evaluateConstantExpressionAsLiteral(const ASTPtr & node, const Context & context) { /// Branch with string in qery. if (typeid_cast(node.get())) return node; /// Branch with TableFunction in query. if (auto table_func_ptr = typeid_cast(node.get())) if (TableFunctionFactory::instance().isTableFunctionName(table_func_ptr->name)) return node; return std::make_shared(evaluateConstantExpression(node, context).first); } ASTPtr evaluateConstantExpressionOrIdentifierAsLiteral(const ASTPtr & node, const Context & context) { if (auto id = typeid_cast(node.get())) return std::make_shared(id->name); return evaluateConstantExpressionAsLiteral(node, context); } }