Skip to content

Commit d0d39c8

Browse files
Trying to fix a test.
1 parent 1663905 commit d0d39c8

5 files changed

Lines changed: 75 additions & 23 deletions

File tree

src/Interpreters/ActionsVisitor.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <Common/quoteString.h>
44
#include <Common/typeid_cast.h>
55
#include <Common/FieldVisitorsAccurateComparison.h>
6-
#include "Parsers/queryToString.h"
6+
#include <Analyzer/QueryNode.h>
7+
#include <Parsers/queryToString.h>
78

89
#include <Core/ColumnNumbers.h>
910
#include <Core/ColumnWithTypeAndName.h>
@@ -55,6 +56,7 @@
5556
#include <Interpreters/interpretSubquery.h>
5657
#include <Interpreters/DatabaseAndTableWithAlias.h>
5758
#include <Interpreters/IdentifierSemantic.h>
59+
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
5860
#include <Functions/UserDefined/UserDefinedExecutableFunctionFactory.h>
5961
#include <Parsers/QueryParameterVisitor.h>
6062

@@ -1394,7 +1396,18 @@ FutureSetPtr ActionsMatcher::makeSet(const ASTFunction & node, Data & data, bool
13941396
if (no_subqueries)
13951397
return {};
13961398
//std::cerr << queryToString(right_in_operand) << std::endl;
1397-
auto set_key = PreparedSetKey::forSubquery(right_in_operand->getTreeHash());
1399+
PreparedSetKey set_key;
1400+
if (data.getContext()->getSettingsRef().allow_experimental_analyzer)
1401+
{
1402+
InterpreterSelectQueryAnalyzer interpreter(right_in_operand, data.getContext(), SelectQueryOptions().analyze(true).subquery());
1403+
auto query_tree = interpreter.getQueryTree();
1404+
if (auto * query_node = query_tree->as<QueryNode>())
1405+
query_node->setIsSubquery(true);
1406+
// std::cerr << "============== " << interpreter.getQueryTree()->dumpTree() << std::endl;
1407+
set_key = PreparedSetKey::forSubquery(interpreter.getQueryTree()->getTreeHash());
1408+
}
1409+
else
1410+
set_key = PreparedSetKey::forSubquery(right_in_operand->getTreeHash());
13981411

13991412
// std::cerr << set_key.toString() << std::endl;
14001413
// std::cerr << data.prepared_sets->getSets().size() << std::endl;
@@ -1446,8 +1459,16 @@ FutureSetPtr ActionsMatcher::makeSet(const ASTFunction & node, Data & data, bool
14461459
* Also it doesn't make sense if it is GLOBAL IN or ordinary IN.
14471460
*/
14481461
{
1449-
auto interpreter = interpretSubquery(right_in_operand, data.getContext(), data.subquery_depth, {});
1450-
subquery_for_set.createSource(*interpreter);
1462+
if (data.getContext()->getSettingsRef().allow_experimental_analyzer)
1463+
{
1464+
auto interpreter = interpretSubquery(right_in_operand, data.getContext(), data.subquery_depth);
1465+
subquery_for_set.source = std::make_unique<QueryPlan>(std::move(*interpreter).extractQueryPlan());
1466+
}
1467+
else
1468+
{
1469+
auto interpreter = interpretSubquery(right_in_operand, data.getContext(), data.subquery_depth, {});
1470+
subquery_for_set.createSource(*interpreter);
1471+
}
14511472
}
14521473

14531474
return data.prepared_sets->addFromSubquery(set_key, std::move(subquery_for_set), data.getContext()->getSettingsRef(), std::move(external_table_set));

src/Interpreters/InterpreterSelectQueryAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class InterpreterSelectQueryAnalyzer : public IInterpreter
6969
const Planner & getPlanner() const { return planner; }
7070
Planner & getPlanner() { return planner; }
7171

72+
const QueryTreeNodePtr & getQueryTree() const { return query_tree; }
73+
7274
private:
7375
ASTPtr query;
7476
ContextMutablePtr context;

src/Interpreters/interpretSubquery.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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+
2447
std::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

3154
std::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
}

src/Interpreters/interpretSubquery.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
namespace DB
77
{
88

9+
class InterpreterSelectQueryAnalyzer;
10+
11+
std::shared_ptr<InterpreterSelectQueryAnalyzer> interpretSubquery(
12+
const ASTPtr & table_expression, ContextPtr context, size_t subquery_depth);
13+
914
std::shared_ptr<InterpreterSelectWithUnionQuery> interpretSubquery(
1015
const ASTPtr & table_expression, ContextPtr context, size_t subquery_depth, const Names & required_source_columns);
1116

src/Planner/CollectSets.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class CollectSetsVisitor : public ConstInDepthQueryTreeVisitor<CollectSetsVisito
9393
in_second_argument_node_type == QueryTreeNodeType::UNION ||
9494
in_second_argument_node_type == QueryTreeNodeType::TABLE)
9595
{
96+
// std::cerr << "======2======= " << in_second_argument->dumpTree() << std::endl;
9697
auto set_key = PreparedSetKey::forSubquery(in_second_argument->getTreeHash());
9798
if (sets.getFuture(set_key))
9899
return;

0 commit comments

Comments
 (0)