Merge pull request #52947 from ClickHouse/analyzer-fix-with-resolve

Analyzer: fix WITH clause resolving
This commit is contained in:
robot-ch-test-poll4 2023-08-04 12:29:01 +02:00 committed by GitHub
commit 34b8872118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 171 additions and 26 deletions

View File

@ -6887,13 +6887,12 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier
scope.scope_node->formatASTForErrorMessage());
}
std::erase_if(with_nodes, [](const QueryTreeNodePtr & node)
{
auto * subquery_node = node->as<QueryNode>();
auto * union_node = node->as<UnionNode>();
return (subquery_node && subquery_node->isCTE()) || (union_node && union_node->isCTE());
});
/** WITH section can be safely removed, because WITH section only can provide aliases to query expressions
* and CTE for other sections to use.
*
* Example: WITH 1 AS constant, (x -> x + 1) AS lambda, a AS (SELECT * FROM test_table);
*/
query_node_typed.getWith().getNodes().clear();
for (auto & window_node : query_node_typed.getWindow().getNodes())
{
@ -6952,9 +6951,6 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier
scope.scope_node->formatASTForErrorMessage());
}
if (query_node_typed.hasWith())
resolveExpressionNodeList(query_node_typed.getWithNode(), scope, true /*allow_lambda_expression*/, false /*allow_table_expression*/);
if (query_node_typed.getPrewhere())
resolveExpressionNode(query_node_typed.getPrewhere(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/);
@ -7123,13 +7119,6 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier
scope.scope_node->formatASTForErrorMessage());
}
/** WITH section can be safely removed, because WITH section only can provide aliases to query expressions
* and CTE for other sections to use.
*
* Example: WITH 1 AS constant, (x -> x + 1) AS lambda, a AS (SELECT * FROM test_table);
*/
query_node_typed.getWith().getNodes().clear();
/** WINDOW section can be safely removed, because WINDOW section can only provide window definition to window functions.
*
* Example: SELECT count(*) OVER w FROM test_table WINDOW w AS (PARTITION BY id);

View File

@ -6,7 +6,7 @@ WITH
( SELECT sleep(0.0001) FROM system.one ) as a3,
( SELECT sleep(0.0001) FROM system.one ) as a4,
( SELECT sleep(0.0001) FROM system.one ) as a5
SELECT '02177_CTE_GLOBAL_ON', a5 FROM system.numbers LIMIT 100
SELECT '02177_CTE_GLOBAL_ON', a1, a2, a3, a4, a5 FROM system.numbers LIMIT 100
FORMAT Null
SETTINGS enable_global_with_statement = 1;
@ -16,17 +16,17 @@ WITH
( SELECT sleep(0.0001) FROM system.one ) as a3,
( SELECT sleep(0.0001) FROM system.one ) as a4,
( SELECT sleep(0.0001) FROM system.one ) as a5
SELECT '02177_CTE_GLOBAL_OFF', a5 FROM system.numbers LIMIT 100
SELECT '02177_CTE_GLOBAL_OFF', a1, a2, a3, a4, a5 FROM system.numbers LIMIT 100
FORMAT Null
SETTINGS enable_global_with_statement = 0;
WITH
( SELECT sleep(0.0001) FROM system.one ),
( SELECT sleep(0.0001) FROM system.one ),
( SELECT sleep(0.0001) FROM system.one ),
( SELECT sleep(0.0001) FROM system.one ),
( SELECT sleep(0.0001) FROM system.one ) as a1,
( SELECT sleep(0.0001) FROM system.one ) as a2,
( SELECT sleep(0.0001) FROM system.one ) as a3,
( SELECT sleep(0.0001) FROM system.one ) as a4,
( SELECT sleep(0.0001) FROM system.one ) as a5
SELECT '02177_CTE_NEW_ANALYZER', a5 FROM system.numbers LIMIT 100
SELECT '02177_CTE_NEW_ANALYZER', a1, a2, a3, a4, a5 FROM system.numbers LIMIT 100
FORMAT Null
SETTINGS allow_experimental_analyzer = 1;

View File

@ -32,7 +32,7 @@ OK_QUERY_JSON="
WITH (
SELECT sleepEachRow(1.0)
) AS sub
SELECT *
SELECT *, sub
FROM
(
SELECT *
@ -50,7 +50,7 @@ WITH (
SELECT *
FROM
(
SELECT *
SELECT *, sub
FROM system.one
)
FORMAT XML

View File

@ -0,0 +1,15 @@
2.5
2.5
2.5
2.5
2.5
(9399,2858)
(7159,6972)
(7456,3524)
(12685,10191)
(12598,4979)
(9824,2699)
(5655,7793)
(14410,10296)
(16211,7662)
(9349,9053)

View File

@ -0,0 +1,141 @@
set allow_experimental_analyzer = 1;
WITH
-- Input
44100 AS sample_frequency
, number AS tick
, tick / sample_frequency AS time
-- Delay
, (time, wave, delay_, decay, count) -> arraySum(n1 -> wave(time - delay_ * n1), range(count)) AS delay
, delay(time, (time -> 0.5), 0.2, 0.5, 5) AS kick
SELECT
kick
FROM system.numbers
LIMIT 5;
WITH
-- Input
44100 AS sample_frequency
, number AS tick
, tick / sample_frequency AS time
-- Output control
, 1 AS master_volume
, level -> least(1.0, greatest(-1.0, level)) AS clamp
, level -> (clamp(level) * 0x7FFF * master_volume)::Int16 AS output
, x -> (x, x) AS mono
-- Basic waves
, time -> sin(time * 2 * pi()) AS sine_wave
, time -> time::UInt64 % 2 * 2 - 1 AS square_wave
, time -> (time - floor(time)) * 2 - 1 AS sawtooth_wave
, time -> abs(sawtooth_wave(time)) * 2 - 1 AS triangle_wave
-- Helpers
, (from, to, wave, time) -> from + ((wave(time) + 1) / 2) * (to - from) AS lfo
, (from, to, steps, time) -> from + floor((time - floor(time)) * steps) / steps * (to - from) AS step_lfo
, (from, to, steps, time) -> exp(step_lfo(log(from), log(to), steps, time)) AS exp_step_lfo
-- Noise
, time -> cityHash64(time) / 0xFFFFFFFFFFFFFFFF AS uniform_noise
, time -> erf(uniform_noise(time)) AS white_noise
, time -> cityHash64(time) % 2 ? 1 : -1 AS bernoulli_noise
-- Distortion
, (x, amount) -> clamp(x * amount) AS clipping
, (x, amount) -> clamp(x > 0 ? pow(x, amount) : -pow(-x, amount)) AS power_distortion
, (x, amount) -> round(x * exp2(amount)) / exp2(amount) AS bitcrush
, (time, sample_frequency) -> round(time * sample_frequency) / sample_frequency AS desample
, (time, wave, amount) -> (time - floor(time) < (1 - amount)) ? wave(time * (1 - amount)) : 0 AS thin
, (time, wave, amount) -> wave(floor(time) + pow(time - floor(time), amount)) AS skew
-- Combining
, (a, b, weight) -> a * (1 - weight) + b * weight AS combine
-- Envelopes
, (time, offset, attack, hold, release) ->
time < offset ? 0
: (time < offset + attack ? ((time - offset) / attack)
: (time < offset + attack + hold ? 1
: (time < offset + attack + hold + release ? (offset + attack + hold + release - time) / release
: 0))) AS envelope
, (bpm, time, offset, attack, hold, release) ->
envelope(
time * (bpm / 60) - floor(time * (bpm / 60)),
offset,
attack,
hold,
release) AS running_envelope
-- Sequencers
, (sequence, time) -> sequence[1 + time::UInt64 % length(sequence)] AS sequencer
-- Delay
, (time, wave, delay, decay, count) -> arraySum(n -> wave(time - delay * n) * pow(decay, n), range(count)) AS delay
, delay(time, (time -> power_distortion(sine_wave(time * 80 + sine_wave(time * 2)), lfo(0.5, 1, sine_wave, time / 16))
* running_envelope(60, time, 0, 0.0, 0.01, 0.1)),
0.2, 0.5, 5) AS kick
SELECT
(output(
kick +
delay(time, (time ->
power_distortion(
sine_wave(time * 50 + 1 * sine_wave(time * 100 + 1/4))
* running_envelope(60, time, 0, 0.01, 0.01, 0.1),
lfo(1, 0.75, triangle_wave, time / 8))),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, time / 7)
+ delay(time, (time ->
power_distortion(
sine_wave(time * sequencer([50, 100, 200, 400], time / 2) + 1 * sine_wave(time * sequencer([50, 100, 200], time / 4) + 1/4))
* running_envelope(60, time, 0.5, 0.01, 0.01, 0.1),
lfo(1, 0.75, triangle_wave, time / 8))),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, 16 + time / 11)
+ delay(time, (time ->
white_noise(time) * running_envelope(60, time, 0.75, 0.01, 0.01, 0.1)),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, 24 + time / 13)
+ sine_wave(time * 100 + 1 * sine_wave(time * 10 + 1/4))
* running_envelope(120, time, 0, 0.01, 0.01, 0.1)
),
output(
kick +
delay(time + 0.01, (time ->
power_distortion(
sine_wave(time * 50 + 1 * sine_wave(time * 100 + 1/4))
* running_envelope(60, time, 0, 0.01, 0.01, 0.1),
lfo(1, 0.75, triangle_wave, time / 8))),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, time / 7)
+ delay(time - 0.01, (time ->
power_distortion(
sine_wave(time * sequencer([50, 100, 200, 400], time / 2) + 1 * sine_wave(time * sequencer([50, 100, 200], time / 4) + 1/4))
* running_envelope(60, time, 0.5, 0.01, 0.01, 0.1),
lfo(1, 0.75, triangle_wave, time / 8))),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, 16 + time / 11)
+ delay(time + 0.005, (time ->
white_noise(time) * running_envelope(60, time, 0.75, 0.01, 0.01, 0.1)),
0.2, 0.5, 10)
* lfo(0.5, 1, triangle_wave, 24 + time / 13)
))
FROM system.numbers
LIMIT 10;