Skip to content

Commit a5ff5db

Browse files
committed
fixed
1 parent c0cc56f commit a5ff5db

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenClickHouseTPCHSuite.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,13 @@ class GlutenClickHouseTPCHSuite extends MergeTreeSuite {
468468
|""".stripMargin
469469
spark.sql(sql)
470470
sql = """
471-
| select * from cross_join_t as t1 full join cross_join_t as t2 limit 10
471+
|select * from (
472+
| select a as a1, b as b1, c as c1 from cross_join_t
473+
|) as t1 full join (
474+
| select a as a2, b as b2, c as c2 from cross_join_t
475+
|) as t2
476+
|order by a1, b1, c1, a2, b2, c2
477+
|limit 10
472478
|""".stripMargin
473479
compareResultsAgainstVanillaSpark(sql, true, { _ => })
474480
spark.sql("drop table cross_join_t")

cpp-ch/local-engine/Common/CHUtil.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,16 +1048,25 @@ UInt64 MemoryUtil::getMemoryRSS()
10481048
return rss * sysconf(_SC_PAGESIZE);
10491049
}
10501050

1051-
1052-
void JoinUtil::reorderJoinOutput(DB::QueryPlan & plan, DB::Names cols)
1051+
void JoinUtil::adjustJoinOutput(DB::QueryPlan & plan, DB::Names cols)
10531052
{
1054-
ActionsDAG project{plan.getCurrentHeader()->getNamesAndTypesList()};
1055-
NamesWithAliases project_cols;
1053+
auto header = plan.getCurrentHeader();
1054+
std::unordered_map<String, const DB::ActionsDAG::Node *> name_to_node;
1055+
ActionsDAG project;
1056+
for (const auto & col : header->getColumnsWithTypeAndName())
1057+
{
1058+
const auto * node = &(project.addInput(col));
1059+
name_to_node[col.name] = node;
1060+
}
10561061
for (const auto & col : cols)
10571062
{
1058-
project_cols.emplace_back(NameWithAlias(col, col));
1063+
const auto it = name_to_node.find(col);
1064+
if (it == name_to_node.end())
1065+
{
1066+
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Column {} not found in header", col);
1067+
}
1068+
project.addOrReplaceInOutputs(*(it->second));
10591069
}
1060-
project.project(project_cols);
10611070
QueryPlanStepPtr project_step = std::make_unique<ExpressionStep>(plan.getCurrentHeader(), std::move(project));
10621071
project_step->setStepDescription("Reorder Join Output");
10631072
plan.addStep(std::move(project_step));

cpp-ch/local-engine/Common/CHUtil.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ static const String MERGETREE_INSERT_WITHOUT_LOCAL_STORAGE = "mergetree.insert_w
4242
static const String MERGETREE_MERGE_AFTER_INSERT = "mergetree.merge_after_insert";
4343
static const std::string DECIMAL_OPERATIONS_ALLOW_PREC_LOSS = "spark.sql.decimalOperations.allowPrecisionLoss";
4444
static const std::string TIMER_PARSER_POLICY = "spark.sql.legacy.timeParserPolicy";
45-
// static constexpr auto CROSS_REL_CONST_KEY_COLUMN = "__CROSS_REL_CONST_KEY_COLUMN__";
46-
4745

4846
static const std::unordered_set<String> BOOL_VALUE_SETTINGS{
4947
MERGETREE_MERGE_AFTER_INSERT, MERGETREE_INSERT_WITHOUT_LOCAL_STORAGE, DECIMAL_OPERATIONS_ALLOW_PREC_LOSS};
@@ -254,7 +252,8 @@ class JoinUtil
254252
static constexpr auto CROSS_REL_LEFT_CONST_KEY_COLUMN = "__CROSS_REL_LEFT_CONST_KEY_COLUMN__";
255253
static constexpr auto CROSS_REL_RIGHT_CONST_KEY_COLUMN = "__CROSS_REL_RIGHT_CONST_KEY_COLUMN__";
256254

257-
static void reorderJoinOutput(DB::QueryPlan & plan, DB::Names cols);
255+
// Keep necessarily columns and reorder them according to cols
256+
static void adjustJoinOutput(DB::QueryPlan & plan, DB::Names cols);
258257
static std::pair<DB::JoinKind, DB::JoinStrictness>
259258
getJoinKindAndStrictness(substrait::JoinRel_JoinType join_type, bool is_existence_join);
260259
static std::pair<DB::JoinKind, DB::JoinStrictness> getCrossJoinKindAndStrictness(substrait::CrossRel_JoinType join_type);

cpp-ch/local-engine/Parser/RelParsers/CrossRelParser.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,7 @@ DB::QueryPlanPtr CrossRelParser::parseJoin(const substrait::CrossRel & join, DB:
189189
right->getCurrentHeader()->dumpNames());
190190
}
191191

192-
Names after_join_names;
193-
auto left_names = left->getCurrentHeader()->getNames();
194-
after_join_names.insert(after_join_names.end(), left_names.begin(), left_names.end());
195-
auto right_name = table_join->columnsFromJoinedTable().getNames();
196-
after_join_names.insert(after_join_names.end(), right_name.begin(), right_name.end());
197-
198-
auto left_header = left->getCurrentHeader();
199-
auto right_header = right->getCurrentHeader();
200-
192+
Names after_join_names = collectOutputColumnsName(*left, *right);
201193

202194
if (table_join->kind() != JoinKind::Cross)
203195
{
@@ -223,15 +215,7 @@ DB::QueryPlanPtr CrossRelParser::parseJoin(const substrait::CrossRel & join, DB:
223215
extra_plan_holder.emplace_back(std::move(right));
224216

225217
addPostFilter(*query_plan, join);
226-
Names cols;
227-
for (auto after_join_name : after_join_names)
228-
{
229-
if (BlockUtil::VIRTUAL_ROW_COUNT_COLUMN == after_join_name)
230-
continue;
231-
232-
cols.emplace_back(after_join_name);
233-
}
234-
JoinUtil::reorderJoinOutput(*query_plan, cols);
218+
JoinUtil::adjustJoinOutput(*query_plan, after_join_names);
235219
}
236220
else
237221
{
@@ -255,7 +239,7 @@ DB::QueryPlanPtr CrossRelParser::parseJoin(const substrait::CrossRel & join, DB:
255239

256240
query_plan = std::make_unique<QueryPlan>();
257241
query_plan->unitePlans(std::move(join_step), {std::move(plans)});
258-
JoinUtil::reorderJoinOutput(*query_plan, after_join_names);
242+
JoinUtil::adjustJoinOutput(*query_plan, after_join_names);
259243
}
260244

261245
return query_plan;
@@ -357,6 +341,26 @@ void CrossRelParser::addConvertStep(TableJoin & table_join, DB::QueryPlan & left
357341
}
358342
}
359343

344+
DB::Names CrossRelParser::collectOutputColumnsName(const DB::QueryPlan & left, const DB::QueryPlan & right)
345+
{
346+
Names join_result_names;
347+
auto is_unused_column = [](const String & name)
348+
{
349+
return name == JoinUtil::CROSS_REL_LEFT_CONST_KEY_COLUMN || name == JoinUtil::CROSS_REL_RIGHT_CONST_KEY_COLUMN
350+
|| name == BlockUtil::VIRTUAL_ROW_COUNT_COLUMN;
351+
};
352+
for (auto & col : left.getCurrentHeader()->getColumnsWithTypeAndName())
353+
{
354+
if (!is_unused_column(col.name))
355+
join_result_names.emplace_back(col.name);
356+
}
357+
for (auto & col : right.getCurrentHeader()->getColumnsWithTypeAndName())
358+
{
359+
if (!is_unused_column(col.name))
360+
join_result_names.emplace_back(col.name);
361+
}
362+
return join_result_names;
363+
}
360364

361365
void registerCrossRelParser(RelParserFactory & factory)
362366
{

cpp-ch/local-engine/Parser/RelParsers/CrossRelParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CrossRelParser : public RelParser
6767
bool allow_mixed_condition);
6868

6969
void addConstJoinKeys(DB::QueryPlan & left, DB::QueryPlan & right);
70+
DB::Names collectOutputColumnsName(const DB::QueryPlan & left, const DB::QueryPlan & right);
7071
};
7172

7273
}

cpp-ch/local-engine/Parser/RelParsers/JoinRelParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ DB::QueryPlanPtr JoinRelParser::parseJoin(const substrait::JoinRel & join, DB::Q
358358
}
359359
}
360360

361-
JoinUtil::reorderJoinOutput(*query_plan, after_join_names);
361+
JoinUtil::adjustJoinOutput(*query_plan, after_join_names);
362362
/// Need to project the right table column into boolean type
363363
if (join_opt_info.is_existence_join)
364364
existenceJoinPostProject(*query_plan, left_names);

0 commit comments

Comments
 (0)