123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- # Copyright Materialize, Inc. and contributors. All rights reserved.
- #
- # Use of this software is governed by the Business Source License
- # included in the LICENSE file at the root of this repository.
- #
- # As of the Change Date specified in that file, in accordance with
- # the Business Source License, use of this software will be governed
- # by the Apache License, Version 2.0.
- simple conn=mz_system,user=mz_system
- ALTER SYSTEM SET unsafe_enable_table_keys = true
- ----
- COMPLETE 0
- query I
- select (select sum(1) from (select c) group by c) from (select 1 as c)
- ----
- 1
- query T
- select (select jsonb_agg(1) from (select c) group by c) from (select 1 as c)
- ----
- [1]
- statement ok
- CREATE TABLE x (f0 int4, f1 string);
- statement ok
- CREATE TABLE y (f0 int2, f1 string primary key);
- # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
- # Then, `ReduceElision` eliminates the Distinct from the 2nd join input.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
- SELECT DISTINCT *
- FROM x, y
- WHERE x.f1 = y.f1
- ----
- Explained Query:
- Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "([0, 1])" }
- Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "([0, 1])" }
- implementation
- %1:y[#1]UK » %0[#1]K
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([0, 1])" }
- Distinct project=[#0{f0}, #1{f1}] // { arity: 2, keys: "([0, 1])" }
- Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
- ReadStorage materialize.public.x // { arity: 2, keys: "()" }
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
- ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
- Source materialize.public.x
- filter=((#1{f1}) IS NOT NULL)
- Source materialize.public.y
- Target cluster: quickstart
- EOF
- ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
- # `ReductionPushdown` pushes the Distinct from after the Join into both join inputs.
- # Then, `ReduceElision` eliminates the Distinct from the 2nd join input.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
- WITH MUTUALLY RECURSIVE
- c0(f0 int4, f1 string, f2 int2, f3 string) AS (
- (SELECT DISTINCT *
- FROM x, y
- WHERE x.f1 = y.f1)
- UNION ALL
- (SELECT *
- FROM c0)
- )
- SELECT * FROM c0;
- ----
- Explained Query:
- With Mutually Recursive
- cte l0 =
- Union // { arity: 4, keys: "()" }
- Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "([0, 1])" }
- Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "([0, 1])" }
- implementation
- %1:y[#1]UK » %0[#1]K
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([0, 1])" }
- Distinct project=[#0{f0}, #1{f1}] // { arity: 2, keys: "([0, 1])" }
- Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
- ReadStorage materialize.public.x // { arity: 2, keys: "()" }
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
- ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
- Get l0 // { arity: 4, keys: "()" }
- Return // { arity: 4, keys: "()" }
- Get l0 // { arity: 4, keys: "()" }
- Source materialize.public.x
- filter=((#1{f1}) IS NOT NULL)
- Source materialize.public.y
- Target cluster: quickstart
- EOF
- # Similar to the previous test, but
- # - Has UNION instead of UNION ALL. This means that there is a Distinct at the root of l0.
- # - The second input of the UNION has a DISTINCT. This should be eliminated later by `ReduceElision` after we make the
- # unique key inference smarter for `LetRec`.
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH(arity, join implementations, keys, humanized expressions) AS VERBOSE TEXT FOR
- WITH MUTUALLY RECURSIVE
- c0(f0 int4, f1 string, f2 int2, f3 string) AS (
- (SELECT DISTINCT *
- FROM x, y
- WHERE x.f1 = y.f1)
- UNION
- (SELECT DISTINCT *
- FROM c0)
- )
- SELECT * FROM c0;
- ----
- Explained Query:
- With Mutually Recursive
- cte l0 =
- Distinct project=[#0{f0}..=#3{f1}] // { arity: 4, keys: "([0, 1, 2, 3])" }
- Union // { arity: 4, keys: "()" }
- Project (#0{f0}..=#2{f0}, #1{f1}) // { arity: 4, keys: "()" }
- Join on=(#1{f1} = #3{f1}) type=differential // { arity: 4, keys: "()" }
- implementation
- %1:y[#1{f1}]UK » %0:x[#1{f1}]K
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "()" }
- Filter (#1{f1}) IS NOT NULL // { arity: 2, keys: "()" }
- ReadStorage materialize.public.x // { arity: 2, keys: "()" }
- ArrangeBy keys=[[#1{f1}]] // { arity: 2, keys: "([1])" }
- ReadStorage materialize.public.y // { arity: 2, keys: "([1])" }
- Get l0 // { arity: 4, keys: "([0, 1, 2, 3])" }
- Return // { arity: 4, keys: "([0, 1, 2, 3])" }
- Get l0 // { arity: 4, keys: "([0, 1, 2, 3])" }
- Source materialize.public.x
- filter=((#1{f1}) IS NOT NULL)
- Source materialize.public.y
- Target cluster: quickstart
- EOF
|