2018-12-11 17:43:12 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2023-08-03 19:18:59 +00:00
|
|
|
#include <Columns/ColumnSet.h>
|
|
|
|
#include <Columns/ColumnSet.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Core/Block.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2022-03-16 11:21:18 +00:00
|
|
|
#include <DataTypes/FieldToDataType.h>
|
2023-08-03 19:18:59 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2023-08-03 19:18:59 +00:00
|
|
|
#include <Interpreters/castColumn.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Interpreters/convertFieldToType.h>
|
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
2020-07-22 17:13:05 +00:00
|
|
|
#include <Interpreters/TreeRewriter.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2022-04-28 20:09:29 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
2018-07-24 14:05:37 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2018-12-11 17:43:12 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2021-02-17 15:36:37 +00:00
|
|
|
#include <Interpreters/FunctionNameNormalizer.h>
|
2019-10-07 10:24:35 +00:00
|
|
|
#include <Interpreters/ReplaceQueryParameterVisitor.h>
|
2023-08-03 19:18:59 +00:00
|
|
|
#include <Processors/QueryPlan/Optimizations/actionsDAGUtils.h>
|
|
|
|
#include <Functions/IFunction.h>
|
2021-08-09 21:48:44 +00:00
|
|
|
#include <unordered_map>
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-07-16 21:26:21 +00:00
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:32:33 +00:00
|
|
|
static std::pair<Field, std::shared_ptr<const IDataType>> getFieldAndDataTypeFromLiteral(ASTLiteral * literal)
|
|
|
|
{
|
|
|
|
auto type = applyVisitor(FieldToDataType(), literal->value);
|
|
|
|
/// In case of Array field nested fields can have different types.
|
|
|
|
/// Example: Array [1, 2.3] will have 2 fields with types UInt64 and Float64
|
|
|
|
/// when result type is Array(Float64).
|
|
|
|
/// So, we need to convert this field to the result type.
|
|
|
|
Field res = convertFieldToType(literal->value, *type);
|
|
|
|
return {res, type};
|
|
|
|
}
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2022-07-06 12:59:25 +00:00
|
|
|
std::pair<Field, std::shared_ptr<const IDataType>> evaluateConstantExpression(const ASTPtr & node, const ContextPtr & context)
|
2016-02-13 06:37:19 +00:00
|
|
|
{
|
2022-03-16 16:16:08 +00:00
|
|
|
if (ASTLiteral * literal = node->as<ASTLiteral>())
|
2022-06-01 15:32:33 +00:00
|
|
|
return getFieldAndDataTypeFromLiteral(literal);
|
2022-03-16 11:21:18 +00:00
|
|
|
|
2018-11-08 15:43:14 +00:00
|
|
|
NamesAndTypesList source_columns = {{ "_dummy", std::make_shared<DataTypeUInt8>() }};
|
2022-04-28 20:09:29 +00:00
|
|
|
|
2018-11-08 17:28:52 +00:00
|
|
|
auto ast = node->clone();
|
2022-04-28 20:09:29 +00:00
|
|
|
|
2022-05-02 12:27:00 +00:00
|
|
|
if (ast->as<ASTSubquery>() != nullptr)
|
2022-04-28 20:09:29 +00:00
|
|
|
{
|
|
|
|
/** For subqueries getColumnName if there are no alias will return __subquery_ + 'hash'.
|
|
|
|
* If there is alias getColumnName for subquery will return alias.
|
|
|
|
* In result block name of subquery after QueryAliasesVisitor pass will be _subquery1.
|
|
|
|
* We specify alias for subquery, because we need to get column from result block.
|
|
|
|
*/
|
|
|
|
ast->setAlias("constant_expression");
|
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ReplaceQueryParameterVisitor param_visitor(context->getQueryParameters());
|
2019-10-07 10:24:35 +00:00
|
|
|
param_visitor.visit(ast);
|
2021-02-18 03:27:24 +00:00
|
|
|
|
2022-05-17 08:22:31 +00:00
|
|
|
/// Notice: function name normalization is disabled when it's a secondary query, because queries are either
|
|
|
|
/// already normalized on initiator node, or not normalized and should remain unnormalized for
|
|
|
|
/// compatibility.
|
|
|
|
if (context->getClientInfo().query_kind != ClientInfo::QueryKind::SECONDARY_QUERY && context->getSettingsRef().normalize_function_names)
|
2021-02-18 03:27:24 +00:00
|
|
|
FunctionNameNormalizer().visit(ast.get());
|
|
|
|
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(ast, source_columns);
|
2022-04-28 20:09:29 +00:00
|
|
|
|
|
|
|
/// AST potentially could be transformed to literal during TreeRewriter analyze.
|
|
|
|
/// For example if we have SQL user defined function that return literal AS subquery.
|
|
|
|
if (ASTLiteral * literal = ast->as<ASTLiteral>())
|
2022-06-01 15:32:33 +00:00
|
|
|
return getFieldAndDataTypeFromLiteral(literal);
|
2022-04-28 20:09:29 +00:00
|
|
|
|
2023-02-03 00:41:20 +00:00
|
|
|
auto actions = ExpressionAnalyzer(ast, syntax_result, context).getConstActionsDAG();
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-02-03 00:41:20 +00:00
|
|
|
ColumnPtr result_column;
|
|
|
|
DataTypePtr result_type;
|
2023-03-20 12:42:30 +00:00
|
|
|
String result_name = ast->getColumnName();
|
2023-02-03 00:41:20 +00:00
|
|
|
for (const auto & action_node : actions->getOutputs())
|
|
|
|
{
|
|
|
|
if ((action_node->result_name == result_name) && action_node->column)
|
|
|
|
{
|
|
|
|
result_column = action_node->column;
|
|
|
|
result_type = action_node->result_type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-02-03 00:41:20 +00:00
|
|
|
if (!result_column)
|
2021-07-02 00:53:08 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
2023-07-16 21:21:30 +00:00
|
|
|
"Element of set in IN, VALUES, or LIMIT, or aggregate function parameter, or a table function argument "
|
2023-02-03 00:41:20 +00:00
|
|
|
"is not a constant expression (result column not found): {}", result_name);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-02-03 00:41:20 +00:00
|
|
|
if (result_column->empty())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"Logical error: empty result column after evaluation "
|
2023-07-16 21:21:30 +00:00
|
|
|
"of constant expression for IN, VALUES, or LIMIT, or aggregate function parameter, or a table function argument");
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2019-09-28 22:12:29 +00:00
|
|
|
/// Expressions like rand() or now() are not constant
|
2023-02-03 00:41:20 +00:00
|
|
|
if (!isColumnConst(*result_column))
|
2021-07-02 00:53:08 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
2023-07-16 21:21:30 +00:00
|
|
|
"Element of set in IN, VALUES, or LIMIT, or aggregate function parameter, or a table function argument "
|
2023-02-03 00:41:20 +00:00
|
|
|
"is not a constant expression (result column is not const): {}", result_name);
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2023-02-03 00:41:20 +00:00
|
|
|
return std::make_pair((*result_column)[0], result_type);
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionAsLiteral(const ASTPtr & node, const ContextPtr & context)
|
2018-07-25 12:31:47 +00:00
|
|
|
{
|
2019-10-09 19:58:50 +00:00
|
|
|
/// If it's already a literal.
|
2019-03-11 13:22:51 +00:00
|
|
|
if (node->as<ASTLiteral>())
|
2017-04-01 07:20:54 +00:00
|
|
|
return node;
|
2018-02-26 03:37:08 +00:00
|
|
|
return std::make_shared<ASTLiteral>(evaluateConstantExpression(node, context).first);
|
2016-08-25 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionOrIdentifierAsLiteral(const ASTPtr & node, const ContextPtr & context)
|
2016-08-25 17:23:29 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * id = node->as<ASTIdentifier>())
|
2020-10-24 18:46:10 +00:00
|
|
|
return std::make_shared<ASTLiteral>(id->name());
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return evaluateConstantExpressionAsLiteral(node, context);
|
2016-08-25 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:59:25 +00:00
|
|
|
ASTPtr evaluateConstantExpressionForDatabaseName(const ASTPtr & node, const ContextPtr & context)
|
2020-02-19 18:58:29 +00:00
|
|
|
{
|
|
|
|
ASTPtr res = evaluateConstantExpressionOrIdentifierAsLiteral(node, context);
|
|
|
|
auto & literal = res->as<ASTLiteral &>();
|
|
|
|
if (literal.value.safeGet<String>().empty())
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
String current_database = context->getCurrentDatabase();
|
2020-02-19 18:58:29 +00:00
|
|
|
if (current_database.empty())
|
|
|
|
{
|
|
|
|
/// Table was created on older version of ClickHouse and CREATE contains not folded expression.
|
|
|
|
/// Current database is not set yet during server startup, so we cannot evaluate it correctly.
|
2021-04-10 23:33:54 +00:00
|
|
|
literal.value = context->getConfigRef().getString("default_database", "default");
|
2020-02-19 18:58:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
literal.value = current_database;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-07-24 01:52:18 +00:00
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using Conjunction = ColumnsWithTypeAndName;
|
|
|
|
using Disjunction = std::vector<Conjunction>;
|
|
|
|
|
2020-03-24 12:55:35 +00:00
|
|
|
Disjunction analyzeEquals(const ASTIdentifier * identifier, const Field & value, const ExpressionActionsPtr & expr)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2020-03-24 12:55:35 +00:00
|
|
|
if (!identifier || value.isNull())
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & name_and_type : expr->getRequiredColumnsWithTypes())
|
|
|
|
{
|
|
|
|
const auto & name = name_and_type.name;
|
|
|
|
const auto & type = name_and_type.type;
|
|
|
|
|
2020-10-24 18:46:10 +00:00
|
|
|
if (name == identifier->name())
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
2020-03-24 15:56:46 +00:00
|
|
|
Field converted = convertFieldToType(value, *type);
|
|
|
|
if (converted.isNull())
|
2020-03-22 11:11:57 +00:00
|
|
|
return {};
|
2020-03-24 15:56:46 +00:00
|
|
|
column.column = type->createColumnConst(1, converted);
|
2018-12-11 17:43:12 +00:00
|
|
|
column.name = name;
|
|
|
|
column.type = type;
|
|
|
|
return {{std::move(column)}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:55:35 +00:00
|
|
|
Disjunction analyzeEquals(const ASTIdentifier * identifier, const ASTLiteral * literal, const ExpressionActionsPtr & expr)
|
|
|
|
{
|
|
|
|
if (!identifier || !literal)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return analyzeEquals(identifier, literal->value, expr);
|
|
|
|
}
|
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
Disjunction andDNF(const Disjunction & left, const Disjunction & right)
|
|
|
|
{
|
|
|
|
if (left.empty())
|
|
|
|
{
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & conjunct1 : left)
|
|
|
|
{
|
|
|
|
for (const auto & conjunct2 : right)
|
|
|
|
{
|
|
|
|
Conjunction new_conjunct{conjunct1};
|
|
|
|
new_conjunct.insert(new_conjunct.end(), conjunct2.begin(), conjunct2.end());
|
|
|
|
result.emplace_back(new_conjunct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
Disjunction analyzeFunction(const ASTFunction * fn, const ExpressionActionsPtr & expr, size_t & limit)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2021-03-08 07:05:56 +00:00
|
|
|
if (!fn || !limit)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: enumerate all possible function names!
|
|
|
|
|
|
|
|
if (fn->name == "equals")
|
|
|
|
{
|
|
|
|
const auto * left = fn->arguments->children.front().get();
|
|
|
|
const auto * right = fn->arguments->children.back().get();
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * identifier = left->as<ASTIdentifier>() ? left->as<ASTIdentifier>() : right->as<ASTIdentifier>();
|
|
|
|
const auto * literal = left->as<ASTLiteral>() ? left->as<ASTLiteral>() : right->as<ASTLiteral>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
--limit;
|
2018-12-11 17:43:12 +00:00
|
|
|
return analyzeEquals(identifier, literal, expr);
|
|
|
|
}
|
|
|
|
else if (fn->name == "in")
|
|
|
|
{
|
|
|
|
const auto * left = fn->arguments->children.front().get();
|
|
|
|
const auto * right = fn->arguments->children.back().get();
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * identifier = left->as<ASTIdentifier>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2020-03-24 12:55:35 +00:00
|
|
|
Disjunction result;
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2021-07-23 00:34:49 +00:00
|
|
|
auto add_dnf = [&](const auto & dnf)
|
2021-03-08 07:05:56 +00:00
|
|
|
{
|
|
|
|
if (dnf.size() > limit)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.insert(result.end(), dnf.begin(), dnf.end());
|
|
|
|
limit -= dnf.size();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-03-24 12:55:35 +00:00
|
|
|
if (const auto * tuple_func = right->as<ASTFunction>(); tuple_func && tuple_func->name == "tuple")
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2020-03-24 12:55:35 +00:00
|
|
|
const auto * tuple_elements = tuple_func->children.front()->as<ASTExpressionList>();
|
|
|
|
for (const auto & child : tuple_elements->children)
|
|
|
|
{
|
|
|
|
const auto * literal = child->as<ASTLiteral>();
|
|
|
|
const auto dnf = analyzeEquals(identifier, literal, expr);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2020-03-24 12:55:35 +00:00
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
if (!add_dnf(dnf))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-24 12:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 13:16:15 +00:00
|
|
|
else if (const auto * tuple_literal = right->as<ASTLiteral>(); tuple_literal)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2021-06-04 13:16:15 +00:00
|
|
|
if (tuple_literal->value.getType() == Field::Types::Tuple)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2021-06-04 13:16:15 +00:00
|
|
|
const auto & tuple = tuple_literal->value.get<const Tuple &>();
|
|
|
|
for (const auto & child : tuple)
|
2020-03-24 12:55:35 +00:00
|
|
|
{
|
2021-06-04 13:16:15 +00:00
|
|
|
const auto dnf = analyzeEquals(identifier, child, expr);
|
2020-03-24 12:55:35 +00:00
|
|
|
|
2021-06-04 13:16:15 +00:00
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!add_dnf(dnf))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-03-08 07:05:56 +00:00
|
|
|
}
|
2020-03-24 12:55:35 +00:00
|
|
|
}
|
2021-06-04 13:16:15 +00:00
|
|
|
else
|
|
|
|
return analyzeEquals(identifier, tuple_literal, expr);
|
2020-03-24 12:55:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (fn->name == "or")
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * args = fn->children.front()->as<ASTExpressionList>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & arg : args->children)
|
|
|
|
{
|
2021-03-08 07:05:56 +00:00
|
|
|
const auto dnf = analyzeFunction(arg->as<ASTFunction>(), expr, limit);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
/// limit accounted in analyzeFunction()
|
2018-12-11 17:43:12 +00:00
|
|
|
result.insert(result.end(), dnf.begin(), dnf.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (fn->name == "and")
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * args = fn->children.front()->as<ASTExpressionList>();
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Disjunction result;
|
|
|
|
|
|
|
|
for (const auto & arg : args->children)
|
|
|
|
{
|
2021-03-08 07:05:56 +00:00
|
|
|
const auto dnf = analyzeFunction(arg->as<ASTFunction>(), expr, limit);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (dnf.empty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
/// limit accounted in analyzeFunction()
|
2018-12-11 17:43:12 +00:00
|
|
|
result = andDNF(result, dnf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-03 19:18:59 +00:00
|
|
|
|
|
|
|
/// This is a map which stores constants for a single conjunction.
|
|
|
|
/// It can contain execution results from different stanges.
|
|
|
|
/// Example: for expression `(a + b) * c` and predicate `a = 1 and b = 2 and a + b = 3` the map will be
|
|
|
|
/// a -> 1, b -> 2, a + b -> 3
|
|
|
|
/// It is allowed to have a map with contradictive conditions, like for `a = 1 and b = 2 and a + b = 5`,
|
|
|
|
/// but a map for predicate like `a = 1 and a = 2` cannot be built.
|
|
|
|
using ConjunctionMap = std::unordered_map<const ActionsDAG::Node *, ColumnConst::Ptr>;
|
|
|
|
using DisjunctionList = std::list<ConjunctionMap>;
|
|
|
|
|
|
|
|
std::optional<ConjunctionMap> andConjunctions(const ConjunctionMap & lhs, const ConjunctionMap & rhs)
|
|
|
|
{
|
|
|
|
ConjunctionMap res;
|
|
|
|
for (const auto & [node, column] : rhs)
|
|
|
|
{
|
|
|
|
auto it = lhs.find(node);
|
|
|
|
/// If constants are different, the conjunction is invalid.
|
|
|
|
if (it != lhs.end() && column->compareAt(0, 0, *it->second, 1))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (it == lhs.end())
|
|
|
|
res.emplace(node, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.insert(lhs.begin(), lhs.end());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
DisjunctionList andDisjunctions(const DisjunctionList & lhs, const DisjunctionList & rhs)
|
|
|
|
{
|
|
|
|
DisjunctionList res;
|
|
|
|
for (const auto & lhs_map : lhs)
|
|
|
|
for(const auto & rhs_map : rhs)
|
|
|
|
if (auto conj = andConjunctions(lhs_map, rhs_map))
|
|
|
|
res.emplace_back(std::move(*conj));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
DisjunctionList orDisjunctions(DisjunctionList && lhs, DisjunctionList && rhs)
|
|
|
|
{
|
|
|
|
lhs.splice(lhs.end(), std::move(rhs));
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ActionsDAG::Node * findMatch(const ActionsDAG::Node * key, const MatchedTrees::Matches & matches)
|
|
|
|
{
|
|
|
|
auto it = matches.find(key);
|
|
|
|
if (it == matches.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto & match = it->second;
|
|
|
|
if (!match.node || match.monotonicity)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return match.node;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnNullable::Ptr tryCastColumn(ColumnPtr col, DataTypePtr from_type, DataTypePtr to_type)
|
|
|
|
{
|
|
|
|
if (!to_type->canBeInsideNullable())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto res = castColumnAccurate({col, from_type, std::string()}, to_type);
|
|
|
|
if (res->onlyNull())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto col_nullable = typeid_cast<const ColumnNullable *>(col.get());
|
|
|
|
if (!col_nullable)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return col_nullable->getPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ConjunctionMap::value_type> analyzeConstant(
|
|
|
|
const ActionsDAG::Node * key,
|
|
|
|
const ActionsDAG::Node * value,
|
|
|
|
const MatchedTrees::Matches & matches)
|
|
|
|
{
|
|
|
|
if (value->type != ActionsDAG::ActionType::COLUMN)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (const auto * col = typeid_cast<const ColumnConst *>(value->column.get()))
|
|
|
|
{
|
|
|
|
if (const auto * node = findMatch(key, matches))
|
|
|
|
{
|
|
|
|
ColumnConst::Ptr column = col->getPtr();
|
|
|
|
if (!value->result_type->equals(*node->result_type))
|
|
|
|
{
|
|
|
|
auto inner = tryCastColumn(column->getDataColumnPtr(), value->result_type, node->result_type);
|
|
|
|
if (!inner || inner->isNullAt(0))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto innder_column = node->result_type->createColumn();
|
|
|
|
innder_column->insert((*inner)[0]);
|
|
|
|
column = ColumnConst::create(std::move(innder_column), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConjunctionMap::value_type{node, column};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<DisjunctionList> analyzeSet(
|
|
|
|
const ActionsDAG::Node * key,
|
|
|
|
const ActionsDAG::Node * value,
|
|
|
|
const MatchedTrees::Matches & matches,
|
|
|
|
const ContextPtr & context)
|
|
|
|
{
|
|
|
|
if (value->type != ActionsDAG::ActionType::COLUMN)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto col = value->column;
|
|
|
|
if (const auto * col_const = typeid_cast<const ColumnConst *>(col.get()))
|
|
|
|
col = col_const->getDataColumnPtr();
|
|
|
|
|
|
|
|
const auto * col_set = typeid_cast<const ColumnSet *>(col.get());
|
|
|
|
if (!col_set || !col_set->getData())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto * set_from_tuple = typeid_cast<FutureSetFromTuple *>(col_set->getData().get());
|
|
|
|
if (!set_from_tuple)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
SetPtr set = set_from_tuple->buildOrderedSetInplace(context);
|
|
|
|
if (!set || !set->hasExplicitSetElements())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto * node = findMatch(key, matches);
|
|
|
|
if (!node)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto elements = set->getSetElements();
|
|
|
|
auto types = set->getElementsTypes();
|
|
|
|
|
|
|
|
ColumnPtr column;
|
|
|
|
DataTypePtr type;
|
|
|
|
if (elements.empty())
|
|
|
|
return {};
|
|
|
|
if (elements.size() == 1)
|
|
|
|
{
|
|
|
|
column = elements[0];
|
|
|
|
type = types[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
column = ColumnTuple::create(std::move(elements));
|
|
|
|
type = std::make_shared<DataTypeTuple>(std::move(types));
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnNullable::Ptr casted_col;
|
|
|
|
const NullMap * null_map = nullptr;
|
|
|
|
|
|
|
|
if (!type->equals(*node->result_type))
|
|
|
|
{
|
|
|
|
casted_col = tryCastColumn(column, value->result_type, node->result_type);
|
|
|
|
if (!casted_col)
|
|
|
|
return {};
|
|
|
|
null_map = &casted_col->getNullMapData();
|
|
|
|
column = casted_col->getNestedColumnPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
DisjunctionList res;
|
|
|
|
if (node->result_type->isNullable() && set->hasNull())
|
|
|
|
res.push_back({ConjunctionMap{{node, node->result_type->createColumnConst(1, Field())}}});
|
|
|
|
|
|
|
|
size_t num_rows = column->size();
|
|
|
|
for (size_t row = 0; row < num_rows; ++row)
|
|
|
|
{
|
|
|
|
if (null_map && (*null_map)[row])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto innder_column = node->result_type->createColumn();
|
|
|
|
innder_column->insert((*column)[row]);
|
|
|
|
auto column_const = ColumnConst::create(std::move(innder_column), 1);
|
|
|
|
|
|
|
|
res.push_back({ConjunctionMap::value_type{node, column}});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool isIndependentSubtree(const ActionsDAG::Node * node, const MatchedTrees::Matches & matches)
|
|
|
|
// {
|
|
|
|
// std::stack<const ActionsDAG::Node *> stack;
|
|
|
|
// stack.push(node);
|
|
|
|
// while (!stack.empty())
|
|
|
|
// {
|
|
|
|
// const auto * cur = stack.top();
|
|
|
|
// stack.pop();
|
|
|
|
// if (findMatch(cur, matches))
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// for (const auto * child : node->children)
|
|
|
|
// stack.push(child);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
std::optional<DisjunctionList> analyze(const ActionsDAG::Node * node, const MatchedTrees::Matches & matches, const ContextPtr & context)
|
|
|
|
{
|
|
|
|
if (node->type == ActionsDAG::ActionType::FUNCTION)
|
|
|
|
{
|
|
|
|
if (node->function_base->getName() == "equals")
|
|
|
|
{
|
|
|
|
const auto * lhs_node = node->children.at(0);
|
|
|
|
const auto * rhs_node = node->children.at(1);
|
|
|
|
if (auto val = analyzeConstant(lhs_node, rhs_node, matches))
|
|
|
|
return DisjunctionList{ConjunctionMap{std::move(*val)}};
|
|
|
|
|
|
|
|
if (auto val = analyzeConstant(rhs_node, lhs_node, matches))
|
|
|
|
return DisjunctionList{ConjunctionMap{std::move(*val)}};
|
|
|
|
}
|
|
|
|
else if (node->function_base->getName() == "in")
|
|
|
|
{
|
|
|
|
const auto * lhs_node = node->children.at(0);
|
|
|
|
const auto * rhs_node = node->children.at(1);
|
|
|
|
|
|
|
|
return analyzeSet(lhs_node, rhs_node, matches, context);
|
|
|
|
}
|
|
|
|
else if (node->function_base->getName() == "or")
|
|
|
|
{
|
|
|
|
DisjunctionList res;
|
|
|
|
for (const auto * child : node->children)
|
|
|
|
{
|
|
|
|
if (auto val = analyze(child, matches, context))
|
|
|
|
res = orDisjunctions(std::move(res), std::move(*val));
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if (node->function_base->getName() == "and")
|
|
|
|
{
|
|
|
|
std::optional<DisjunctionList> res;
|
|
|
|
for (const auto * child : node->children)
|
|
|
|
{
|
|
|
|
auto val = analyze(child, matches, context);
|
|
|
|
if (!val)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
res = std::move(val);
|
|
|
|
else
|
|
|
|
res = andDisjunctions(std::move(*res), std::move(*val));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Blocks result;
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * fn = node->as<ASTFunction>())
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2021-03-08 07:05:56 +00:00
|
|
|
const auto dnf = analyzeFunction(fn, target_expr, limit);
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2021-03-08 07:05:56 +00:00
|
|
|
if (dnf.empty() || !limit)
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
2018-12-19 12:38:13 +00:00
|
|
|
return {};
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
auto has_required_columns = [&target_expr](const Block & block) -> bool
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
for (const auto & name : target_expr->getRequiredColumns())
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
bool has_column = false;
|
2018-12-11 17:43:12 +00:00
|
|
|
for (const auto & column_name : block.getNames())
|
|
|
|
{
|
|
|
|
if (column_name == name)
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
has_column = true;
|
2018-12-11 17:43:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
if (!has_column)
|
2018-12-11 17:43:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & conjunct : dnf)
|
|
|
|
{
|
2021-07-24 01:52:18 +00:00
|
|
|
Block block;
|
2023-08-02 20:46:12 +00:00
|
|
|
bool always_false = false;
|
2021-07-24 01:52:18 +00:00
|
|
|
|
|
|
|
for (const auto & elem : conjunct)
|
|
|
|
{
|
|
|
|
if (!block.has(elem.name))
|
|
|
|
{
|
|
|
|
block.insert(elem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Conjunction of condition on column equality to distinct values can never be satisfied.
|
|
|
|
|
|
|
|
const ColumnWithTypeAndName & prev = block.getByName(elem.name);
|
|
|
|
|
|
|
|
if (isColumnConst(*prev.column) && isColumnConst(*elem.column))
|
|
|
|
{
|
|
|
|
Field prev_value = assert_cast<const ColumnConst &>(*prev.column).getField();
|
|
|
|
Field curr_value = assert_cast<const ColumnConst &>(*elem.column).getField();
|
|
|
|
|
2023-08-02 20:46:12 +00:00
|
|
|
always_false = prev_value != curr_value;
|
|
|
|
if (always_false)
|
|
|
|
break;
|
2021-07-24 01:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2023-08-02 20:46:12 +00:00
|
|
|
if (always_false)
|
|
|
|
continue;
|
|
|
|
|
2018-12-11 17:43:12 +00:00
|
|
|
// Block should contain all required columns from `target_expr`
|
2020-03-23 02:12:31 +00:00
|
|
|
if (!has_required_columns(block))
|
2018-12-11 17:43:12 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
target_expr->execute(block);
|
|
|
|
|
|
|
|
if (block.rows() == 1)
|
|
|
|
{
|
|
|
|
result.push_back(block);
|
|
|
|
}
|
|
|
|
else if (block.rows() == 0)
|
|
|
|
{
|
|
|
|
// filter out cases like "WHERE a = 1 AND a = 2"
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// FIXME: shouldn't happen
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 09:45:47 +00:00
|
|
|
else if (const auto * literal = node->as<ASTLiteral>())
|
|
|
|
{
|
|
|
|
// Check if it's always true or false.
|
|
|
|
if (literal->value.getType() == Field::Types::UInt64 && literal->value.get<UInt64>() == 0)
|
|
|
|
return {result};
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-11 17:43:12 +00:00
|
|
|
|
2018-12-19 12:38:13 +00:00
|
|
|
return {result};
|
2018-12-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|