predicate_reduction.slt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 simplification of predicates.
  10. statement ok
  11. CREATE TABLE t1(f1 int, f2 int)
  12. statement ok
  13. INSERT INTO t1 VALUES (null, null), (0, null), (1, null), (1, 0), (null, 0)
  14. # We can simplify predicates with overlapping predicate subexpressions.
  15. query II
  16. SELECT * FROM t1 WHERE f1 = 0 and (f1 = 0 or f1 = 1)
  17. ----
  18. 0
  19. NULL
  20. query T multiline
  21. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 = 0 and (f1 = 0 or f1 = 1)
  22. ----
  23. Explained Query:
  24. Filter (#0{f1} = 0) // { arity: 2 }
  25. ReadStorage materialize.public.t1 // { arity: 2 }
  26. Source materialize.public.t1
  27. filter=((#0{f1} = 0))
  28. Target cluster: quickstart
  29. EOF
  30. query T multiline
  31. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 is null and (f1 is null or f1 = 1)
  32. ----
  33. Explained Query:
  34. Filter (#0{f1}) IS NULL // { arity: 2 }
  35. ReadStorage materialize.public.t1 // { arity: 2 }
  36. Source materialize.public.t1
  37. filter=((#0{f1}) IS NULL)
  38. Target cluster: quickstart
  39. EOF
  40. mode cockroach
  41. query II rowsort
  42. SELECT * FROM t1 WHERE f1 is null and (f1 is null or f1 = 1)
  43. ----
  44. NULL NULL
  45. NULL 0
  46. mode standard
  47. # Test that subexpression matching can detect a `!(predicate)` and then replace
  48. # other instances of `predicate` with `false`.
  49. query T multiline
  50. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE f1 is not null and (f1 is null or f1 = 1)
  51. ----
  52. Explained Query:
  53. Filter (#0{f1} = 1) // { arity: 2 }
  54. ReadStorage materialize.public.t1 // { arity: 2 }
  55. Source materialize.public.t1
  56. filter=((#0{f1} = 1))
  57. Target cluster: quickstart
  58. EOF
  59. mode cockroach
  60. query II rowsort
  61. SELECT * FROM t1 WHERE f1 is not null and (f1 is null or f1 = 1)
  62. ----
  63. 1 NULL
  64. 1 0
  65. mode standard
  66. # A test that simplification works when overlapping subexpressions are nested.
  67. query T multiline
  68. EXPLAIN DECORRELATED PLAN WITH(arity) FOR SELECT * FROM t1 WHERE (f1 is null)::int - 1 = 0 and ((f1 is null) or ((f1 is null)::int - 1 = 0))
  69. ----
  70. Filter (((boolean_to_integer((#0{f1}) IS NULL) - 1) = 0) AND ((#0{f1}) IS NULL OR ((boolean_to_integer((#0{f1}) IS NULL) - 1) = 0))) // { arity: 2 }
  71. CrossJoin // { arity: 2 }
  72. Constant // { arity: 0 }
  73. - ()
  74. Get materialize.public.t1 // { arity: 2 }
  75. Target cluster: quickstart
  76. EOF
  77. query T multiline
  78. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 WHERE ((f1 is null)::int - 1)::string LIKE '1' and ((f1 is null) or not (((f1 is null)::int - 1)::string LIKE '1'))
  79. ----
  80. Explained Query (fast path):
  81. Constant <empty>
  82. Target cluster: quickstart
  83. EOF
  84. # Ensure that subexpression matching does not break predicate evaluation order
  85. # guarantees for `CASE`
  86. statement ok
  87. CREATE TABLE t2(f1 int not null, f2 int not null)
  88. statement ok
  89. INSERT INTO t2 VALUES (0, -1), (1, 5), (1, -2)
  90. query T multiline
  91. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 WHERE f1 + f2 > 0 and case when f1 + f2 > 0 then 1/f1 > 0 else false end;
  92. ----
  93. Explained Query:
  94. Project (#0{f1}, #1{f2}) // { arity: 2 }
  95. Filter #2 AND case when #2 then ((1 / #0{f1}) > 0) else false end // { arity: 3 }
  96. Map (((#0{f1} + #1{f2}) > 0)) // { arity: 3 }
  97. ReadStorage materialize.public.t2 // { arity: 2 }
  98. Source materialize.public.t2
  99. filter=(#2 AND case when #2 then ((1 / #0{f1}) > 0) else false end)
  100. map=(((#0{f1} + #1{f2}) > 0))
  101. Target cluster: quickstart
  102. EOF
  103. query T multiline
  104. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t2 WHERE case when f1 + f2 > 0 then 1/f1 > 0 else false end and f1 + f2 > 0;
  105. ----
  106. Explained Query:
  107. Project (#0{f1}, #1{f2}) // { arity: 2 }
  108. Filter #2 AND case when #2 then ((1 / #0{f1}) > 0) else false end // { arity: 3 }
  109. Map (((#0{f1} + #1{f2}) > 0)) // { arity: 3 }
  110. ReadStorage materialize.public.t2 // { arity: 2 }
  111. Source materialize.public.t2
  112. filter=(#2 AND case when #2 then ((1 / #0{f1}) > 0) else false end)
  113. map=(((#0{f1} + #1{f2}) > 0))
  114. Target cluster: quickstart
  115. EOF
  116. query II
  117. SELECT * FROM t2 WHERE f1 + f2 > 0 and case when f1 + f2 > 0 then 1/f1 > 0 else false end;
  118. ----
  119. 1
  120. 5