topk.slt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. # This file contains tests for the TopK elision transform
  10. # and interaction of transforms with the TopK operator.
  11. simple conn=mz_system,user=mz_system
  12. ALTER SYSTEM SET unsafe_enable_table_keys = true
  13. ----
  14. COMPLETE 0
  15. statement ok
  16. CREATE TABLE test1(a int, b int, c int, d int)
  17. # check that the extra project caused by the CSE is lifted
  18. # through the TopK
  19. query T multiline
  20. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  21. SELECT avg(d), sumc, sumd FROM (
  22. SELECT a + b + c as sumc, a + b + d as sumd, d
  23. FROM test1
  24. ORDER BY d NULLS FIRST LIMIT 4
  25. )
  26. GROUP BY sumc, sumd
  27. ----
  28. Explained Query:
  29. Project (#4, #0, #1) // { arity: 3 }
  30. Map ((bigint_to_numeric(#2{sum_d}) / bigint_to_numeric(case when (#3{count_d} = 0) then null else #3{count_d} end))) // { arity: 5 }
  31. Reduce group_by=[#1, #2] aggregates=[sum(#0{d}), count(#0{d})] // { arity: 4 }
  32. TopK order_by=[#0{d} asc nulls_first] limit=4 // { arity: 3 }
  33. Project (#3{d}, #5, #6) // { arity: 3 }
  34. Map ((#0{a} + #1{b}), (#4 + #2{c}), (#4 + #3{d})) // { arity: 7 }
  35. ReadStorage materialize.public.test1 // { arity: 4 }
  36. Source materialize.public.test1
  37. Target cluster: quickstart
  38. EOF
  39. # check that TopK elision is recursive
  40. statement ok
  41. CREATE VIEW plan_test1 AS
  42. SELECT avg(d), sumc, sumd FROM (
  43. SELECT a + b + c as sumc, a + b + d as sumd, d
  44. FROM test1
  45. ORDER BY d
  46. )
  47. GROUP BY sumc, sumd
  48. ORDER BY sumc
  49. query T multiline
  50. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM plan_test1
  51. ----
  52. Explained Query:
  53. Project (#4, #0, #1) // { arity: 3 }
  54. Map ((bigint_to_numeric(#2{sum_d}) / bigint_to_numeric(case when (#3{count_d} = 0) then null else #3{count_d} end))) // { arity: 5 }
  55. Reduce group_by=[((#0{a} + #1{b}) + #2{c}), ((#0{a} + #1{b}) + #3{d})] aggregates=[sum(#3{d}), count(#3{d})] // { arity: 4 }
  56. ReadStorage materialize.public.test1 // { arity: 4 }
  57. Source materialize.public.test1
  58. Target cluster: quickstart
  59. EOF
  60. # Test that chained unions get planned with a single union operator.
  61. query T multiline
  62. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM test1 UNION ALL SELECT * FROM test1 UNION ALL SELECT * FROM test1
  63. ----
  64. Explained Query:
  65. Union // { arity: 4 }
  66. ReadStorage materialize.public.test1 // { arity: 4 }
  67. ReadStorage materialize.public.test1 // { arity: 4 }
  68. ReadStorage materialize.public.test1 // { arity: 4 }
  69. Source materialize.public.test1
  70. Target cluster: quickstart
  71. EOF
  72. # Test that `limit 0` results in an empty constant with () as the keys
  73. statement ok
  74. CREATE TABLE with_primary_key(a int primary key, b int)
  75. query T multiline
  76. EXPLAIN OPTIMIZED PLAN WITH(TYPES) AS VERBOSE TEXT FOR
  77. select * from (select * from with_primary_key limit 0);
  78. ----
  79. Explained Query (fast path):
  80. Constant <empty>
  81. Target cluster: quickstart
  82. EOF
  83. # Check that TopK fusion transform is wired up
  84. statement ok
  85. create table t1(f1 int, f2 int);
  86. statement ok
  87. create materialized view mv1 as select * from (select * from t1 order by f1 limit 10 offset 2) order by f1 limit 3 offset 1;
  88. ----
  89. query T multiline
  90. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR materialized view mv1;
  91. ----
  92. materialize.public.mv1:
  93. TopK order_by=[#0{f1} asc nulls_last] limit=3 offset=3 // { arity: 2 }
  94. ReadStorage materialize.public.t1 // { arity: 2 }
  95. Source materialize.public.t1
  96. Target cluster: quickstart
  97. EOF