Try fix more cases.

This commit is contained in:
Nikolai Kochetov 2021-05-19 15:16:18 +03:00
parent 9d152a3c4d
commit c07043fd97
3 changed files with 19 additions and 7 deletions

View File

@ -92,6 +92,11 @@ Block evaluateConstantGroupByKeysWithShardNumber(
{ {
Block res; Block res;
ColumnWithTypeAndName shard_num_col;
shard_num_col.type = std::make_shared<DataTypeUInt32>();
shard_num_col.column = shard_num_col.type->createColumnConst(0, shard_num);
shard_num_col.name = "_shard_num";
if (auto group_by = query_ast->as<ASTSelectQuery &>().groupBy()) if (auto group_by = query_ast->as<ASTSelectQuery &>().groupBy())
{ {
for (const auto & elem : group_by->children) for (const auto & elem : group_by->children)
@ -108,19 +113,20 @@ Block evaluateConstantGroupByKeysWithShardNumber(
if (required_columns.size() != 1 || required_columns.count("_shard_num") == 0) if (required_columns.size() != 1 || required_columns.count("_shard_num") == 0)
continue; continue;
auto type = std::make_shared<DataTypeUInt32>(); Block block({shard_num_col});
auto col = type->createColumnConst(1, shard_num); auto syntax_result = TreeRewriter(context).analyze(ast, {NameAndTypePair{shard_num_col.name, shard_num_col.type}});
std::string name = "_shard_num"; ExpressionAnalyzer(ast, syntax_result, context).getActions(true, false)->execute(block);
Block block({ColumnWithTypeAndName{col, type, name}});
auto syntax_result = TreeRewriter(context).analyze(ast, {NameAndTypePair{name, type}});
ExpressionAnalyzer(ast, syntax_result, context).getActions(true)->execute(block);
res.insert(block.getByName(key_name)); res.insert(block.getByName(key_name));
} }
} }
} }
/// We always add _shard_num constant just in case.
/// For initial query it is considered as a column from table, and may be required by intermediate block.
if (!res.has(shard_num_col.name))
res.insert(std::move(shard_num_col));
return res; return res;
} }

View File

@ -14,3 +14,5 @@
2 2
1 1
2 2
1 1
2 1

View File

@ -10,3 +10,7 @@ SELECT _shard_num + dummy s, count() FROM remote('127.0.0.{1,2}', system.one) GR
SELECT _shard_num FROM remote('127.0.0.{1,2}', system.one) ORDER BY _shard_num; SELECT _shard_num FROM remote('127.0.0.{1,2}', system.one) ORDER BY _shard_num;
SELECT _shard_num s FROM remote('127.0.0.{1,2}', system.one) ORDER BY _shard_num; SELECT _shard_num s FROM remote('127.0.0.{1,2}', system.one) ORDER BY _shard_num;
SELECT _shard_num s, count() FROM remote('127.0.0.{1,2}', system.one) GROUP BY s order by s;
select materialize(_shard_num), * from remote('127.{1,2}', system.one) limit 1 by dummy format Null;