2021-01-19 13:35:26 +00:00
#!/usr/bin/env bash
CURDIR = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
# shellcheck source=../shell_config.sh
. " $CURDIR " /../shell_config.sh
2021-02-26 16:29:56 +00:00
$CLICKHOUSE_CLIENT -q "select x + 1 from (select y + 2 as x from (select dummy + 3 as y)) settings query_plan_max_optimizations_to_apply = 1" 2>& 1 |
grep -o "Too many optimizations applied to query plan"
2021-02-12 11:31:14 +00:00
echo "> sipHash should be calculated after filtration"
2021-01-19 13:35:26 +00:00
$CLICKHOUSE_CLIENT -q "explain actions = 1 select sum(x), sum(y) from (select sipHash64(number) as x, bitAnd(number, 1024) as y from numbers_mt(1000000000) limit 1000000000) where y = 0" | grep -o "FUNCTION sipHash64\|Filter column: equals"
2021-02-12 11:31:14 +00:00
echo "> sorting steps should know about limit"
2021-10-19 08:11:54 +00:00
$CLICKHOUSE_CLIENT -q "explain actions = 1 select number from (select number from numbers(500000000) order by -number) limit 10" | grep -o "Sorting\|Limit 10"
2021-02-12 11:31:14 +00:00
echo "-- filter push down --"
echo "> filter should be pushed down after aggregating"
$CLICKHOUSE_CLIENT -q "
explain select * from ( select sum( x) , y from (
select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0
settings enable_optimize_predicate_expression = 0" | grep -o " Aggregating\| Filter"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y from ( select sum( x) as s, y from (
select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 order by s, y
settings enable_optimize_predicate_expression = 0"
2021-02-12 11:31:14 +00:00
echo "> filter should be pushed down after aggregating, column after aggregation is const"
2023-05-17 19:09:13 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select s, y, y != 0 from ( select sum( x) as s, y from (
2021-02-12 11:31:14 +00:00
select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0
settings enable_optimize_predicate_expression = 0" | grep -o " Aggregating\| Filter\| COLUMN Const( UInt8) -> notEquals( y, 0) "
2023-05-17 19:09:13 +00:00
echo "> (analyzer) filter should be pushed down after aggregating, column after aggregation is const"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select s, y, y != 0 from ( select sum( x) as s, y from (
select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0
2023-08-24 20:32:40 +00:00
settings enable_optimize_predicate_expression = 0" | grep -o " Aggregating\| Filter\| COLUMN Const( UInt8) -> notEquals( __table1.y, 0_UInt8) "
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y, y != 0 from ( select sum( x) as s, y from (
select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 order by s, y, y != 0
settings enable_optimize_predicate_expression = 0"
2021-02-12 11:31:14 +00:00
echo "> one condition of filter should be pushed down after aggregating, other condition is aliased"
2023-05-17 19:09:13 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select s, y from (
2021-02-12 11:31:14 +00:00
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(y, 0)\|ALIAS notEquals(s, 4) :: 4 -> and(notEquals(y, 0), notEquals(s, 4)) UInt8 : 2"
2023-05-17 19:09:13 +00:00
echo "> (analyzer) one condition of filter should be pushed down after aggregating, other condition is aliased"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(__table1.y, 0_UInt8)\|ALIAS notEquals(__table1.s, 4_UInt8) :: 1 -> and(notEquals(__table1.y, 0_UInt8), notEquals(__table1.s, 4_UInt8))"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 4 order by s, y
settings enable_optimize_predicate_expression = 0"
2021-02-12 11:31:14 +00:00
echo "> one condition of filter should be pushed down after aggregating, other condition is casted"
2023-05-17 19:09:13 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select s, y from (
2021-02-12 11:31:14 +00:00
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(y, 0)\|FUNCTION and(minus(s, 4) :: 5, 1 :: 3) -> and(notEquals(y, 0), minus(s, 4))"
2023-05-17 19:09:13 +00:00
echo "> (analyzer) one condition of filter should be pushed down after aggregating, other condition is casted"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(__table1.y, 0_UInt8)\|FUNCTION and(minus(__table1.s, 4_UInt8) :: 1, 1 :: 3) -> and(notEquals(__table1.y, 0_UInt8), minus(__table1.s, 4_UInt8))"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 4 order by s, y
settings enable_optimize_predicate_expression = 0"
2021-02-12 11:31:14 +00:00
echo "> one condition of filter should be pushed down after aggregating, other two conditions are ANDed"
2023-05-17 19:09:13 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 --convert_query_to_cnf= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select s, y from (
2021-02-12 11:31:14 +00:00
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 8 and s - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(y, 0)\|FUNCTION and(minus(s, 8) :: 5, minus(s, 4) :: 2) -> and(notEquals(y, 0), minus(s, 8), minus(s, 4))"
2023-05-17 19:09:13 +00:00
echo "> (analyzer) one condition of filter should be pushed down after aggregating, other two conditions are ANDed"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 --convert_query_to_cnf= 0 -q "
explain actions = 1 select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 8 and s - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: notEquals(__table1.y, 0_UInt8)\|FUNCTION and(minus(__table1.s, 8_UInt8) :: 1, minus(__table1.s, 4_UInt8) :: 2) -> and(notEquals(__table1.y, 0_UInt8), minus(__table1.s, 8_UInt8), minus(__table1.s, 4_UInt8))"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s - 8 and s - 4 order by s, y
settings enable_optimize_predicate_expression = 0"
2021-02-12 11:31:14 +00:00
echo "> two conditions of filter should be pushed down after aggregating and ANDed, one condition is aliased"
2023-05-17 19:09:13 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 --convert_query_to_cnf= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select s, y from (
2021-02-12 11:31:14 +00:00
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 8 and y - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: and(notEquals(y, 0), minus(y, 4))\|ALIAS notEquals(s, 8) :: 4 -> and(notEquals(y, 0), notEquals(s, 8), minus(y, 4))"
2023-05-17 19:09:13 +00:00
echo "> (analyzer) two conditions of filter should be pushed down after aggregating and ANDed, one condition is aliased"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 --convert_query_to_cnf= 0 -q "
explain actions = 1 select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 8 and y - 4
settings enable_optimize_predicate_expression = 0" |
2024-06-04 16:11:45 +00:00
grep -o "Aggregating\|Filter column\|Filter column: and(notEquals(__table1.y, 0_UInt8), minus(__table1.y, 4_UInt8))\|ALIAS notEquals(__table1.s, 8_UInt8) :: 1 -> and(notEquals(__table1.y, 0_UInt8), notEquals(__table1.s, 8_UInt8), minus(__table1.y, 4_UInt8))"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select s, y from (
select sum( x) as s, y from ( select number as x, number + 1 as y from numbers( 10) ) group by y
) where y != 0 and s != 8 and y - 4 order by s, y
settings enable_optimize_predicate_expression = 0"
2021-02-12 16:06:18 +00:00
echo "> filter is split, one part is filtered before ARRAY JOIN"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-12 16:06:18 +00:00
explain actions = 1 select x, y from (
select range( number) as x, number + 1 as y from numbers( 3)
) array join x where y != 2 and x != 0" |
2021-02-17 16:54:37 +00:00
grep -o "Filter column: and(notEquals(y, 2), notEquals(x, 0))\|ARRAY JOIN x\|Filter column: notEquals(y, 2)"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) filter is split, one part is filtered before ARRAY JOIN"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select x, y from (
select range( number) as x, number + 1 as y from numbers( 3)
) array join x where y != 2 and x != 0" |
2023-08-24 20:32:40 +00:00
grep -o "Filter column: and(notEquals(__table2.y, 2_UInt8), notEquals(__table1.x, 0_UInt8))\|ARRAY JOIN __table1.x\|Filter column: notEquals(__table2.y, 2_UInt8)"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select x, y from (
select range( number) as x, number + 1 as y from numbers( 3)
) array join x where y != 2 and x != 0 order by x, y"
# echo "> filter is split, one part is filtered before Aggregating and Cube"
# $CLICKHOUSE_CLIENT -q "
# explain actions = 1 select * from (
# select sum(x) as s, x, y from (select number as x, number + 1 as y from numbers(10)) group by x, y with cube
# ) where y != 0 and s != 4
# settings enable_optimize_predicate_expression=0" |
# grep -o "Cube\|Aggregating\|Filter column: notEquals(y, 0)"
# $CLICKHOUSE_CLIENT -q "
# select s, x, y from (
# select sum(x) as s, x, y from (select number as x, number + 1 as y from numbers(10)) group by x, y with cube
# ) where y != 0 and s != 4 order by s, x, y
# settings enable_optimize_predicate_expression=0"
echo "> filter is pushed down before Distinct"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select x, y from (
select distinct x, y from ( select number % 2 as x, number % 3 as y from numbers( 10) )
) where y != 2
settings enable_optimize_predicate_expression = 0" |
grep -o "Distinct\|Filter column: notEquals(y, 2)"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) filter is pushed down before Distinct"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select x, y from (
select distinct x, y from ( select number % 2 as x, number % 3 as y from numbers( 10) )
) where y != 2
settings enable_optimize_predicate_expression = 0" |
2023-08-24 20:32:40 +00:00
grep -o "Distinct\|Filter column: notEquals(__table1.y, 2_UInt8)"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select x, y from (
select distinct x, y from ( select number % 2 as x, number % 3 as y from numbers( 10) )
) where y != 2 order by x, y
settings enable_optimize_predicate_expression = 0"
echo "> filter is pushed down before sorting steps"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 --convert_query_to_cnf= 0 -q "
2021-02-17 16:54:37 +00:00
explain actions = 1 select x, y from (
select number % 2 as x, number % 3 as y from numbers( 6) order by y desc
) where x != 0 and y != 0
settings enable_optimize_predicate_expression = 0" |
2021-10-19 08:11:54 +00:00
grep -o "Sorting\|Filter column: and(notEquals(x, 0), notEquals(y, 0))"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) filter is pushed down before sorting steps"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 --convert_query_to_cnf= 0 -q "
explain actions = 1 select x, y from (
select number % 2 as x, number % 3 as y from numbers( 6) order by y desc
) where x != 0 and y != 0
settings enable_optimize_predicate_expression = 0" |
2023-08-24 20:32:40 +00:00
grep -o "Sorting\|Filter column: and(notEquals(__table1.x, 0_UInt8), notEquals(__table1.y, 0_UInt8))"
2021-02-17 16:54:37 +00:00
$CLICKHOUSE_CLIENT -q "
select x, y from (
select number % 2 as x, number % 3 as y from numbers( 6) order by y desc
) where x != 0 and y != 0
settings enable_optimize_predicate_expression = 0"
2021-02-18 13:13:09 +00:00
echo "> filter is pushed down before TOTALS HAVING and aggregating"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-02-18 13:13:09 +00:00
explain actions = 1 select * from (
select y, sum( x) from ( select number as x, number % 4 as y from numbers( 10) ) group by y with totals
) where y != 2
settings enable_optimize_predicate_expression = 0" |
grep -o "TotalsHaving\|Aggregating\|Filter column: notEquals(y, 2)"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) filter is pushed down before TOTALS HAVING and aggregating"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1 select * from (
select y, sum( x) from ( select number as x, number % 4 as y from numbers( 10) ) group by y with totals
) where y != 2
settings enable_optimize_predicate_expression = 0" |
2023-08-24 20:32:40 +00:00
grep -o "TotalsHaving\|Aggregating\|Filter column: notEquals(__table1.y, 2_UInt8)"
2021-02-18 13:13:09 +00:00
$CLICKHOUSE_CLIENT -q "
select * from (
select y, sum( x) from ( select number as x, number % 4 as y from numbers( 10) ) group by y with totals
2021-02-26 16:29:56 +00:00
) where y != 2"
2021-03-25 18:11:54 +00:00
echo "> filter is pushed down before CreatingSets"
$CLICKHOUSE_CLIENT -q "
explain select number from (
select number from numbers( 5) where number in ( select 1 + number from numbers( 3) )
) where number != 2 settings enable_optimize_predicate_expression = 0" |
grep -o "CreatingSets\|Filter"
$CLICKHOUSE_CLIENT -q "
select number from (
select number from numbers( 5) where number in ( select 1 + number from numbers( 3) )
) where number != 2 settings enable_optimize_predicate_expression = 0"
echo "> one condition of filter is pushed down before LEFT JOIN"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-03-25 18:11:54 +00:00
explain actions = 1
select number as a, r.b from numbers( 4) as l any left join (
select number + 2 as b from numbers( 3)
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0" |
grep -o "Join\|Filter column: notEquals(number, 1)"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) one condition of filter is pushed down before LEFT JOIN"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1
select number as a, r.b from numbers( 4) as l any left join (
select number + 2 as b from numbers( 3)
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0" |
2023-08-24 20:32:40 +00:00
grep -o "Join\|Filter column: notEquals(__table1.number, 1_UInt8)"
2021-03-25 18:11:54 +00:00
$CLICKHOUSE_CLIENT -q "
select number as a, r.b from numbers( 4) as l any left join (
select number + 2 as b from numbers( 3)
2022-06-13 14:29:52 +00:00
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0" | sort
2021-03-25 18:11:54 +00:00
echo "> one condition of filter is pushed down before INNER JOIN"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
2021-03-25 18:11:54 +00:00
explain actions = 1
select number as a, r.b from numbers( 4) as l any inner join (
select number + 2 as b from numbers( 3)
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0" |
2024-04-09 11:39:08 +00:00
grep -o "Join\|Filter column: and(notEquals(number, 1), notEquals(number, 2))\|Filter column: and(notEquals(b, 2), notEquals(b, 1))"
2023-05-22 11:24:29 +00:00
echo "> (analyzer) one condition of filter is pushed down before INNER JOIN"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
explain actions = 1
select number as a, r.b from numbers( 4) as l any inner join (
select number + 2 as b from numbers( 3)
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0" |
2024-04-09 11:39:08 +00:00
grep -o "Join\|Filter column: and(notEquals(__table1.number, 1_UInt8), notEquals(__table1.number, 2_UInt8))\|Filter column: and(notEquals(__table2.b, 2_UInt8), notEquals(__table2.b, 1_UInt8))"
2021-03-25 18:11:54 +00:00
$CLICKHOUSE_CLIENT -q "
select number as a, r.b from numbers( 4) as l any inner join (
select number + 2 as b from numbers( 3)
) as r on a = r.b where a != 1 and b != 2 settings enable_optimize_predicate_expression = 0"
echo "> filter is pushed down before UNION"
$CLICKHOUSE_CLIENT -q "
explain select a, b from (
select number + 1 as a, number + 2 as b from numbers( 2) union all select number + 1 as b, number + 2 as a from numbers( 2)
) where a != 1 settings enable_optimize_predicate_expression = 0" |
grep -o "Union\|Filter"
$CLICKHOUSE_CLIENT -q "
select a, b from (
select number + 1 as a, number + 2 as b from numbers( 2) union all select number + 1 as b, number + 2 as a from numbers( 2)
) where a != 1 settings enable_optimize_predicate_expression = 0"
2022-03-25 15:18:24 +00:00
echo "> function calculation should be done after sorting and limit (if possible)"
2022-03-25 16:20:29 +00:00
echo "> Expression should be divided into two subexpressions and only one of them should be moved after Sorting"
2023-05-22 11:24:29 +00:00
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 0 -q "
explain actions = 1 select number as n, sipHash64( n) from numbers( 100) order by number + 1 limit 5" |
sed 's/^ *//g' | grep -o "^ *\(Expression (.*Before ORDER BY.*)\|Sorting\|FUNCTION \w\+\)"
echo "> (analyzer) function calculation should be done after sorting and limit (if possible)"
echo "> Expression should be divided into two subexpressions and only one of them should be moved after Sorting"
$CLICKHOUSE_CLIENT --allow_experimental_analyzer= 1 -q "
2022-03-29 19:16:05 +00:00
explain actions = 1 select number as n, sipHash64( n) from numbers( 100) order by number + 1 limit 5" |
2022-04-25 18:23:24 +00:00
sed 's/^ *//g' | grep -o "^ *\(Expression (.*Before ORDER BY.*)\|Sorting\|FUNCTION \w\+\)"
2022-03-29 10:55:48 +00:00
echo "> this query should be executed without throwing an exception"
$CLICKHOUSE_CLIENT -q "
select throwIf( number = 5) from ( select * from numbers( 10) ) order by number limit 1"