join_fusion.slt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.
  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 t1 (f1 integer, f2 integer)
  12. statement ok
  13. INSERT INTO t1 VALUES (1, 1), (2, 3), (4, 5);
  14. statement ok
  15. CREATE TABLE t2 (f1 integer, f2 integer)
  16. statement ok
  17. INSERT INTO t2 VALUES (2, 3), (5, 5);
  18. statement ok
  19. CREATE TABLE t3 (f1 integer, f2 integer)
  20. statement ok
  21. INSERT INTO t3 VALUES (2, 3), (5, 5);
  22. query T multiline
  23. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 INNER JOIN t2 ON t2.f2 = t1.f2 INNER JOIN t3 ON t1.f1 = t3.f1 WHERE t1.f1 <= t2.f1 AND t3.f1 > 0;
  24. ----
  25. Explained Query:
  26. Project (#0{f1}..=#2{f1}, #1{f2}, #0{f1}, #5{f2}) // { arity: 6 }
  27. Filter (#0{f1} <= #2{f1}) // { arity: 6 }
  28. Join on=(#0{f1} = #4{f1} AND #1{f2} = #3{f2}) type=delta // { arity: 6 }
  29. implementation
  30. %0:t1 » %2:t3[#0{f1}]Kif » %1:t2[#1{f2}]K
  31. %1:t2 » %0:t1[#1{f2}]Kif » %2:t3[#0{f1}]Kif
  32. %2:t3 » %0:t1[#0{f1}]Kif » %1:t2[#1{f2}]K
  33. ArrangeBy keys=[[#0{f1}], [#1{f2}]] // { arity: 2 }
  34. Filter (#0{f1} > 0) AND (#1{f2}) IS NOT NULL // { arity: 2 }
  35. ReadStorage materialize.public.t1 // { arity: 2 }
  36. ArrangeBy keys=[[#1{f2}]] // { arity: 2 }
  37. Filter (#1{f2}) IS NOT NULL // { arity: 2 }
  38. ReadStorage materialize.public.t2 // { arity: 2 }
  39. ArrangeBy keys=[[#0{f1}]] // { arity: 2 }
  40. Filter (#0{f1} > 0) // { arity: 2 }
  41. ReadStorage materialize.public.t3 // { arity: 2 }
  42. Source materialize.public.t1
  43. filter=((#0{f1} > 0) AND (#1{f2}) IS NOT NULL)
  44. Source materialize.public.t2
  45. filter=((#1{f2}) IS NOT NULL)
  46. Source materialize.public.t3
  47. filter=((#0{f1} > 0))
  48. Target cluster: quickstart
  49. EOF
  50. query IIIIII
  51. SELECT * FROM t1 INNER JOIN t2 ON t2.f2 = t1.f2 INNER JOIN t3 ON t1.f1 = t3.f1 WHERE t1.f1 <= t2.f1 AND t3.f1 > 0;
  52. ----
  53. 2 3 2 3 2 3
  54. #
  55. # Additional queries that came out of the randomized testing of materialize#6936
  56. #
  57. #
  58. # Randomized queries against a TPC-like schema
  59. #
  60. statement ok
  61. CREATE TABLE customer (c_custkey integer, c_nationkey integer NOT NULL, c_acctbal decimal(15, 2) NOT NULL);
  62. statement ok
  63. CREATE INDEX pk_customer_custkey ON customer (c_custkey);
  64. statement ok
  65. CREATE INDEX fk_customer_nationkey ON customer (c_nationkey ASC);
  66. statement ok
  67. CREATE TABLE orders (o_orderkey integer, o_custkey integer NOT NULL, o_totalprice decimal(15, 2) NOT NULL, o_orderdate DATE NOT NULL);
  68. statement ok
  69. CREATE INDEX pk_orders_orderkey ON orders (o_orderkey);
  70. statement ok
  71. CREATE INDEX fk_orders_custkey ON orders (o_custkey ASC);
  72. statement ok
  73. CREATE TABLE lineitem (l_orderkey integer NOT NULL, l_partkey integer NOT NULL, l_suppkey integer NOT NULL, l_linenumber integer NOT NULL, l_extendedprice decimal(15, 2) NOT NULL, l_shipdate date NOT NULL, l_commitdate date NOT NULL, l_receiptdate date NOT NULL);
  74. statement ok
  75. CREATE INDEX pk_lineitem_orderkey_linenumber ON lineitem (l_orderkey, l_linenumber);
  76. statement ok
  77. CREATE INDEX fk_lineitem_orderkey ON lineitem (l_orderkey ASC);
  78. statement ok
  79. CREATE INDEX fk_lineitem_partkey ON lineitem (l_partkey ASC);
  80. statement ok
  81. CREATE INDEX fk_lineitem_suppkey ON lineitem (l_suppkey ASC);
  82. statement ok
  83. CREATE INDEX fk_lineitem_partsuppkey ON lineitem (l_partkey ASC, l_suppkey ASC);
  84. query T multiline
  85. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM lineitem
  86. JOIN orders ON ( l_orderkey = o_orderkey )
  87. JOIN customer ON ( c_acctbal >= o_totalprice )
  88. WHERE l_shipDATE <> o_orderdate
  89. AND o_orderdate = l_shipDATE - INTERVAL ' 9 MONTHS ';
  90. ----
  91. Explained Query:
  92. Project (#0{l_orderkey}..=#7{l_receiptdate}, #0{l_orderkey}, #9{o_custkey}..=#14{c_acctbal}) // { arity: 15 }
  93. Filter (#5{l_shipdate} != #11{o_orderdate}) AND (#14{c_acctbal} >= #10{o_totalprice}) // { arity: 15 }
  94. Join on=(#0{l_orderkey} = #8{o_orderkey} AND date_to_timestamp(#11{o_orderdate}) = (#5{l_shipdate} - 9 months)) type=delta // { arity: 15 }
  95. implementation
  96. %0:lineitem » %1:orders[#0{o_orderkey}, date_to_timestamp(#3{o_orderdate})]KK » %2:customer[×]
  97. %1:orders » %0:lineitem[#0{l_orderkey}, (#5{l_shipdate} - 9 months)]KK » %2:customer[×]
  98. %2:customer » %0:lineitem[×] » %1:orders[#0{o_orderkey}, date_to_timestamp(#3{o_orderdate})]KK
  99. ArrangeBy keys=[[], [#0{l_orderkey}, (#5{l_shipdate} - 9 months)]] // { arity: 8 }
  100. ReadIndex on=lineitem pk_lineitem_orderkey_linenumber=[*** full scan ***] // { arity: 8 }
  101. ArrangeBy keys=[[#0{o_orderkey}, date_to_timestamp(#3{o_orderdate})]] // { arity: 4 }
  102. Filter (#0{o_orderkey}) IS NOT NULL // { arity: 4 }
  103. ReadIndex on=orders pk_orders_orderkey=[*** full scan ***] // { arity: 4 }
  104. ArrangeBy keys=[[]] // { arity: 3 }
  105. ReadIndex on=customer pk_customer_custkey=[*** full scan ***] // { arity: 3 }
  106. Used Indexes:
  107. - materialize.public.pk_customer_custkey (*** full scan ***)
  108. - materialize.public.pk_orders_orderkey (*** full scan ***)
  109. - materialize.public.pk_lineitem_orderkey_linenumber (*** full scan ***)
  110. Target cluster: quickstart
  111. EOF
  112. query T multiline
  113. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT MIN( o_orderkey )
  114. FROM lineitem JOIN orders ON ( l_extendedprice = o_totalprice )
  115. WHERE l_commitDATE = '1997-01-25'
  116. AND o_orderkey BETWEEN 38 AND 195
  117. AND o_orderdate = l_commitDATE + ' 7 MONTHS '
  118. AND o_orderkey = ( SELECT l_orderkey FROM lineitem WHERE l_orderkey = 38 )
  119. ----
  120. Explained Query:
  121. With
  122. cte l0 =
  123. Project (#0{l_orderkey}) // { arity: 1 }
  124. ReadIndex on=materialize.public.lineitem fk_lineitem_orderkey=[lookup value=(38)] // { arity: 9 }
  125. cte l1 =
  126. Reduce aggregates=[min(#0{o_orderkey})] // { arity: 1 }
  127. Project (#1{o_orderkey}) // { arity: 1 }
  128. Join on=(#0{l_extendedprice} = #2{o_totalprice} AND #1{o_orderkey} = #3{l_orderkey}) type=delta // { arity: 4 }
  129. implementation
  130. %0:lineitem » %1:orders[#1{o_totalprice}]Keiif » %2[#0]K
  131. %1:orders » %0:lineitem[#0{l_extendedprice}]Kef » %2[#0]K
  132. %2 » %1:orders[#0{o_orderkey}]KAeiif » %0:lineitem[#0{l_extendedprice}]Kef
  133. ArrangeBy keys=[[#0{l_extendedprice}]] // { arity: 1 }
  134. Project (#4{l_extendedprice}) // { arity: 1 }
  135. Filter (#6{l_commitdate} = 1997-01-25) // { arity: 8 }
  136. ReadIndex on=lineitem fk_lineitem_orderkey=[*** full scan ***] // { arity: 8 }
  137. ArrangeBy keys=[[#0{o_orderkey}], [#1{o_totalprice}]] // { arity: 2 }
  138. Project (#0{o_orderkey}, #2{o_totalprice}) // { arity: 2 }
  139. Filter (#0{o_orderkey} <= 195) AND (#0{o_orderkey} >= 38) AND (1997-08-25 00:00:00 = date_to_timestamp(#3{o_orderdate})) // { arity: 4 }
  140. ReadIndex on=orders pk_orders_orderkey=[*** full scan ***] // { arity: 4 }
  141. ArrangeBy keys=[[#0{l_orderkey}]] // { arity: 1 }
  142. Union // { arity: 1 }
  143. Get l0 // { arity: 1 }
  144. Project (#1) // { arity: 1 }
  145. Filter (#1 <= 195) AND (#1 >= 38) // { arity: 2 }
  146. FlatMap guard_subquery_size(#0{count}) // { arity: 2 }
  147. Reduce aggregates=[count(*)] // { arity: 1 }
  148. Project () // { arity: 0 }
  149. Get l0 // { arity: 1 }
  150. Return // { arity: 1 }
  151. Union // { arity: 1 }
  152. Get l1 // { arity: 1 }
  153. Map (null) // { arity: 1 }
  154. Union // { arity: 0 }
  155. Negate // { arity: 0 }
  156. Project () // { arity: 0 }
  157. Get l1 // { arity: 1 }
  158. Constant // { arity: 0 }
  159. - ()
  160. Used Indexes:
  161. - materialize.public.pk_orders_orderkey (*** full scan ***)
  162. - materialize.public.fk_lineitem_orderkey (*** full scan ***, lookup)
  163. Target cluster: quickstart
  164. EOF
  165. query T multiline
  166. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT l_partkey AS col24843 , l_orderkey AS col24844 , l_partkey AS col24845
  167. FROM lineitem JOIN orders ON ( l_commitDATE = o_orderdate )
  168. JOIN customer ON ( o_custkey = c_custkey )
  169. WHERE l_extendedprice = o_totalprice
  170. AND c_custkey = 134
  171. AND l_extendedprice = MOD (o_totalprice , 5 ) ;
  172. ----
  173. Explained Query:
  174. With
  175. cte l0 =
  176. ArrangeBy keys=[[#0]] // { arity: 1 }
  177. Constant // { arity: 1 }
  178. - (134)
  179. Return // { arity: 3 }
  180. Project (#1{l_partkey}, #0{l_orderkey}, #1{l_partkey}) // { arity: 3 }
  181. Join on=(#2{l_extendedprice} = #4{o_totalprice} AND #3{l_commitdate} = #5{o_orderdate}) type=delta // { arity: 6 }
  182. implementation
  183. %0:lineitem » %1:orders[#0{o_totalprice}, #1{o_orderdate}]KKef » %2:customer[×]e
  184. %1:orders » %0:lineitem[#2{l_extendedprice}, #3{l_commitdate}]KKf » %2:customer[×]e
  185. %2:customer » %1:orders[×]ef » %0:lineitem[#2{l_extendedprice}, #3{l_commitdate}]KKf
  186. ArrangeBy keys=[[#2{l_extendedprice}, #3{l_commitdate}]] // { arity: 4 }
  187. Project (#0{l_orderkey}, #1{l_partkey}, #4{l_extendedprice}, #6{l_commitdate}) // { arity: 4 }
  188. Filter (#4{l_extendedprice} = (#4{l_extendedprice} % 5)) // { arity: 8 }
  189. ReadIndex on=lineitem pk_lineitem_orderkey_linenumber=[*** full scan ***] // { arity: 8 }
  190. ArrangeBy keys=[[], [#0{o_totalprice}, #1{o_orderdate}]] // { arity: 2 }
  191. Project (#2{o_totalprice}, #3{o_orderdate}) // { arity: 2 }
  192. Filter (#2{o_totalprice} = (#2{o_totalprice} % 5)) // { arity: 5 }
  193. ReadIndex on=materialize.public.orders fk_orders_custkey=[lookup values=<Get l0>] // { arity: 5 }
  194. ArrangeBy keys=[[]] // { arity: 0 }
  195. Project () // { arity: 0 }
  196. ReadIndex on=materialize.public.customer pk_customer_custkey=[lookup values=<Get l0>] // { arity: 4 }
  197. Used Indexes:
  198. - materialize.public.pk_customer_custkey (lookup)
  199. - materialize.public.fk_orders_custkey (lookup)
  200. - materialize.public.pk_lineitem_orderkey_linenumber (*** full scan ***)
  201. Target cluster: quickstart
  202. EOF
  203. query T multiline
  204. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
  205. FROM lineitem JOIN orders ON ( l_extendedprice = o_totalprice )
  206. JOIN customer ON ( o_custkey = c_custkey )
  207. WHERE o_custkey = 229
  208. AND l_receiptDATE = o_orderdate + INTERVAL ' 6 DAYS '
  209. AND l_shipDATE = o_orderdate;
  210. ----
  211. Explained Query:
  212. With
  213. cte l0 =
  214. ArrangeBy keys=[[#0]] // { arity: 1 }
  215. Constant // { arity: 1 }
  216. - (229)
  217. Return // { arity: 15 }
  218. Project (#0{l_orderkey}..=#9{o_custkey}, #4{l_extendedprice}, #5{l_shipdate}, #12{c_custkey}..=#14{c_acctbal}) // { arity: 15 }
  219. Join on=(#4{l_extendedprice} = #10{o_totalprice} AND #5{l_shipdate} = #11{o_orderdate}) type=delta // { arity: 15 }
  220. implementation
  221. %0:lineitem » %1:orders[#2{o_totalprice}, #3{o_orderdate}]KKe » %2:customer[×]e
  222. %1:orders » %0:lineitem[#4{l_extendedprice}, #5{l_shipdate}]KKf » %2:customer[×]e
  223. %2:customer » %1:orders[×]e » %0:lineitem[#4{l_extendedprice}, #5{l_shipdate}]KKf
  224. ArrangeBy keys=[[#4{l_extendedprice}, #5{l_shipdate}]] // { arity: 8 }
  225. Filter (date_to_timestamp(#7{l_receiptdate}) = (#5{l_shipdate} + 6 days)) // { arity: 8 }
  226. ReadIndex on=lineitem pk_lineitem_orderkey_linenumber=[*** full scan ***] // { arity: 8 }
  227. ArrangeBy keys=[[], [#2{o_totalprice}, #3{o_orderdate}]] // { arity: 4 }
  228. Project (#0{o_orderkey}..=#3{o_orderdate}) // { arity: 4 }
  229. ReadIndex on=materialize.public.orders fk_orders_custkey=[lookup values=<Get l0>] // { arity: 5 }
  230. ArrangeBy keys=[[]] // { arity: 3 }
  231. Project (#0{c_custkey}..=#2{c_acctbal}) // { arity: 3 }
  232. ReadIndex on=materialize.public.customer pk_customer_custkey=[lookup values=<Get l0>] // { arity: 4 }
  233. Used Indexes:
  234. - materialize.public.pk_customer_custkey (lookup)
  235. - materialize.public.fk_orders_custkey (lookup)
  236. - materialize.public.pk_lineitem_orderkey_linenumber (*** full scan ***)
  237. Target cluster: quickstart
  238. EOF
  239. #
  240. # Randomized queries against the "simple" schema
  241. #
  242. query T multiline
  243. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT * FROM t1 AS a1, t1 AS a2
  244. WHERE a2.f1 + a1.f2 = (SELECT 1)
  245. AND a2.f1 IS NULL;
  246. ----
  247. Explained Query (fast path):
  248. Constant <empty>
  249. Target cluster: quickstart
  250. EOF