Skip to content

Commit

Permalink
add test case and support constant expression
Browse files Browse the repository at this point in the history
  • Loading branch information
liuneng1994 committed Mar 22, 2024
1 parent ada4384 commit 6a550bc
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Analyzer/Passes/ConvertInToEqualPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ConvertInToEqualPassVisitor : public InDepthQueryTreeVisitorWithContext<Co
|| constant_node->getValue().getType() == Field::Types::Which::Array)
return ;
// x IN null not equivalent to x = null
if (constant_node->hasSourceExpression() || constant_node->getValue().isNull())
if (constant_node->getValue().isNull())
return ;
auto result_func_name = MAPPING.at(func_node->getFunctionName());
auto equal = std::make_shared<FunctionNode>(result_func_name);
Expand Down
138 changes: 135 additions & 3 deletions tests/queries/0_stateless/03013_optimize_in_to_equal.reference
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ QUERY id: 0
CONSTANT id: 8, constant_value: \'a\', constant_value_type: String
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: equals, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: \'A\', constant_value_type: String
EXPRESSION
FUNCTION id: 9, function_name: upper, function_type: ordinary, result_type: String
ARGUMENTS
LIST id: 10, nodes: 1
CONSTANT id: 11, constant_value: \'a\', constant_value_type: String
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
Expand All @@ -40,6 +63,24 @@ QUERY id: 0
CONSTANT id: 8, constant_value: Tuple_(\'a\', \'b\'), constant_value_type: Tuple(String, String)
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: in, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: Array_[\'a\', \'b\'], constant_value_type: Array(String)
SETTINGS allow_experimental_analyzer=1
-------------------
b 2
c 3
-------------------
Expand All @@ -61,6 +102,97 @@ QUERY id: 0
CONSTANT id: 8, constant_value: \'a\', constant_value_type: String
SETTINGS allow_experimental_analyzer=1
-------------------
a 1
b 2
c 3
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: notEquals, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: \'A\', constant_value_type: String
EXPRESSION
FUNCTION id: 9, function_name: upper, function_type: ordinary, result_type: String
ARGUMENTS
LIST id: 10, nodes: 1
CONSTANT id: 11, constant_value: \'a\', constant_value_type: String
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: notIn, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: Tuple_(\'a\', \'b\'), constant_value_type: Tuple(String, String)
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: notIn, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: Array_[\'a\', \'b\'], constant_value_type: Array(String)
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: notIn, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: NULL, constant_value_type: Nullable(Nothing)
SETTINGS allow_experimental_analyzer=1
-------------------
QUERY id: 0
PROJECTION COLUMNS
x String
y Int32
PROJECTION
LIST id: 1, nodes: 2
COLUMN id: 2, column_name: x, result_type: String, source_id: 3
COLUMN id: 4, column_name: y, result_type: Int32, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test
WHERE
FUNCTION id: 5, function_name: in, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 6, nodes: 2
COLUMN id: 7, column_name: x, result_type: String, source_id: 3
CONSTANT id: 8, constant_value: NULL, constant_value_type: Nullable(Nothing)
SETTINGS allow_experimental_analyzer=1
14 changes: 13 additions & 1 deletion tests/queries/0_stateless/03013_optimize_in_to_equal.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ select x in Null from test;
select '-------------------';
explain query tree select * from test where x in ('a') SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x in (upper('a')) SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x in ('a','b') SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x in ['a','b'] SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
select * from test where x not in ('a') SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x not in ('a') SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
select * from test where x not in (NULL) SETTINGS allow_experimental_analyzer = 1;
explain query tree select * from test where x not in (upper('a')) SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x not in ('a','b') SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x not in ['a','b'] SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x not in (NULL) SETTINGS allow_experimental_analyzer = 1;
select '-------------------';
explain query tree select * from test where x in (NULL) SETTINGS allow_experimental_analyzer = 1;

0 comments on commit 6a550bc

Please sign in to comment.