Fix incorrect sharding key types for optimize_skip_unused_shards_rewrite_in

Before it always uses the first column from sample block, while it is
not guaranteed.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2024-03-28 14:38:28 +01:00
parent 5576cb7964
commit 32ed192434
3 changed files with 8 additions and 10 deletions

View File

@ -249,7 +249,6 @@ void executeQuery(
{ {
OptimizeShardingKeyRewriteInVisitor::Data visitor_data{ OptimizeShardingKeyRewriteInVisitor::Data visitor_data{
sharding_key_expr, sharding_key_expr,
sharding_key_expr->getSampleBlock().getByPosition(0).type,
sharding_key_column_name, sharding_key_column_name,
shard_info, shard_info,
not_optimized_cluster->getSlotToShard(), not_optimized_cluster->getSlotToShard(),
@ -286,7 +285,6 @@ void executeQuery(
{ {
OptimizeShardingKeyRewriteInVisitor::Data visitor_data{ OptimizeShardingKeyRewriteInVisitor::Data visitor_data{
sharding_key_expr, sharding_key_expr,
sharding_key_expr->getSampleBlock().getByPosition(0).type,
sharding_key_column_name, sharding_key_column_name,
shard_info, shard_info,
not_optimized_cluster->getSlotToShard(), not_optimized_cluster->getSlotToShard(),

View File

@ -38,25 +38,27 @@ Field executeFunctionOnField(
return (*ret.column)[0]; return (*ret.column)[0];
} }
/// @param sharding_column_value - one of values from IN /// @param column_value - one of values from IN
/// @param sharding_column_name - name of that column /// @param sharding_column_name - name of that column
/// @return true if shard may contain such value (or it is unknown), otherwise false. /// @return true if shard may contain such value (or it is unknown), otherwise false.
bool shardContains( bool shardContains(
Field sharding_column_value, Field column_value,
const std::string & sharding_column_name, const std::string & sharding_column_name,
const OptimizeShardingKeyRewriteInMatcher::Data & data) const OptimizeShardingKeyRewriteInMatcher::Data & data)
{ {
/// 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;
/// Implicit conversion. /// Implicit conversion.
sharding_column_value = convertFieldToType(sharding_column_value, *data.sharding_key_type); column_value = convertFieldToType(column_value, *column_type);
/// NULL is not allowed in sharding key, /// NULL is not allowed in sharding key,
/// so it should be safe to assume that shard cannot contain it. /// so it should be safe to assume that shard cannot contain it.
if (sharding_column_value.isNull()) if (column_value.isNull())
return false; return false;
Field sharding_value = executeFunctionOnField( Field sharding_value = executeFunctionOnField(
sharding_column_value, sharding_column_name, column_value, sharding_column_name,
data.sharding_key_expr, data.sharding_key_type, data.sharding_key_expr, column_type,
data.sharding_key_column_name); data.sharding_key_column_name);
/// The value from IN can be non-numeric, /// The value from IN can be non-numeric,
/// but in this case it should be convertible to numeric type, let's try. /// but in this case it should be convertible to numeric type, let's try.

View File

@ -28,8 +28,6 @@ struct OptimizeShardingKeyRewriteInMatcher
{ {
/// Expression of sharding_key for the Distributed() table /// Expression of sharding_key for the Distributed() table
const ExpressionActionsPtr & sharding_key_expr; const ExpressionActionsPtr & sharding_key_expr;
/// Type of sharding_key column.
const DataTypePtr & sharding_key_type;
/// Name of the column for sharding_expr /// Name of the column for sharding_expr
const std::string & sharding_key_column_name; const std::string & sharding_key_column_name;
/// Info for the current shard (to compare shard_num with calculated) /// Info for the current shard (to compare shard_num with calculated)