forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pass covert in single value to equal
- Loading branch information
1 parent
eb26bb2
commit a742b23
Showing
4 changed files
with
82 additions
and
0 deletions.
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,55 @@ | ||
#include <Analyzer/ColumnNode.h> | ||
#include <Analyzer/ConstantNode.h> | ||
#include <Analyzer/FunctionNode.h> | ||
#include <Analyzer/InDepthQueryTreeVisitor.h> | ||
#include <Analyzer/Passes/ConvertInToEqualsPass.h> | ||
#include <Functions/FunctionsComparison.h> | ||
#include <Functions/IFunctionAdaptors.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
using FunctionEquals = FunctionComparison<EqualsOp, NameEquals>; | ||
|
||
class ConvertInToEqualsPassVisitor : public InDepthQueryTreeVisitorWithContext<ConvertInToEqualsPassVisitor> | ||
{ | ||
public: | ||
using Base = InDepthQueryTreeVisitorWithContext<ConvertInToEqualsPassVisitor>; | ||
using Base::Base; | ||
|
||
FunctionOverloadResolverPtr createInternalFunctionEqualOverloadResolver() | ||
{ | ||
return std::make_unique<FunctionToOverloadResolverAdaptor>(std::make_shared<FunctionEquals>(getContext()->getSettings().decimal_check_overflow)); | ||
} | ||
|
||
void enterImpl(QueryTreeNodePtr & node) | ||
{ | ||
if (!getSettings().optimize_in_to_equal) | ||
return; | ||
auto * func_node = node->as<FunctionNode>(); | ||
if (!func_node || func_node->getFunctionName() != "in" || 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 ; | ||
if (constant_node->getValue().getType() == Field::Types::Which::Tuple) | ||
return; | ||
auto const_value = std::make_shared<ConstantNode>(constant_node->getValue()); | ||
|
||
auto equal_resolver = createInternalFunctionEqualOverloadResolver(); | ||
auto equal = std::make_shared<FunctionNode>("equals"); | ||
QueryTreeNodes arguments{column_node->clone(), const_value}; | ||
equal->getArguments().getNodes() = std::move(arguments); | ||
equal->resolveAsFunction(equal_resolver); | ||
node = equal; | ||
} | ||
}; | ||
|
||
void ConvertInToEqualsPass::run(QueryTreeNodePtr & query_tree_node, ContextPtr context) | ||
{ | ||
ConvertInToEqualsPassVisitor 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,24 @@ | ||
#pragma once | ||
|
||
#include <Analyzer/IQueryTreePass.h> | ||
|
||
namespace DB { | ||
|
||
/** Optimize `in` to `equals` if possible. | ||
* | ||
* Example: SELECT * from test where x IN (1); | ||
* Result: SELECT * from test where x = 1; | ||
* | ||
*/ | ||
class ConvertInToEqualsPass final : public IQueryTreePass { | ||
public: | ||
String getName() override { return "ConvertInToEqualsPass"; } | ||
|
||
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