demand.slt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. mode cockroach
  10. statement ok
  11. CREATE TABLE t (a int);
  12. statement ok
  13. INSERT INTO t (a) VALUES (0), (1), (2), (3);
  14. # Test that demand analysis does not result in a 1/0 error for column
  15. # that is not demanded as a join output (column a from t).
  16. # The output should have NO errors.
  17. query I
  18. select x from (select x, 1/a from (select 2 as x), t);
  19. ----
  20. 2
  21. 2
  22. 2
  23. 2
  24. # A `dummy` used to occur in the following plan before putting an extra call to `ProjectionPushdown` after the `Demand`
  25. # call in the physical optimizer, because at the time of the first `Demand` call, the column that is later dummied in
  26. # the second call is still being used in a join constraint, but this join is then eliminated by `RedundantJoin`.
  27. query T multiline
  28. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  29. select
  30. case when (((false)
  31. and (true))
  32. and ((numrange(0,0)) -|- (case when (cast(null as mz_aclitem)) = (cast(null as mz_aclitem)) then numrange(0,0) else numrange(0,0) end
  33. )))
  34. and (((10::uint8) & (case when (TIMESTAMPTZ '2023-01-01 01:23:45+06') >= ((TIMESTAMPTZ '95143-12-31 23:59:59+06' + INTERVAL '167 MILLENNIUM')) then 2::uint8 else 2::uint8 end
  35. )) < (pg_catalog.mod(
  36. CAST(null::uint8 as uint8),
  37. CAST(null::uint8 as uint8)))) then mz_catalog.kafka_murmur2(
  38. CAST(cast('\xDEADBEEF' as bytea) as bytea)) else mz_catalog.kafka_murmur2(
  39. CAST(cast('\xDEADBEEF' as bytea) as bytea)) end
  40. as c0,
  41. (mz_unsafe.mz_avg_promotion(
  42. CAST(0::uint4 as uint4))) / (null::numeric) as c1,
  43. mz_catalog.try_parse_monotonic_iso8601_timestamp(
  44. CAST(pg_catalog.obj_description(
  45. CAST(mz_internal.aclitem_grantee(
  46. CAST(cast(null as aclitem) as aclitem)) as oid),
  47. CAST((('[]'::jsonb) -> (pg_catalog.session_user())) ->> (pg_catalog.pg_get_viewdef(
  48. CAST(case when ('{}'::map[text=>text]) ?| (array['a', 'b', null, '']::text[]) then null::oid else null::oid end
  49. as oid),
  50. CAST(true as bool))) as text)) as text)) as c2,
  51. '2024-12-18 12:54:29.994+00'::timestamptz as c3
  52. from
  53. (select distinct
  54. mz_catalog.map_agg(
  55. CAST(cast(coalesce(null::text,
  56. null::text) as text) as text),
  57. null) as c0,
  58. mz_catalog.mz_environment_id() as c1,
  59. (mz_catalog.mz_environment_id()) || ((null::uint4) + (4294967295::uint4)) as c2,
  60. pg_catalog.tstzrange(
  61. CAST((INTERVAL '2147483647 MONTHS') + (TIMESTAMPTZ '2023-01-01 01:23:45+06') as timestamptz),
  62. CAST(TIMESTAMPTZ '2023-01-01 01:23:45+06' as timestamptz)) as c3,
  63. pg_catalog.version() as c4
  64. from
  65. (select
  66. 36 as c0,
  67. 33 as c1
  68. from
  69. (select
  70. 4 as c0
  71. from
  72. "mz_catalog"."mz_columns" as ref_2
  73. where (false) <> (true)
  74. limit coalesce(13, 72)) as subq_0
  75. where (true) = (true)
  76. limit coalesce(82, 50)) as subq_1
  77. where (pg_catalog.mod(
  78. CAST(case when ((TIMESTAMPTZ '0001-01-01 00:00:00+06' - INTERVAL '4713 YEARS')) >= ((TIMESTAMPTZ '95143-12-31 23:59:59+06' + INTERVAL '167 MILLENNIUM')) then null::int2 else null::int2 end
  79. as int2),
  80. CAST(10::int2 as int2))) > (null::int2)
  81. limit coalesce(90, 42)) as subq_2
  82. where true
  83. limit coalesce(43, 120);
  84. ----
  85. Explained Query:
  86. Finish limit=43 output=[#0..=#3]
  87. With
  88. cte l0 =
  89. Distinct project=[] // { arity: 0 }
  90. TopK limit=13 // { arity: 0 }
  91. Filter error("timestamp out of range") // { arity: 0 }
  92. Project () // { arity: 0 }
  93. ReadIndex on=mz_columns mz_columns_ind=[*** full scan ***] // { arity: 8 }
  94. Return // { arity: 4 }
  95. Map (833564499, null, null, 2024-12-18 12:54:29.994 UTC) // { arity: 4 }
  96. TopK limit=90 // { arity: 0 }
  97. Union // { arity: 0 }
  98. Get l0 // { arity: 0 }
  99. Negate // { arity: 0 }
  100. Get l0 // { arity: 0 }
  101. Constant // { arity: 0 }
  102. - ()
  103. Used Indexes:
  104. - mz_catalog.mz_columns_ind (*** full scan ***)
  105. Target cluster: mz_catalog_server
  106. EOF
  107. ## -------------------- Tests for WITH MUTUALLY RECURSIVE --------------------
  108. # Demand creates the `#0 + #0` from `#0 + #2`.
  109. query T multiline
  110. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  111. WITH MUTUALLY RECURSIVE
  112. c0(key int, a int) AS (
  113. SELECT * FROM c2
  114. UNION
  115. SELECT a, a FROM t
  116. ),
  117. c1(key int, a int) AS (
  118. SELECT key + 1, -a FROM c0
  119. ),
  120. c2(key int, a int) AS (
  121. SELECT c0.key + c1.key, c0.a + c1.a
  122. FROM c0, c1
  123. WHERE c0.key = c1.key
  124. )
  125. SELECT * FROM c2;
  126. ----
  127. Explained Query:
  128. With Mutually Recursive
  129. cte l0 =
  130. Distinct project=[#0{a}, #1{a}] // { arity: 2 }
  131. Union // { arity: 2 }
  132. Get l1 // { arity: 2 }
  133. Project (#0{a}, #0{a}) // { arity: 2 }
  134. Filter (#0{a}) IS NOT NULL // { arity: 1 }
  135. ReadStorage materialize.public.t // { arity: 1 }
  136. cte l1 =
  137. Project (#4, #5) // { arity: 2 }
  138. Map ((#0{a} + #0{a}), (#1{a} + #3{a})) // { arity: 6 }
  139. Join on=(#0{a} = #2{key}) type=differential // { arity: 4 }
  140. implementation
  141. %0:l0[#0{key}]K » %1:l0[#0{key}]K
  142. ArrangeBy keys=[[#0{a}]] // { arity: 2 }
  143. Get l0 // { arity: 2 }
  144. ArrangeBy keys=[[#0{key}]] // { arity: 2 }
  145. Project (#2, #3) // { arity: 2 }
  146. Map ((#0{a} + 1), -(#1{a})) // { arity: 4 }
  147. Get l0 // { arity: 2 }
  148. Return // { arity: 2 }
  149. Get l1 // { arity: 2 }
  150. Source materialize.public.t
  151. filter=((#0{a}) IS NOT NULL)
  152. Target cluster: quickstart
  153. EOF
  154. query II
  155. WITH MUTUALLY RECURSIVE
  156. c0(key int, a int) AS (
  157. SELECT * FROM c2
  158. UNION
  159. SELECT a, a FROM t
  160. ),
  161. c1(key int, a int) AS (
  162. SELECT key + 1, -a FROM c0
  163. ),
  164. c2(key int, a int) AS (
  165. SELECT c0.key + c1.key, c0.a + c1.a
  166. FROM c0, c1
  167. WHERE c0.key = c1.key
  168. )
  169. SELECT * FROM c2
  170. ORDER BY 1, 2;
  171. ----
  172. 2 1
  173. 4 0
  174. 4 1
  175. 6 1
  176. 6 2
  177. 8 -3
  178. 8 -2