New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New analyzer pass to optimize in single value #61564
Merged
yariks5s
merged 12 commits into
ClickHouse:master
from
liuneng1994:optimize_in_single_value
Mar 25, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a742b23
add pass covert in single value to equal
liuneng1994 532d72a
add test case
liuneng1994 761c612
fix style
liuneng1994 a29d4f5
fix bug
liuneng1994 862abf2
remove config
liuneng1994 45bbef6
fix error for in Null
liuneng1994 d2d5f3b
add not in
liuneng1994 ada4384
fix ut failed
liuneng1994 6a550bc
add test case and support constant expression
liuneng1994 ad21841
remove duplicated SETTINGS statement
yariks5s 5d5f073
revert previous commit
yariks5s 97bd6ec
remove duplicated SETTINGS statement
yariks5s File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <Analyzer/ColumnNode.h> | ||
#include <Analyzer/ConstantNode.h> | ||
#include <Analyzer/FunctionNode.h> | ||
#include <Analyzer/InDepthQueryTreeVisitor.h> | ||
#include <Analyzer/Passes/ConvertInToEqualPass.h> | ||
#include <Functions/equals.h> | ||
#include <Functions/notEquals.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
class ConvertInToEqualPassVisitor : public InDepthQueryTreeVisitorWithContext<ConvertInToEqualPassVisitor> | ||
{ | ||
public: | ||
using Base = InDepthQueryTreeVisitorWithContext<ConvertInToEqualPassVisitor>; | ||
using Base::Base; | ||
|
||
void enterImpl(QueryTreeNodePtr & node) | ||
{ | ||
static const std::unordered_map<String, String> MAPPING = { | ||
{"in", "equals"}, | ||
{"notIn", "notEquals"} | ||
}; | ||
auto * func_node = node->as<FunctionNode>(); | ||
if (!func_node | ||
|| !MAPPING.contains(func_node->getFunctionName()) | ||
|| func_node->getArguments().getNodes().size() != 2) | ||
return ; | ||
auto args = func_node->getArguments().getNodes(); | ||
auto * column_node = args[0]->as<ColumnNode>(); | ||
auto * constant_node = args[1]->as<ConstantNode>(); | ||
if (!column_node || !constant_node) | ||
return ; | ||
// IN multiple values is not supported | ||
if (constant_node->getValue().getType() == Field::Types::Which::Tuple | ||
|| constant_node->getValue().getType() == Field::Types::Which::Array) | ||
return ; | ||
// x IN null not equivalent to x = null | ||
if (constant_node->getValue().isNull()) | ||
return ; | ||
auto result_func_name = MAPPING.at(func_node->getFunctionName()); | ||
auto equal = std::make_shared<FunctionNode>(result_func_name); | ||
QueryTreeNodes arguments{column_node->clone(), constant_node->clone()}; | ||
equal->getArguments().getNodes() = std::move(arguments); | ||
FunctionOverloadResolverPtr resolver; | ||
bool decimal_check_overflow = getContext()->getSettingsRef().decimal_check_overflow; | ||
if (result_func_name == "equals") | ||
{ | ||
resolver = createInternalFunctionEqualOverloadResolver(decimal_check_overflow); | ||
} | ||
else | ||
{ | ||
resolver = createInternalFunctionNotEqualOverloadResolver(decimal_check_overflow); | ||
} | ||
try | ||
{ | ||
equal->resolveAsFunction(resolver); | ||
} | ||
catch (...) | ||
{ | ||
// When function resolver fails, we should not replace the function node | ||
return; | ||
} | ||
node = equal; | ||
} | ||
}; | ||
|
||
void ConvertInToEqualPass::run(QueryTreeNodePtr & query_tree_node, ContextPtr context) | ||
{ | ||
ConvertInToEqualPassVisitor visitor(std::move(context)); | ||
visitor.visit(query_tree_node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <Analyzer/IQueryTreePass.h> | ||
|
||
namespace DB | ||
{ | ||
/** Optimize `in` to `equals` if possible. | ||
* 1. convert in single value to equal | ||
* Example: SELECT * from test where x IN (1); | ||
* Result: SELECT * from test where x = 1; | ||
* | ||
* 2. convert not in single value to notEqual | ||
* Example: SELECT * from test where x NOT IN (1); | ||
* Result: SELECT * from test where x != 1; | ||
* | ||
* If value is null or tuple, do not convert. | ||
*/ | ||
class ConvertInToEqualPass final : public IQueryTreePass | ||
{ | ||
public: | ||
String getName() override { return "ConvertInToEqualPass"; } | ||
|
||
String getDescription() override { return "Convert in to equal"; } | ||
|
||
void run(QueryTreeNodePtr & query_tree_node, ContextPtr context) override; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
#include <memory> | ||
|
||
namespace DB | ||
{ | ||
|
||
class IFunctionOverloadResolver; | ||
using FunctionOverloadResolverPtr = std::shared_ptr<IFunctionOverloadResolver>; | ||
|
||
FunctionOverloadResolverPtr createInternalFunctionEqualOverloadResolver(bool decimal_check_overflow); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
#include <memory> | ||
|
||
namespace DB | ||
{ | ||
|
||
class IFunctionOverloadResolver; | ||
using FunctionOverloadResolverPtr = std::shared_ptr<IFunctionOverloadResolver>; | ||
|
||
FunctionOverloadResolverPtr createInternalFunctionNotEqualOverloadResolver(bool decimal_check_overflow); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<test> | ||
<settings> | ||
<max_insert_threads>8</max_insert_threads> | ||
<max_threads>1</max_threads> | ||
</settings> | ||
|
||
<create_query> | ||
CREATE TABLE t_nullable | ||
( | ||
key_string1 Nullable(String), | ||
key_string2 Nullable(String), | ||
key_string3 Nullable(String), | ||
key_int64_1 Nullable(Int64), | ||
key_int64_2 Nullable(Int64), | ||
key_int64_3 Nullable(Int64), | ||
key_int64_4 Nullable(Int64), | ||
key_int64_5 Nullable(Int64), | ||
m1 Int64, | ||
m2 Int64 | ||
) | ||
ENGINE = Memory | ||
</create_query> | ||
<fill_query>insert into t_nullable select ['aaaaaa','bbaaaa','ccaaaa','ddaaaa'][number % 101 + 1], ['aa','bb','cc','dd'][number % 100 + 1], ['aa','bb','cc','dd'][number % 102 + 1], number%10+1, number%10+2, number%10+3, number%10+4,number%10+5, number%6000+1, number%5000+2 from numbers_mt(30000000)</fill_query> | ||
<query>select * from t_nullable where key_string1 in ('aaaaaa') format Null SETTINGS allow_experimental_analyzer=1</query> | ||
<query>select * from t_nullable where key_string2 in ('3') format Null SETTINGS allow_experimental_analyzer=1</query> | ||
<drop_query>drop table if exists t_nullable</drop_query> | ||
|
||
</test> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of creating a
.h
files forequals
andnotEquals
? I think we can avoid this behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will also use it for other subsequent optimizations, which can be better reused through .h