@@ -229,39 +229,29 @@ void IStorageCluster::updateQueryWithJoinToSendIfNeeded(
229229 {
230230 auto modified_query_tree = query_tree->clone ();
231231
232- SearcherVisitor table_function_searcher ({QueryTreeNodeType::TABLE, QueryTreeNodeType::TABLE_FUNCTION}, context);
233- table_function_searcher .visit (modified_query_tree);
234- auto table_function_node = table_function_searcher .getNode ();
232+ SearcherVisitor left_table_expression_searcher ({QueryTreeNodeType::TABLE, QueryTreeNodeType::TABLE_FUNCTION}, context);
233+ left_table_expression_searcher .visit (modified_query_tree);
234+ auto table_function_node = left_table_expression_searcher .getNode ();
235235 if (!table_function_node)
236236 throw Exception (ErrorCodes::LOGICAL_ERROR, " Can't find table function node" );
237237
238238 QueryTreeNodePtr query_tree_distributed;
239239
240240 auto & query_node = modified_query_tree->as <QueryNode &>();
241241
242- if (info.has_join || info. has_cross_join )
242+ if (info.has_join )
243243 {
244- if (table_function_searcher.getType ().value () == QueryTreeNodeType::TABLE_FUNCTION)
245- {
246- auto table_function = extractTableFunctionASTPtrFromSelectQuery (query_to_send);
247- query_tree_distributed = buildTableFunctionQueryTree (table_function, context);
248- auto & table_function_ast = table_function->as <ASTFunction &>();
249- query_tree_distributed->setAlias (table_function_ast.alias );
250- }
251- else if (info.has_join )
252- {
253- auto join_node = query_node.getJoinTree ();
254- query_tree_distributed = join_node->as <JoinNode>()->getLeftTableExpression ()->clone ();
255- }
256- else
257- {
258- SearcherVisitor join_searcher ({QueryTreeNodeType::CROSS_JOIN}, context);
259- join_searcher.visit (modified_query_tree);
260- auto cross_join_node = join_searcher.getNode ();
261- if (!cross_join_node)
262- throw Exception (ErrorCodes::LOGICAL_ERROR, " Can't find CROSS JOIN node" );
263- query_tree_distributed = cross_join_node->as <CrossJoinNode>()->getTableExpressions ()[0 ]->clone ();
264- }
244+ auto join_node = query_node.getJoinTree ();
245+ query_tree_distributed = join_node->as <JoinNode>()->getLeftTableExpression ()->clone ();
246+ }
247+ else if (info.has_cross_join )
248+ {
249+ SearcherVisitor join_searcher ({QueryTreeNodeType::CROSS_JOIN}, context);
250+ join_searcher.visit (modified_query_tree);
251+ auto cross_join_node = join_searcher.getNode ();
252+ if (!cross_join_node)
253+ throw Exception (ErrorCodes::LOGICAL_ERROR, " Can't find CROSS JOIN node" );
254+ query_tree_distributed = cross_join_node->as <CrossJoinNode>()->getTableExpressions ()[0 ]->clone ();
265255 }
266256
267257 // Find add used columns from table function to make proper projection list
@@ -545,9 +535,9 @@ IStorageCluster::QueryTreeInfo IStorageCluster::getQueryTreeInfo(QueryTreeNodePt
545535 info.has_cross_join = true ;
546536 }
547537
548- SearcherVisitor table_function_searcher ({QueryTreeNodeType::TABLE, QueryTreeNodeType::TABLE_FUNCTION}, context);
549- table_function_searcher .visit (query_tree);
550- auto table_function_node = table_function_searcher .getNode ();
538+ SearcherVisitor left_table_expression_searcher ({QueryTreeNodeType::TABLE, QueryTreeNodeType::TABLE_FUNCTION}, context);
539+ left_table_expression_searcher .visit (query_tree);
540+ auto table_function_node = left_table_expression_searcher .getNode ();
551541 if (!table_function_node)
552542 throw Exception (ErrorCodes::LOGICAL_ERROR, " Can't find table or table function node" );
553543
@@ -560,8 +550,9 @@ IStorageCluster::QueryTreeInfo IStorageCluster::getQueryTreeInfo(QueryTreeNodePt
560550 if (query_node.hasWhere ())
561551 collector_where.visit (query_node.getWhere ());
562552
563- // SELECT x FROM datalake.table WHERE x IN local.table
553+ // SELECT x FROM datalake.table WHERE x IN local.table.
564554 // Need to modify 'WHERE' on remote node if it contains columns from other sources
555+ // because remote node might not have those sources.
565556 if (!collector_where.getColumns ().empty ())
566557 info.has_local_columns_in_where = true ;
567558 }
0 commit comments