-
Notifications
You must be signed in to change notification settings - Fork 19
Fix join filter pushdown #2249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix join filter pushdown #2249
Changes from 11 commits
6f91aa6
af87b90
4e1b754
a54a78a
dc2f772
78a27b3
28b3de8
0e14162
707dcdc
2018169
a1d4502
cdc4b28
d9ebb0b
cf210cb
18a667b
523f4a0
f9203a7
f459b98
a5e8776
cc31f79
6a44388
9925af7
59af131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,7 @@ | |
|
|
||
| #include <Core/Streaming/CursorTree_fwd.h> | ||
|
|
||
| #include <functional> | ||
| #include <ranges> | ||
|
|
||
| namespace DB | ||
|
|
@@ -1168,24 +1169,77 @@ bool hasUnknownColumn(const QueryTreeNodePtr & node, QueryTreeNodePtr table_expr | |
| return false; | ||
| } | ||
|
|
||
| void removeExpressionsThatDoNotDependOnTableIdentifiers( | ||
| namespace | ||
| { | ||
|
|
||
| template <typename KeepFunction> | ||
| bool walkOrdinaryFunctions(const QueryTreeNodePtr & node, KeepFunction && keep_function) | ||
| { | ||
| QueryTreeNodes stack = {node}; | ||
| while (!stack.empty()) | ||
| { | ||
| auto current = std::move(stack.back()); | ||
| stack.pop_back(); | ||
| if (!current) | ||
| continue; | ||
|
|
||
| const auto type = current->getNodeType(); | ||
| if (type == QueryTreeNodeType::QUERY || type == QueryTreeNodeType::UNION) | ||
| return false; | ||
|
|
||
| if (const auto * function = current->as<FunctionNode>()) | ||
| { | ||
| if (function->isWindowFunction() || function->isAggregateFunction()) | ||
| return false; | ||
| if (function->isOrdinaryFunction()) | ||
| { | ||
| auto function_base = function->getFunction(); | ||
| if (!function_base || !keep_function(function_base)) | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| for (const auto & child : current->getChildren()) | ||
| { | ||
| if (child) | ||
| stack.push_back(child); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool isSafeToDuplicateInQueryTree(const QueryTreeNodePtr & node) | ||
| { | ||
| return walkOrdinaryFunctions( | ||
| node, | ||
| [](const FunctionBasePtr & function_base) | ||
| { | ||
| return function_base->isDeterministicInScopeOfQuery() && !function_base->isStateful(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an AGENTS.md reference: AGENTS.md:L7-L7 Useful? React with 👍 / 👎. |
||
| }); | ||
| } | ||
|
|
||
| void filterConjunctions( | ||
| QueryTreeNodePtr & expression, | ||
| const QueryTreeNodePtr & table_expression, | ||
| const std::function<bool(const QueryTreeNodePtr &)> & keep, | ||
| const ContextPtr & context) | ||
| { | ||
| auto * function = expression->as<FunctionNode>(); | ||
| if (!function) | ||
| { | ||
| if (!keep(expression)) | ||
| expression = {}; | ||
| return; | ||
| } | ||
|
|
||
| if (function->getFunctionName() != "and") | ||
| { | ||
| if (hasUnknownColumn(expression, table_expression)) | ||
| expression = nullptr; | ||
| if (!keep(expression)) | ||
| expression = {}; | ||
| return; | ||
| } | ||
|
|
||
| QueryTreeNodesDeque conjunctions; | ||
| QueryTreeNodesDeque processing{ expression }; | ||
| QueryTreeNodesDeque processing{expression}; | ||
|
|
||
| while (!processing.empty()) | ||
| { | ||
|
|
@@ -1195,10 +1249,7 @@ void removeExpressionsThatDoNotDependOnTableIdentifiers( | |
| if (auto * function_node = node->as<FunctionNode>()) | ||
| { | ||
| if (function_node->getFunctionName() == "and") | ||
| std::ranges::copy( | ||
| function_node->getArguments(), | ||
| std::back_inserter(processing) | ||
| ); | ||
| std::ranges::copy(function_node->getArguments(), std::back_inserter(processing)); | ||
| else | ||
| conjunctions.push_back(node); | ||
| } | ||
|
|
@@ -1212,7 +1263,7 @@ void removeExpressionsThatDoNotDependOnTableIdentifiers( | |
|
|
||
| for (const auto & node : processing) | ||
| { | ||
| if (!hasUnknownColumn(node, table_expression)) | ||
| if (keep(node)) | ||
| conjunctions.push_back(node); | ||
| } | ||
|
|
||
|
|
@@ -1234,6 +1285,29 @@ void removeExpressionsThatDoNotDependOnTableIdentifiers( | |
| function->resolveAsFunction(function_impl->build(function->getArgumentColumns())); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| void removeExpressionsThatDoNotDependOnTableIdentifiers( | ||
| QueryTreeNodePtr & expression, | ||
| const QueryTreeNodePtr & table_expression, | ||
| const ContextPtr & context) | ||
| { | ||
| filterConjunctions( | ||
| expression, | ||
| [&](const QueryTreeNodePtr & node) { return !hasUnknownColumn(node, table_expression); }, | ||
| context); | ||
| } | ||
|
|
||
| void removeExpressionsThatAreUnsafeToDuplicate( | ||
| QueryTreeNodePtr & expression, | ||
| const ContextPtr & context) | ||
| { | ||
| if (!expression) | ||
| return; | ||
|
|
||
| filterConjunctions(expression, isSafeToDuplicateInQueryTree, context); | ||
| } | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we should to do if function not
isOrdinaryFunction? Just ignoring?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Window and aggregate functions are checked above, so here can be only ordinary or unresolved functions.
But all functions must be resolved when this method is called.
I rewrite test to be more clean, without logic change.