From 3f156b83c983eb55fe3c6f2bd4990afdacceaaee Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 18 Apr 2021 09:07:09 +0300 Subject: [PATCH] Add some comments in OptimizeShardingKeyRewriteInVisitor --- .../OptimizeShardingKeyRewriteInVisitor.cpp | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Interpreters/OptimizeShardingKeyRewriteInVisitor.cpp b/src/Interpreters/OptimizeShardingKeyRewriteInVisitor.cpp index 4d1c0526910..a031b8dc962 100644 --- a/src/Interpreters/OptimizeShardingKeyRewriteInVisitor.cpp +++ b/src/Interpreters/OptimizeShardingKeyRewriteInVisitor.cpp @@ -13,7 +13,7 @@ using namespace DB; Field executeFunctionOnField( const Field & field, const std::string & name, - const ExpressionActionsPtr & expr, + const ExpressionActionsPtr & sharding_expr, const std::string & sharding_key_column_name) { DataTypePtr type = applyVisitor(FieldToDataType{}, field); @@ -25,17 +25,23 @@ Field executeFunctionOnField( Block block{column}; size_t num_rows = 1; - expr->execute(block, num_rows); + sharding_expr->execute(block, num_rows); ColumnWithTypeAndName & ret = block.getByName(sharding_key_column_name); return (*ret.column)[0]; } -/// Return true if shard may contain such value (or it is unknown), otherwise false. +/// @param sharding_column_value - one of values from IN +/// @param sharding_column_name - name of that column +/// @param sharding_expr - expression of sharding_key for the Distributed() table +/// @param sharding_key_column_name - name of the column for sharding_expr +/// @param shard_info - info for the current shard (to compare shard_num with calculated) +/// @param slots - weight -> shard mapping +/// @return true if shard may contain such value (or it is unknown), otherwise false. bool shardContains( const Field & sharding_column_value, const std::string & sharding_column_name, - const ExpressionActionsPtr & expr, + const ExpressionActionsPtr & sharding_expr, const std::string & sharding_key_column_name, const Cluster::ShardInfo & shard_info, const Cluster::SlotToShard & slots) @@ -45,7 +51,7 @@ bool shardContains( if (sharding_column_value.isNull()) return false; - Field sharding_value = executeFunctionOnField(sharding_column_value, sharding_column_name, expr, sharding_key_column_name); + Field sharding_value = executeFunctionOnField(sharding_column_value, sharding_column_name, sharding_expr, sharding_key_column_name); UInt64 value = sharding_value.get(); const auto shard_num = slots[value % slots.size()] + 1; return shard_info.shard_num == shard_num; @@ -78,10 +84,10 @@ void OptimizeShardingKeyRewriteInMatcher::visit(ASTFunction & function, Data & d if (!identifier) return; - const auto & expr = data.sharding_key_expr; + const auto & sharding_expr = data.sharding_key_expr; const auto & sharding_key_column_name = data.sharding_key_column_name; - if (!expr->getRequiredColumnsWithTypes().contains(identifier->name())) + if (!sharding_expr->getRequiredColumnsWithTypes().contains(identifier->name())) return; /// NOTE: that we should not take care about empty tuple, @@ -93,7 +99,7 @@ void OptimizeShardingKeyRewriteInMatcher::visit(ASTFunction & function, Data & d std::erase_if(tuple_elements->children, [&](auto & child) { auto * literal = child->template as(); - return literal && !shardContains(literal->value, identifier->name(), expr, sharding_key_column_name, data.shard_info, data.slots); + return literal && !shardContains(literal->value, identifier->name(), sharding_expr, sharding_key_column_name, data.shard_info, data.slots); }); } else if (auto * tuple_literal = right->as(); @@ -102,7 +108,7 @@ void OptimizeShardingKeyRewriteInMatcher::visit(ASTFunction & function, Data & d auto & tuple = tuple_literal->value.get(); std::erase_if(tuple, [&](auto & child) { - return !shardContains(child, identifier->name(), expr, sharding_key_column_name, data.shard_info, data.slots); + return !shardContains(child, identifier->name(), sharding_expr, sharding_key_column_name, data.shard_info, data.slots); }); } }