2024-03-14 17:38:27 +00:00
|
|
|
#include <Interpreters/OptimizeShardingKeyRewriteInVisitor.h>
|
|
|
|
|
|
|
|
#include <Analyzer/ColumnNode.h>
|
|
|
|
#include <Analyzer/ConstantNode.h>
|
|
|
|
#include <Analyzer/FunctionNode.h>
|
|
|
|
#include <Analyzer/InDepthQueryTreeVisitor.h>
|
|
|
|
#include <Analyzer/Utils.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Interpreters/Context_fwd.h>
|
2021-03-07 15:51:01 +00:00
|
|
|
#include <Interpreters/convertFieldToType.h>
|
2024-03-14 17:38:27 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2021-03-07 15:51:01 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2024-03-14 17:38:27 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2021-03-07 15:51:01 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
Field executeFunctionOnField(
|
2021-06-28 18:55:31 +00:00
|
|
|
const Field & field,
|
|
|
|
const std::string & name,
|
2021-04-18 06:07:09 +00:00
|
|
|
const ExpressionActionsPtr & sharding_expr,
|
2021-06-28 18:55:31 +00:00
|
|
|
const DataTypePtr & type,
|
2021-03-07 15:51:01 +00:00
|
|
|
const std::string & sharding_key_column_name)
|
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.column = type->createColumnConst(1, field);
|
|
|
|
column.name = name;
|
|
|
|
column.type = type;
|
|
|
|
|
|
|
|
Block block{column};
|
|
|
|
size_t num_rows = 1;
|
2021-04-18 06:07:09 +00:00
|
|
|
sharding_expr->execute(block, num_rows);
|
2021-03-07 15:51:01 +00:00
|
|
|
|
|
|
|
ColumnWithTypeAndName & ret = block.getByName(sharding_key_column_name);
|
|
|
|
return (*ret.column)[0];
|
|
|
|
}
|
|
|
|
|
2024-03-28 13:38:28 +00:00
|
|
|
/// @param column_value - one of values from IN
|
2021-04-18 06:07:09 +00:00
|
|
|
/// @param sharding_column_name - name of that column
|
|
|
|
/// @return true if shard may contain such value (or it is unknown), otherwise false.
|
2021-03-07 15:51:01 +00:00
|
|
|
bool shardContains(
|
2024-03-28 13:38:28 +00:00
|
|
|
Field column_value,
|
2021-03-07 15:51:01 +00:00
|
|
|
const std::string & sharding_column_name,
|
2021-06-28 18:55:30 +00:00
|
|
|
const OptimizeShardingKeyRewriteInMatcher::Data & data)
|
2021-03-07 15:51:01 +00:00
|
|
|
{
|
2024-03-28 13:38:28 +00:00
|
|
|
/// Type of column in storage (used for implicit conversion from i.e. String to Int)
|
|
|
|
const DataTypePtr & column_type = data.sharding_key_expr->getSampleBlock().getByName(sharding_column_name).type;
|
2022-05-12 13:12:16 +00:00
|
|
|
/// Implicit conversion.
|
2024-03-28 13:38:28 +00:00
|
|
|
column_value = convertFieldToType(column_value, *column_type);
|
2021-06-28 21:02:06 +00:00
|
|
|
|
2021-03-07 15:51:01 +00:00
|
|
|
/// NULL is not allowed in sharding key,
|
|
|
|
/// so it should be safe to assume that shard cannot contain it.
|
2024-03-28 13:38:28 +00:00
|
|
|
if (column_value.isNull())
|
2021-03-07 15:51:01 +00:00
|
|
|
return false;
|
|
|
|
|
2021-06-28 18:55:31 +00:00
|
|
|
Field sharding_value = executeFunctionOnField(
|
2024-03-28 13:38:28 +00:00
|
|
|
column_value, sharding_column_name,
|
|
|
|
data.sharding_key_expr, column_type,
|
2021-06-28 18:55:31 +00:00
|
|
|
data.sharding_key_column_name);
|
2021-04-17 13:25:57 +00:00
|
|
|
/// The value from IN can be non-numeric,
|
|
|
|
/// but in this case it should be convertible to numeric type, let's try.
|
2022-03-08 20:28:24 +00:00
|
|
|
///
|
|
|
|
/// NOTE: that conversion should not be done for signed types,
|
|
|
|
/// since it uses accurate cast, that will return Null,
|
|
|
|
/// but we need static_cast<> (as createBlockSelector()).
|
|
|
|
if (!isInt64OrUInt64FieldType(sharding_value.getType()))
|
|
|
|
sharding_value = convertFieldToType(sharding_value, DataTypeUInt64());
|
2021-04-17 13:25:57 +00:00
|
|
|
/// In case of conversion is not possible (NULL), shard cannot contain the value anyway.
|
|
|
|
if (sharding_value.isNull())
|
|
|
|
return false;
|
|
|
|
|
2021-03-07 15:51:01 +00:00
|
|
|
UInt64 value = sharding_value.get<UInt64>();
|
2021-06-28 18:55:30 +00:00
|
|
|
const auto shard_num = data.slots[value % data.slots.size()] + 1;
|
|
|
|
return data.shard_info.shard_num == shard_num;
|
2021-03-07 15:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool OptimizeShardingKeyRewriteInMatcher::needChildVisit(ASTPtr & /*node*/, const ASTPtr & /*child*/)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptimizeShardingKeyRewriteInMatcher::visit(ASTPtr & node, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * function = node->as<ASTFunction>())
|
|
|
|
visit(*function, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptimizeShardingKeyRewriteInMatcher::visit(ASTFunction & function, Data & data)
|
|
|
|
{
|
|
|
|
if (function.name != "in")
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto * left = function.arguments->children.front().get();
|
|
|
|
auto * right = function.arguments->children.back().get();
|
|
|
|
auto * identifier = left->as<ASTIdentifier>();
|
|
|
|
if (!identifier)
|
|
|
|
return;
|
|
|
|
|
2023-08-10 18:41:57 +00:00
|
|
|
auto name = identifier->shortName();
|
|
|
|
if (!data.sharding_key_expr->getRequiredColumnsWithTypes().contains(name))
|
2021-03-07 15:51:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto * tuple_func = right->as<ASTFunction>(); tuple_func && tuple_func->name == "tuple")
|
|
|
|
{
|
|
|
|
auto * tuple_elements = tuple_func->children.front()->as<ASTExpressionList>();
|
|
|
|
std::erase_if(tuple_elements->children, [&](auto & child)
|
|
|
|
{
|
|
|
|
auto * literal = child->template as<ASTLiteral>();
|
2023-08-10 18:41:57 +00:00
|
|
|
return tuple_elements->children.size() > 1 && literal && !shardContains(literal->value, name, data);
|
2021-03-07 15:51:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (auto * tuple_literal = right->as<ASTLiteral>();
|
|
|
|
tuple_literal && tuple_literal->value.getType() == Field::Types::Tuple)
|
|
|
|
{
|
|
|
|
auto & tuple = tuple_literal->value.get<Tuple &>();
|
|
|
|
std::erase_if(tuple, [&](auto & child)
|
|
|
|
{
|
2023-08-10 18:41:57 +00:00
|
|
|
return tuple.size() > 1 && !shardContains(child, name, data);
|
2021-03-07 15:51:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:17:34 +00:00
|
|
|
|
2024-02-28 14:30:06 +00:00
|
|
|
class OptimizeShardingKeyRewriteIn : public InDepthQueryTreeVisitorWithContext<OptimizeShardingKeyRewriteIn>
|
2024-02-27 18:17:34 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Base = InDepthQueryTreeVisitorWithContext<OptimizeShardingKeyRewriteIn>;
|
2024-03-06 13:04:09 +00:00
|
|
|
|
2024-02-28 14:30:06 +00:00
|
|
|
OptimizeShardingKeyRewriteIn(OptimizeShardingKeyRewriteInVisitor::Data data_, ContextPtr context)
|
|
|
|
: Base(std::move(context))
|
|
|
|
, data(std::move(data_))
|
|
|
|
{}
|
2024-02-27 18:17:34 +00:00
|
|
|
|
|
|
|
void enterImpl(QueryTreeNodePtr & node)
|
|
|
|
{
|
|
|
|
auto * function_node = node->as<FunctionNode>();
|
|
|
|
if (!function_node || function_node->getFunctionName() != "in")
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & arguments = function_node->getArguments().getNodes();
|
|
|
|
auto * column = arguments[0]->as<ColumnNode>();
|
|
|
|
if (!column)
|
|
|
|
return;
|
|
|
|
|
2024-02-28 14:30:06 +00:00
|
|
|
auto name = column->getColumnName();
|
|
|
|
|
2024-02-27 18:17:34 +00:00
|
|
|
if (!data.sharding_key_expr->getRequiredColumnsWithTypes().contains(column->getColumnName()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto * constant = arguments[1]->as<ConstantNode>())
|
|
|
|
{
|
|
|
|
if (isTuple(constant->getResultType()))
|
|
|
|
{
|
2024-02-28 14:30:06 +00:00
|
|
|
const auto & tuple = constant->getValue().get<Tuple &>();
|
|
|
|
Tuple new_tuple;
|
|
|
|
new_tuple.reserve(tuple.size());
|
|
|
|
|
|
|
|
for (const auto & child : tuple)
|
2024-02-27 18:17:34 +00:00
|
|
|
{
|
2024-02-28 14:30:06 +00:00
|
|
|
if (shardContains(child, name, data))
|
|
|
|
new_tuple.push_back(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_tuple.empty())
|
|
|
|
new_tuple.push_back(tuple.back());
|
2024-03-15 15:35:31 +00:00
|
|
|
|
|
|
|
if (new_tuple.size() == tuple.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
arguments[1] = std::make_shared<ConstantNode>(new_tuple);
|
|
|
|
rerunFunctionResolve(function_node, getContext());
|
2024-02-27 18:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 14:30:06 +00:00
|
|
|
OptimizeShardingKeyRewriteInVisitor::Data data;
|
2024-02-27 18:17:34 +00:00
|
|
|
};
|
|
|
|
|
2024-02-28 14:30:06 +00:00
|
|
|
void optimizeShardingKeyRewriteIn(QueryTreeNodePtr & node, OptimizeShardingKeyRewriteInVisitor::Data data, ContextPtr context)
|
|
|
|
{
|
|
|
|
OptimizeShardingKeyRewriteIn visitor(std::move(data), std::move(context));
|
|
|
|
visitor.visit(node);
|
|
|
|
}
|
2024-02-27 18:17:34 +00:00
|
|
|
|
2021-03-07 15:51:01 +00:00
|
|
|
}
|