33#include < Analyzer/InDepthQueryTreeVisitor.h>
44#include < Analyzer/JoinNode.h>
55#include < Analyzer/QueryNode.h>
6+ #include < Analyzer/TableFunctionNode.h>
67#include < Analyzer/TableNode.h>
78#include < Analyzer/UnionNode.h>
89#include < Core/Settings.h>
@@ -30,6 +31,7 @@ namespace DB
3031{
3132namespace Setting
3233{
34+ extern const SettingsMap additional_table_filters;
3335 extern const SettingsBool parallel_replicas_allow_in_with_subquery;
3436 extern const SettingsBool parallel_replicas_for_non_replicated_merge_tree;
3537 extern const SettingsBool parallel_replicas_for_queries_with_multiple_tables;
@@ -45,7 +47,7 @@ namespace ErrorCodes
4547 extern const int UNSUPPORTED_METHOD ;
4648}
4749
48- bool isTableNodeEligibleForParallelReplicas (const TableNode & table_node, const StoragePtr & storage , const ContextPtr & context)
50+ static bool isStorageEligibleForParallelReplicas (const StoragePtr & storage, bool has_final , const ContextPtr & context)
4951{
5052 const auto & settings = context->getSettingsRef ();
5153
@@ -60,12 +62,70 @@ bool isTableNodeEligibleForParallelReplicas(const TableNode & table_node, const
6062 return false ;
6163
6264 // / Parallel replicas not supported with FINAL.
63- if (table_node. hasTableExpressionModifiers () && table_node. getTableExpressionModifiers ()-> hasFinal () )
65+ if (has_final )
6466 return false ;
6567
6668 return true ;
6769}
6870
71+ bool isTableNodeEligibleForParallelReplicas (const TableNode & table_node, const StoragePtr & storage, const ContextPtr & context)
72+ {
73+ const bool has_final = table_node.hasTableExpressionModifiers () && table_node.getTableExpressionModifiers ()->hasFinal ();
74+ return isStorageEligibleForParallelReplicas (storage, has_final, context);
75+ }
76+
77+ // / A table function that denotes the same source on every replica (`ITableFunction::getParallelReplicasReadID`)
78+ // / can anchor a parallel-replicas read just like a `TableNode`: every replica resolves the call locally and
79+ // / reads its own copy. A table function that builds a storage of its own cannot - it is not the same table on
80+ // / every replica - so it is not enough for the resolved storage to look like a `MergeTree`.
81+ bool queryTreeSetsAdditionalTableFilters (const IQueryTreeNode * query_tree_node)
82+ {
83+ std::vector<const IQueryTreeNode *> nodes{query_tree_node};
84+ while (!nodes.empty ())
85+ {
86+ const auto * node = nodes.back ();
87+ nodes.pop_back ();
88+
89+ ContextPtr node_context;
90+ if (const auto * query_node = node->as <QueryNode>())
91+ node_context = query_node->getContext ();
92+ else if (const auto * union_node = node->as <UnionNode>())
93+ node_context = union_node->getContext ();
94+ if (node_context && !node_context->getSettingsRef ()[Setting::additional_table_filters].value .empty ())
95+ return true ;
96+
97+ for (const auto & child : node->getChildren ())
98+ {
99+ if (child)
100+ nodes.push_back (child.get ());
101+ }
102+ }
103+ return false ;
104+ }
105+
106+ bool tableFunctionCanDriveParallelReplicasRead (const TableFunctionNode & table_function_node, bool query_sets_additional_table_filters)
107+ {
108+ if (!table_function_node.getParallelReplicasReadID ())
109+ return false ;
110+
111+ // / A read through a table function is sent to the replicas as query text even with `serialize_query_plan`,
112+ // / and a replica matches `additional_table_filters` against its own current database, which is not the
113+ // / initiator's. The filters could match here and not there, so the combination is not distributed. The
114+ // / text is the whole query, so filters set in any of its subqueries count, not only around this read.
115+ return !query_sets_additional_table_filters;
116+ }
117+
118+ static bool canUseTableFunctionForParallelReplicas (
119+ const TableFunctionNode & table_function_node, const IQueryTreeNode * query_tree_root, const ContextPtr & context)
120+ {
121+ if (!tableFunctionCanDriveParallelReplicasRead (table_function_node, queryTreeSetsAdditionalTableFilters (query_tree_root)))
122+ return false ;
123+
124+ const bool has_final
125+ = table_function_node.hasTableExpressionModifiers () && table_function_node.getTableExpressionModifiers ()->hasFinal ();
126+ return isStorageEligibleForParallelReplicas (table_function_node.getStorage (), has_final, context);
127+ }
128+
69129bool canUseTableForParallelReplicas (const TableNode & table_node, const ContextPtr & context)
70130{
71131 const auto & settings = context->getSettingsRef ();
@@ -110,6 +170,7 @@ bool canUseTableForParallelReplicas(const TableNode & table_node, const ContextP
110170static std::vector<const QueryNode *> getSupportingParallelReplicasQueries (const IQueryTreeNode * query_tree_node, const ContextPtr & context)
111171{
112172 const auto & settings = context->getSettingsRef ();
173+ const IQueryTreeNode * const query_tree_root = query_tree_node;
113174
114175 std::vector<const QueryNode *> res;
115176
@@ -132,6 +193,13 @@ static std::vector<const QueryNode *> getSupportingParallelReplicasQueries(const
132193 }
133194 case QueryTreeNodeType::TABLE_FUNCTION :
134195 {
196+ // / Kept in step with the `TABLE_FUNCTION` case of `findTableForParallelReplicas` below: a node
197+ // / that can anchor the read there must be able to reach this traversal too, otherwise the
198+ // / initiator picks an anchor for a query this one has already refused to distribute.
199+ const auto & table_function_node = query_tree_node->as <TableFunctionNode &>();
200+ if (canUseTableFunctionForParallelReplicas (table_function_node, query_tree_root, current_context))
201+ return res;
202+
135203 return {};
136204 }
137205 case QueryTreeNodeType::QUERY :
@@ -544,9 +612,10 @@ const QueryNode * findQueryForParallelReplicas(const QueryTreeNodePtr & query_tr
544612 return res;
545613}
546614
547- static const TableNode * findTableForParallelReplicas (const IQueryTreeNode * query_tree_node, const ContextPtr & context)
615+ static const ITableExpressionNode * findTableForParallelReplicas (const IQueryTreeNode * query_tree_node, const ContextPtr & context)
548616{
549617 const auto & settings = context->getSettingsRef ();
618+ const IQueryTreeNode * const query_tree_root = query_tree_node;
550619 std::stack<const IQueryTreeNode *> join_nodes;
551620 // / Enclosing (sub)query context per pending join branch: must match the one
552621 // / getSupportingParallelReplicasQueries used, or no eligible table is found here.
@@ -577,6 +646,10 @@ static const TableNode * findTableForParallelReplicas(const IQueryTreeNode * que
577646 }
578647 case QueryTreeNodeType::TABLE_FUNCTION :
579648 {
649+ const auto & table_function_node = query_tree_node->as <TableFunctionNode &>();
650+ if (canUseTableFunctionForParallelReplicas (table_function_node, query_tree_root, current_context))
651+ return &table_function_node;
652+
580653 query_tree_node = nullptr ;
581654 break ;
582655 }
@@ -652,7 +725,17 @@ static const TableNode * findTableForParallelReplicas(const IQueryTreeNode * que
652725 return nullptr ;
653726}
654727
655- const TableNode * findTableForParallelReplicas (const QueryTreeNodePtr & query_tree_node, const SelectQueryOptions & select_query_options)
728+ StorageID getStorageIDForParallelReplicas (const ITableExpressionNode & table_expression_node)
729+ {
730+ if (const auto * table_node = table_expression_node.as <TableNode>())
731+ return table_node->getStorageID ();
732+
733+ const auto * table_function_node = table_expression_node.as <TableFunctionNode>();
734+ chassert (table_function_node);
735+ return table_function_node->getStorageID ();
736+ }
737+
738+ const ITableExpressionNode * findTableForParallelReplicas (const QueryTreeNodePtr & query_tree_node, const SelectQueryOptions & select_query_options)
656739{
657740 if (select_query_options.only_analyze )
658741 return nullptr ;
@@ -780,14 +863,14 @@ JoinTreeQueryPlan buildQueryPlanForParallelReplicas(
780863 removeGroupingFunctionSpecializations (modified_query_tree_for_ast);
781864 ASTPtr modified_query_ast = queryNodeToDistributedSelectQuery (modified_query_tree_for_ast);
782865
783- const TableNode * table_node = findTableForParallelReplicas (modified_query_tree.get (), context);
784- if (!table_node )
866+ const auto * table_expression_node = findTableForParallelReplicas (modified_query_tree.get (), context);
867+ if (!table_expression_node )
785868 throw Exception (ErrorCodes::LOGICAL_ERROR , " Can't determine table for parallel replicas" );
786869
787870 QueryPlan query_plan;
788871 ClusterProxy::executeQueryWithParallelReplicas (
789872 query_plan,
790- table_node-> getStorageID ( ),
873+ getStorageIDForParallelReplicas (*table_expression_node ),
791874 header,
792875 processed_stage,
793876 modified_query_ast,
0 commit comments