scalar_cse.slt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Scalar CSE transforms.
  10. statement ok
  11. CREATE TABLE x (a string, b int not null)
  12. query T multiline
  13. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT b*b*b, b*b FROM x
  14. ----
  15. Explained Query:
  16. Project (#3, #2) // { arity: 2 }
  17. Map ((#1{b} * #1{b}), (#2 * #1{b})) // { arity: 4 }
  18. ReadStorage materialize.public.x // { arity: 2 }
  19. Source materialize.public.x
  20. Target cluster: quickstart
  21. EOF
  22. query T multiline
  23. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT b*b*b, b*b+1 FROM x
  24. ----
  25. Explained Query:
  26. Project (#3, #4) // { arity: 2 }
  27. Map ((#1{b} * #1{b}), (#2 * #1{b}), (#2 + 1)) // { arity: 5 }
  28. ReadStorage materialize.public.x // { arity: 2 }
  29. Source materialize.public.x
  30. Target cluster: quickstart
  31. EOF
  32. query T multiline
  33. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  34. a::json->'Field1',
  35. a::json->'Field2',
  36. a::json->'Field3'
  37. FROM x
  38. ----
  39. Explained Query:
  40. Project (#3..=#5) // { arity: 3 }
  41. Map (text_to_jsonb(#0{a}), (#2 -> "Field1"), (#2 -> "Field2"), (#2 -> "Field3")) // { arity: 6 }
  42. ReadStorage materialize.public.x // { arity: 2 }
  43. Source materialize.public.x
  44. Target cluster: quickstart
  45. EOF
  46. query T multiline
  47. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  48. a::json->'Field1'->'Foo',
  49. a::json->'Field1'->'Bar',
  50. a::json->'Field2'->'Baz',
  51. a::json->'Field2'->'Quux'->'Zorb'
  52. FROM x
  53. ----
  54. Explained Query:
  55. Project (#4, #5, #7, #8) // { arity: 4 }
  56. Map (text_to_jsonb(#0{a}), (#2 -> "Field1"), (#3 -> "Foo"), (#3 -> "Bar"), (#2 -> "Field2"), (#6 -> "Baz"), ((#6 -> "Quux") -> "Zorb")) // { arity: 9 }
  57. ReadStorage materialize.public.x // { arity: 2 }
  58. Source materialize.public.x
  59. Target cluster: quickstart
  60. EOF
  61. # Ensure we don't inline if-guarded expressions
  62. query T multiline
  63. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  64. CASE WHEN b = 0 THEN 0 ELSE 1/b END,
  65. CASE WHEN b != 0 THEN 1/b ELSE 0 END
  66. FROM x
  67. ----
  68. Explained Query:
  69. Project (#2, #3) // { arity: 2 }
  70. Map (case when (#1{b} = 0) then 0 else (1 / #1{b}) end, case when (#1{b} != 0) then (1 / #1{b}) else 0 end) // { arity: 4 }
  71. ReadStorage materialize.public.x // { arity: 2 }
  72. Source materialize.public.x
  73. Target cluster: quickstart
  74. EOF
  75. # Ensure we update columns references in if-guarded expressions
  76. query T multiline
  77. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  78. CASE WHEN b = 0 THEN 0 ELSE 1/b END,
  79. CASE WHEN b != 0 THEN 1/b ELSE 0 END
  80. FROM
  81. (SELECT b/2 as b FROM x)
  82. ----
  83. Explained Query:
  84. Project (#3, #4) // { arity: 2 }
  85. Map ((#1{b} / 2), case when (#2{b} = 0) then 0 else (1 / #2) end, case when (#2{b} != 0) then (1 / #2) else 0 end) // { arity: 5 }
  86. ReadStorage materialize.public.x // { arity: 2 }
  87. Source materialize.public.x
  88. Target cluster: quickstart
  89. EOF
  90. # Regression test for https://github.com/MaterializeInc/database-issues/issues/6372
  91. statement ok
  92. CREATE TABLE t(x int);
  93. query T multiline
  94. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR select * from t where 2*x + 2*x > 0
  95. ----
  96. Explained Query:
  97. Project (#0{x})
  98. Filter ((#1 + #1) > 0)
  99. Map ((2 * #0{x}))
  100. ReadStorage materialize.public.t
  101. Source materialize.public.t
  102. filter=(((#1 + #1) > 0))
  103. map=((2 * #0{x}))
  104. Target cluster: quickstart
  105. EOF
  106. query T multiline
  107. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR select * from t where 2*x + 2*x + 2*x > 0
  108. ----
  109. Explained Query:
  110. Project (#0{x})
  111. Filter (((#1 + #1) + #1) > 0)
  112. Map ((2 * #0{x}))
  113. ReadStorage materialize.public.t
  114. Source materialize.public.t
  115. filter=((((#1 + #1) + #1) > 0))
  116. map=((2 * #0{x}))
  117. Target cluster: quickstart
  118. EOF
  119. query T multiline
  120. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR select * from t where 2*x + 5*x + 2*x > 0
  121. ----
  122. Explained Query:
  123. Project (#0{x})
  124. Filter (((#1 + (5 * #0{x})) + #1) > 0)
  125. Map ((2 * #0{x}))
  126. ReadStorage materialize.public.t
  127. Source materialize.public.t
  128. filter=((((#1 + (5 * #0{x})) + #1) > 0))
  129. map=((2 * #0{x}))
  130. Target cluster: quickstart
  131. EOF