11#include < Common/typeid_cast.h>
2+ #include < Interpreters/InterpreterSelectQueryAnalyzer.h>
23#include < IO/WriteHelpers.h>
34
45#include < Storages/IStorage.h>
@@ -21,6 +22,28 @@ namespace ErrorCodes
2122 extern const int LOGICAL_ERROR;
2223}
2324
25+ static ASTPtr buildQueryAST (const ASTPtr & table_expression, const ContextPtr & context, SelectQueryOptions & subquery_options);
26+
27+ static ContextPtr getSubqueryContext (const ContextPtr & context)
28+ {
29+ /* * The subquery in the IN / JOIN section does not have any restrictions on the maximum size of the result.
30+ * Because the result of this query is not the result of the entire query.
31+ * Constraints work instead
32+ * max_rows_in_set, max_bytes_in_set, set_overflow_mode,
33+ * max_rows_in_join, max_bytes_in_join, join_overflow_mode,
34+ * which are checked separately (in the Set, Join objects).
35+ */
36+ auto subquery_context = Context::createCopy (context);
37+ Settings subquery_settings = context->getSettings ();
38+ subquery_settings.max_result_rows = 0 ;
39+ subquery_settings.max_result_bytes = 0 ;
40+ // / The calculation of `extremes` does not make sense and is not necessary (if you do it, then the `extremes` of the subquery can be taken instead of the whole query).
41+ subquery_settings.extremes = false ;
42+ subquery_context->setSettings (subquery_settings);
43+
44+ return subquery_context;
45+ }
46+
2447std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery (
2548 const ASTPtr & table_expression, ContextPtr context, size_t subquery_depth, const Names & required_source_columns)
2649{
@@ -30,6 +53,23 @@ std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery(
3053
3154std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery (
3255 const ASTPtr & table_expression, ContextPtr context, const Names & required_source_columns, const SelectQueryOptions & options)
56+ {
57+ auto subquery_options = options.subquery ();
58+ auto query = buildQueryAST (table_expression, context, subquery_options);
59+ auto subquery_context = getSubqueryContext (context);
60+ return std::make_shared<InterpreterSelectWithUnionQuery>(query, subquery_context, subquery_options, required_source_columns);
61+ }
62+
63+ std::shared_ptr<InterpreterSelectQueryAnalyzer> interpretSubquery (
64+ const ASTPtr & table_expression, ContextPtr context, size_t subquery_depth)
65+ {
66+ auto subquery_options = SelectQueryOptions (QueryProcessingStage::Complete, subquery_depth).subquery ();
67+ auto query = buildQueryAST (table_expression, context, subquery_options);
68+ auto subquery_context = getSubqueryContext (context);
69+ return std::make_shared<InterpreterSelectQueryAnalyzer>(query, subquery_context, subquery_options);
70+ }
71+
72+ static ASTPtr buildQueryAST (const ASTPtr & table_expression, const ContextPtr & context, SelectQueryOptions & subquery_options)
3373{
3474 if (auto * expr = table_expression->as <ASTTableExpression>())
3575 {
@@ -41,7 +81,7 @@ std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery(
4181 else if (expr->database_and_table_name )
4282 table = expr->database_and_table_name ;
4383
44- return interpretSubquery (table, context, required_source_columns, options );
84+ return buildQueryAST (table, context, subquery_options );
4585 }
4686
4787 // / Subquery or table name. The name of the table is similar to the subquery `SELECT * FROM t`.
@@ -52,23 +92,6 @@ std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery(
5292 if (!subquery && !table && !function)
5393 throw Exception (ErrorCodes::LOGICAL_ERROR, " Table expression is undefined, Method: ExpressionAnalyzer::interpretSubquery." );
5494
55- /* * The subquery in the IN / JOIN section does not have any restrictions on the maximum size of the result.
56- * Because the result of this query is not the result of the entire query.
57- * Constraints work instead
58- * max_rows_in_set, max_bytes_in_set, set_overflow_mode,
59- * max_rows_in_join, max_bytes_in_join, join_overflow_mode,
60- * which are checked separately (in the Set, Join objects).
61- */
62- auto subquery_context = Context::createCopy (context);
63- Settings subquery_settings = context->getSettings ();
64- subquery_settings.max_result_rows = 0 ;
65- subquery_settings.max_result_bytes = 0 ;
66- // / The calculation of `extremes` does not make sense and is not necessary (if you do it, then the `extremes` of the subquery can be taken instead of the whole query).
67- subquery_settings.extremes = false ;
68- subquery_context->setSettings (subquery_settings);
69-
70- auto subquery_options = options.subquery ();
71-
7295 ASTPtr query;
7396 if (table || function)
7497 {
@@ -112,7 +135,7 @@ std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery(
112135 subquery_options.removeDuplicates ();
113136 }
114137
115- return std::make_shared<InterpreterSelectWithUnionQuery>( query, subquery_context, subquery_options, required_source_columns) ;
138+ return query;
116139}
117140
118141}
0 commit comments