From d7cd77640d33f74161b8b59a6c2745c570231a17 Mon Sep 17 00:00:00 2001 From: zhang2014 Date: Thu, 2 Aug 2018 12:15:39 +0800 Subject: [PATCH] ISSUES-2786 fix replace asterisk with join query --- dbms/src/Interpreters/ExpressionAnalyzer.cpp | 25 ++++++++-------- .../0_stateless/00050_any_left_join.reference | 20 ++++++------- .../0_stateless/00050_any_left_join.sql | 2 +- .../00051_any_inner_join.reference | 10 +++---- .../0_stateless/00051_any_inner_join.sql | 2 +- .../0_stateless/00052_all_left_join.reference | 30 +++++++++---------- .../0_stateless/00052_all_left_join.sql | 2 +- .../00053_all_inner_join.reference | 20 ++++++------- .../0_stateless/00053_all_inner_join.sql | 2 +- .../0_stateless/00054_join_string.reference | 30 +++++++++---------- .../queries/0_stateless/00054_join_string.sql | 2 +- .../00555_right_join_excessive_rows.reference | 22 +++++++------- ...query_aggregation_column_removal.reference | 8 ++--- .../00679_replace_asterisk.reference | 2 ++ .../0_stateless/00679_replace_asterisk.sql | 2 ++ 15 files changed, 91 insertions(+), 88 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00679_replace_asterisk.reference create mode 100644 dbms/tests/queries/0_stateless/00679_replace_asterisk.sql diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 121b05c2968..5d069a339ca 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -1156,22 +1156,21 @@ void ExpressionAnalyzer::normalizeTreeImpl( { if (typeid_cast(asts[i].get())) { - ASTs all_columns; + Names all_columns_name; - if (storage) - { - /// If we select from a table, get only not MATERIALIZED, not ALIAS columns. - for (const auto & name_type : storage->getColumns().ordinary) - all_columns.emplace_back(std::make_shared(name_type.name)); - } - else - { - for (const auto & name_type : source_columns) - all_columns.emplace_back(std::make_shared(name_type.name)); - } + auto columns_name = storage ? storage->getColumns().ordinary.getNames() : source_columns.getNames(); + all_columns_name.insert(all_columns_name.begin(), columns_name.begin(), columns_name.end()); + + NameSet joined_columns; + collectJoinedColumns(joined_columns); + auto columns_from_joined_table = analyzed_join.columns_from_joined_table.getNames(); + all_columns_name.insert(all_columns_name.end(), columns_from_joined_table.begin(), columns_from_joined_table.end()); + /// HACK: The side effect of calling the `collectJoinedColumns` method, we have to reset it. + analyzed_join = AnalyzedJoin(); asts.erase(asts.begin() + i); - asts.insert(asts.begin() + i, all_columns.begin(), all_columns.end()); + for (size_t idx = 0; idx < all_columns_name.size(); idx++) + asts.insert(asts.begin() + idx + i, std::make_shared(all_columns_name[idx])); } } } diff --git a/dbms/tests/queries/0_stateless/00050_any_left_join.reference b/dbms/tests/queries/0_stateless/00050_any_left_join.reference index 463a9286abe..dc46f7e8a75 100644 --- a/dbms/tests/queries/0_stateless/00050_any_left_join.reference +++ b/dbms/tests/queries/0_stateless/00050_any_left_join.reference @@ -1,10 +1,10 @@ -0 0 -1 0 -2 1 -3 0 -4 2 -5 0 -6 3 -7 0 -8 4 -9 0 +0 0 0 +1 1 0 +2 2 1 +3 3 0 +4 4 2 +5 5 0 +6 6 3 +7 7 0 +8 8 4 +9 9 0 diff --git a/dbms/tests/queries/0_stateless/00050_any_left_join.sql b/dbms/tests/queries/0_stateless/00050_any_left_join.sql index d1f03b79e41..2440a9f93ab 100644 --- a/dbms/tests/queries/0_stateless/00050_any_left_join.sql +++ b/dbms/tests/queries/0_stateless/00050_any_left_join.sql @@ -1,4 +1,4 @@ -SELECT *, joined FROM +SELECT * FROM ( SELECT number AS k FROM system.numbers LIMIT 10 ) diff --git a/dbms/tests/queries/0_stateless/00051_any_inner_join.reference b/dbms/tests/queries/0_stateless/00051_any_inner_join.reference index 2a61b78f6ea..a7949c6a350 100644 --- a/dbms/tests/queries/0_stateless/00051_any_inner_join.reference +++ b/dbms/tests/queries/0_stateless/00051_any_inner_join.reference @@ -1,5 +1,5 @@ -0 0 -2 1 -4 2 -6 3 -8 4 +0 0 0 +2 2 1 +4 4 2 +6 6 3 +8 8 4 diff --git a/dbms/tests/queries/0_stateless/00051_any_inner_join.sql b/dbms/tests/queries/0_stateless/00051_any_inner_join.sql index 53f245fb819..a3ff3c437cf 100644 --- a/dbms/tests/queries/0_stateless/00051_any_inner_join.sql +++ b/dbms/tests/queries/0_stateless/00051_any_inner_join.sql @@ -1,4 +1,4 @@ -SELECT *, joined FROM +SELECT * FROM ( SELECT number AS k FROM system.numbers LIMIT 10 ) diff --git a/dbms/tests/queries/0_stateless/00052_all_left_join.reference b/dbms/tests/queries/0_stateless/00052_all_left_join.reference index a33465fcd1d..7d48d304488 100644 --- a/dbms/tests/queries/0_stateless/00052_all_left_join.reference +++ b/dbms/tests/queries/0_stateless/00052_all_left_join.reference @@ -1,15 +1,15 @@ -0 0 -0 1 -1 2 -1 3 -2 4 -2 5 -3 6 -3 7 -4 8 -4 9 -5 0 -6 0 -7 0 -8 0 -9 0 +0 0 0 +0 0 1 +1 1 2 +1 1 3 +2 2 4 +2 2 5 +3 3 6 +3 3 7 +4 4 8 +4 4 9 +5 5 0 +6 6 0 +7 7 0 +8 8 0 +9 9 0 diff --git a/dbms/tests/queries/0_stateless/00052_all_left_join.sql b/dbms/tests/queries/0_stateless/00052_all_left_join.sql index 8476d035f62..d7221bd2d8f 100644 --- a/dbms/tests/queries/0_stateless/00052_all_left_join.sql +++ b/dbms/tests/queries/0_stateless/00052_all_left_join.sql @@ -1,4 +1,4 @@ -SELECT *, joined FROM +SELECT * FROM ( SELECT number AS k FROM system.numbers LIMIT 10 ) diff --git a/dbms/tests/queries/0_stateless/00053_all_inner_join.reference b/dbms/tests/queries/0_stateless/00053_all_inner_join.reference index 41707378e8d..24857668974 100644 --- a/dbms/tests/queries/0_stateless/00053_all_inner_join.reference +++ b/dbms/tests/queries/0_stateless/00053_all_inner_join.reference @@ -1,10 +1,10 @@ -0 0 -0 1 -1 2 -1 3 -2 4 -2 5 -3 6 -3 7 -4 8 -4 9 +0 0 0 +0 0 1 +1 1 2 +1 1 3 +2 2 4 +2 2 5 +3 3 6 +3 3 7 +4 4 8 +4 4 9 diff --git a/dbms/tests/queries/0_stateless/00053_all_inner_join.sql b/dbms/tests/queries/0_stateless/00053_all_inner_join.sql index 565a85aa509..18a94588033 100644 --- a/dbms/tests/queries/0_stateless/00053_all_inner_join.sql +++ b/dbms/tests/queries/0_stateless/00053_all_inner_join.sql @@ -1,4 +1,4 @@ -SELECT *, joined FROM +SELECT * FROM ( SELECT number AS k FROM system.numbers LIMIT 10 ) diff --git a/dbms/tests/queries/0_stateless/00054_join_string.reference b/dbms/tests/queries/0_stateless/00054_join_string.reference index 75a0a5bc5fc..4c35b240b32 100644 --- a/dbms/tests/queries/0_stateless/00054_join_string.reference +++ b/dbms/tests/queries/0_stateless/00054_join_string.reference @@ -1,15 +1,15 @@ -A 0 -A 1 -B 2 -B 3 -C 4 -C 5 -D 6 -D 7 -E 8 -E 9 -F 0 -G 0 -H 0 -I 0 -J 0 +A A 0 +A A 1 +B B 2 +B B 3 +C C 4 +C C 5 +D D 6 +D D 7 +E E 8 +E E 9 +F F 0 +G G 0 +H H 0 +I I 0 +J J 0 diff --git a/dbms/tests/queries/0_stateless/00054_join_string.sql b/dbms/tests/queries/0_stateless/00054_join_string.sql index ecb6d4af84f..7912a08c1d6 100644 --- a/dbms/tests/queries/0_stateless/00054_join_string.sql +++ b/dbms/tests/queries/0_stateless/00054_join_string.sql @@ -1,4 +1,4 @@ -SELECT *, joined FROM +SELECT * FROM ( SELECT reinterpretAsString(number + reinterpretAsUInt8('A')) AS k FROM system.numbers LIMIT 10 ) diff --git a/dbms/tests/queries/0_stateless/00555_right_join_excessive_rows.reference b/dbms/tests/queries/0_stateless/00555_right_join_excessive_rows.reference index b033488efab..2a50aaf906b 100644 --- a/dbms/tests/queries/0_stateless/00555_right_join_excessive_rows.reference +++ b/dbms/tests/queries/0_stateless/00555_right_join_excessive_rows.reference @@ -1,11 +1,11 @@ -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 +0 0 +1 1 +2 2 +3 3 +4 4 +5 5 +6 6 +7 7 +8 8 +9 9 +10 10 diff --git a/dbms/tests/queries/0_stateless/00585_union_all_subquery_aggregation_column_removal.reference b/dbms/tests/queries/0_stateless/00585_union_all_subquery_aggregation_column_removal.reference index a0265bdb7ed..936106d559e 100644 --- a/dbms/tests/queries/0_stateless/00585_union_all_subquery_aggregation_column_removal.reference +++ b/dbms/tests/queries/0_stateless/00585_union_all_subquery_aggregation_column_removal.reference @@ -14,10 +14,10 @@ 2 facebook.com 1 google.com 2 yandex.ru -1 baidu.com -1 google.com -2 facebook.com -2 yandex.ru +1 baidu.com 1 baidu.com +1 google.com 1 google.com +2 facebook.com 2 facebook.com +2 yandex.ru 2 yandex.ru 1 1 2 diff --git a/dbms/tests/queries/0_stateless/00679_replace_asterisk.reference b/dbms/tests/queries/0_stateless/00679_replace_asterisk.reference new file mode 100644 index 00000000000..a217033ec2c --- /dev/null +++ b/dbms/tests/queries/0_stateless/00679_replace_asterisk.reference @@ -0,0 +1,2 @@ +1 2 +1 2 3 1 4 5 diff --git a/dbms/tests/queries/0_stateless/00679_replace_asterisk.sql b/dbms/tests/queries/0_stateless/00679_replace_asterisk.sql new file mode 100644 index 00000000000..7033de61984 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00679_replace_asterisk.sql @@ -0,0 +1,2 @@ +SELECT * FROM (SELECT 1 AS id, 2 AS value); +SELECT * FROM (SELECT 1 AS id, 2 AS value, 3 AS A) ANY INNER JOIN (SELECT 1 AS id, 4 AS values, 5 AS D) USING id;