Merge pull request #46653 from aiven-sal/aiven-sal/and_opt_pr

ActionsDAG: do not change result of and() during optimization
This commit is contained in:
Alexey Milovidov 2023-02-26 02:56:35 +03:00 committed by GitHub
commit 3a3a2f352c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 147 additions and 1 deletions

View File

@ -1946,6 +1946,9 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown(
}
auto conjunction = getConjunctionNodes(predicate, allowed_nodes);
if (conjunction.rejected.size() == 1 && WhichDataType{conjunction.rejected.front()->result_type}.isFloat())
return nullptr;
auto actions = cloneActionsForConjunction(conjunction.allowed, all_inputs);
if (!actions)
return nullptr;
@ -2011,10 +2014,12 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown(
node.children.swap(new_children);
*predicate = std::move(node);
}
else
else if (!WhichDataType{new_children.front()->result_type}.isFloat())
{
/// If type is different, cast column.
/// This case is possible, cause AND can use any numeric type as argument.
/// But casting floats to UInt8 or Bool produces different results.
/// so we can't apply this optimization to them.
Node node;
node.type = ActionType::COLUMN;
node.result_name = predicate->result_type->getName();
@ -2036,8 +2041,20 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown(
else
{
/// Predicate is function AND, which still have more then one argument.
/// Or there is only one argument that is a float and we can't just
/// remove the AND.
/// Just update children and rebuild it.
predicate->children.swap(new_children);
if (WhichDataType{predicate->children.front()->result_type}.isFloat())
{
Node node;
node.type = ActionType::COLUMN;
node.result_name = "1";
node.column = DataTypeUInt8().createColumnConst(0, 1u);
node.result_type = std::make_shared<DataTypeUInt8>();
const auto * const_col = &nodes.emplace_back(std::move(node));
predicate->children.emplace_back(const_col);
}
auto arguments = prepareFunctionArguments(predicate->children);
FunctionOverloadResolverPtr func_builder_and = std::make_unique<FunctionToOverloadResolverAdaptor>(std::make_shared<FunctionAnd>());

View File

@ -0,0 +1,23 @@
true
=====
true
=====
true
=====
true
=====
=====
1
=====
=====
allow_experimental_analyzer
true
#45440
2086579505 0 1 0 0
-542998757 -542998757 1 0 0
=
2086579505 0 1 0 0
-542998757 -542998757 1 0 0
=
2086579505 0 1 0 0
-542998757 -542998757 1 0 0

View File

@ -0,0 +1,106 @@
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING 1 AND sin(sum(number))
SETTINGS enable_optimize_predicate_expression = 0;
SELECT '=====';
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING 1 AND sin(1)
SETTINGS enable_optimize_predicate_expression = 0;
SELECT '=====';
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING x AND sin(sum(number))
SETTINGS enable_optimize_predicate_expression = 1;
SELECT '=====';
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING 1 AND sin(sum(number))
SETTINGS enable_optimize_predicate_expression = 0;
SELECT '=====';
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING 1 AND sin(sum(number))
SETTINGS enable_optimize_predicate_expression = 1; -- { serverError 59 }
SELECT '=====';
SELECT 1 and sin(1);
SELECT '=====';
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING x AND sin(1)
SETTINGS enable_optimize_predicate_expression = 0; -- { serverError 59 }
SELECT '=====';
SELECT 'allow_experimental_analyzer';
SET allow_experimental_analyzer = 1;
SELECT toBool(sin(SUM(number))) AS x
FROM
(
SELECT 1 AS number
)
GROUP BY number
HAVING 1 AND sin(sum(number))
SETTINGS enable_optimize_predicate_expression = 1;
select '#45440';
DROP TABLE IF EXISTS t2;
CREATE TABLE t2(c0 Int32) ENGINE = MergeTree ORDER BY c0;
INSERT INTO t2 VALUES (928386547), (1541944097), (2086579505), (1990427322), (-542998757), (390253678), (554855248), (203290629), (1504693323);
SELECT
MAX(left.c0),
min2(left.c0, -(-left.c0) * (radians(left.c0) - radians(left.c0))) AS g,
(((-1925024212 IS NOT NULL) IS NOT NULL) != radians(tan(1216286224))) AND cos(lcm(MAX(left.c0), -1966575216) OR (MAX(left.c0) * 1180517420)) AS h,
NOT h,
h IS NULL
FROM t2 AS left
GROUP BY g;
select '=';
SELECT MAX(left.c0), min2(left.c0, -(-left.c0) * (radians(left.c0) - radians(left.c0))) as g, (((-1925024212 IS NOT NULL) IS NOT NULL) != radians(tan(1216286224))) AND cos(lcm(MAX(left.c0), -1966575216) OR (MAX(left.c0) * 1180517420)) as h, not h, h is null
FROM t2 AS left
GROUP BY g HAVING h SETTINGS enable_optimize_predicate_expression = 0;
select '=';
SELECT MAX(left.c0), min2(left.c0, -(-left.c0) * (radians(left.c0) - radians(left.c0))) as g, (((-1925024212 IS NOT NULL) IS NOT NULL) != radians(tan(1216286224))) AND cos(lcm(MAX(left.c0), -1966575216) OR (MAX(left.c0) * 1180517420)) as h, not h, h is null
FROM t2 AS left
GROUP BY g HAVING h SETTINGS enable_optimize_predicate_expression = 1;
DROP TABLE IF EXISTS t2;