Fix "Column is not under aggregate function and not in GROUP BY" with PREWHERE

In 21.7 the function column type is unknown:

  (lldb) p tmp_actions_dag->dumpDAG()
  (std::string) $14 = "0 : INPUT () (no column) Int32 key\n1 : COLUMN () Const(UInt8) UInt8 1\n2 : FUNCTION (0) (no column) UInt8 ignore(key) [ignore]\n3 : FUNCTION (1, 2) (no column) UInt8 or(1, ignore(key)) [or]\nIndex: 0 1 2 3\n"
                                                                                                                                                                            ^^^^^^^^^^^

While in 21.8+ it is const:

  (lldb) p tmp_actions_dag->dumpDAG()
  (std::string) $22 = "0 : INPUT () (no column) Int32 key\n1 : COLUMN () Const(UInt8) UInt8 1\n2 : FUNCTION (0) (no column) UInt8 ignore(key) [ignore]\n3 : FUNCTION (1, 2) Const(UInt8) UInt8 or(1, ignore(key)) [or]\nIndex: 0 1 2 3\n"
                                                                                                                                                                            ^^^^^

This is after getRootActions() in SelectQueryExpressionAnalyzer::appendPrewhere()

v1: fix for SELECT only, but breaks virtual columns
v2: hacky fix, that also touches MergeTree code
v3: allow_remove_inputs=false
v4: allow_constant_folding=false instead
This commit is contained in:
Azat Khuzhin 2021-09-02 08:35:30 +03:00
parent dd7bffcaf9
commit fbf98bea0b

View File

@ -1045,7 +1045,12 @@ ActionsDAGPtr SelectQueryExpressionAnalyzer::appendPrewhere(
/// Remove unused source_columns from prewhere actions.
auto tmp_actions_dag = std::make_shared<ActionsDAG>(sourceColumns());
getRootActions(select_query->prewhere(), only_types, tmp_actions_dag);
tmp_actions_dag->removeUnusedActions(NameSet{prewhere_column_name});
/// Constants cannot be removed since they can be used in other parts of the query.
/// And if they are not used anywhere, except PREWHERE, they will be removed on the next step.
tmp_actions_dag->removeUnusedActions(
NameSet{prewhere_column_name},
/* allow_remove_inputs= */ true,
/* allow_constant_folding= */ false);
auto required_columns = tmp_actions_dag->getRequiredColumnsNames();
NameSet required_source_columns(required_columns.begin(), required_columns.end());