lookup_join.slt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/lookup_join
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. simple conn=mz_system,user=mz_system
  21. ALTER SYSTEM SET unsafe_enable_table_keys = true
  22. ----
  23. COMPLETE 0
  24. statement ok
  25. CREATE TABLE abc (a INT, b INT, c INT, PRIMARY KEY (a, c))
  26. statement ok
  27. INSERT INTO abc VALUES (1, 1, 2), (2, 1, 1), (2, NULL, 2)
  28. statement ok
  29. CREATE TABLE def (d INT, e INT, f INT, PRIMARY KEY (f, e))
  30. statement ok
  31. INSERT INTO def VALUES (1, 1, 2), (2, 1, 1), (NULL, 2, 1)
  32. statement ok
  33. CREATE TABLE gh (g INT, h INT, INDEX g_idx (g))
  34. statement ok
  35. INSERT INTO gh VALUES (NULL, 1)
  36. # # Set up the statistics as if the first table is much smaller than the second.
  37. # # This will make lookup join into the second table be the best plan.
  38. # # TODO(radu): we have to use very small row counts because of the poor row
  39. # # count estimation for joins (left-rows * right-rows / 10).
  40. # statement ok
  41. # ALTER TABLE abc INJECT STATISTICS '[
  42. # {
  43. # "columns": ["a"],
  44. # "created_at": "2018-01-01 1:00:00.00000+00:00",
  45. # "row_count": 100,
  46. # "distinct_count": 100
  47. # }
  48. # ]'
  49. # statement ok
  50. # ALTER TABLE def INJECT STATISTICS '[
  51. # {
  52. # "columns": ["f"],
  53. # "created_at": "2018-01-01 1:00:00.00000+00:00",
  54. # "row_count": 10000,
  55. # "distinct_count": 10000
  56. # }
  57. # ]'
  58. # statement ok
  59. # ALTER TABLE gh INJECT STATISTICS '[
  60. # {
  61. # "columns": ["g"],
  62. # "created_at": "2018-01-01 1:00:00.00000+00:00",
  63. # "row_count": 10000,
  64. # "distinct_count": 10000
  65. # }
  66. # ]'
  67. query IIIIII rowsort
  68. SELECT * FROM abc JOIN def ON f = b
  69. ----
  70. 1 1 2 2 1 1
  71. 2 1 1 2 1 1
  72. 1 1 2 NULL 2 1
  73. 2 1 1 NULL 2 1
  74. query IIIIII rowsort
  75. SELECT * FROM abc JOIN def ON f = b WHERE a > 1 AND e > 1
  76. ----
  77. 2 1 1 NULL 2 1
  78. query IIIIII rowsort
  79. SELECT * FROM abc JOIN def ON f = b AND a > 1 AND e > 1
  80. ----
  81. 2 1 1 NULL 2 1
  82. # Filter right side of a lookup join with a restriction on an indexed column.
  83. query IIIIII rowsort
  84. SELECT * FROM abc JOIN def ON f = a WHERE f > 1
  85. ----
  86. 2 1 1 1 1 2
  87. 2 NULL 2 1 1 2
  88. # Test lookup join with restriction relating the left and right side.
  89. query IIIIII rowsort
  90. SELECT * FROM abc JOIN def ON f = b WHERE a >= e
  91. ----
  92. 1 1 2 2 1 1
  93. 2 1 1 2 1 1
  94. 2 1 1 NULL 2 1
  95. # Test lookup join with restriction relating the left and right side.
  96. query IIIIII rowsort
  97. SELECT * FROM abc JOIN def ON f = b AND a >= e
  98. ----
  99. 1 1 2 2 1 1
  100. 2 1 1 2 1 1
  101. 2 1 1 NULL 2 1
  102. # Test lookup join with selecting a subset of the columns.
  103. query III rowsort
  104. SELECT a, b, e FROM abc JOIN def ON f = b WHERE a >= e
  105. ----
  106. 1 1 1
  107. 2 1 1
  108. 2 1 2
  109. # Test lookup join on NULL column. (https://github.com/cockroachdb/cockroach/issues/27032)
  110. query I
  111. SELECT h FROM abc JOIN gh ON b = g
  112. ----
  113. statement ok
  114. CREATE TABLE data (a INT, b INT, c INT, d INT, PRIMARY KEY (a, b, c, d))
  115. # Generate all combinations of values 1 to 10.
  116. statement ok
  117. INSERT INTO data SELECT a, b, c, d FROM
  118. generate_series(1, 10) AS a(a),
  119. generate_series(1, 10) AS b(b),
  120. generate_series(1, 10) AS c(c),
  121. generate_series(1, 10) AS d(d)
  122. # Ensure lookup join performs properly on input that has more than 100 rows.
  123. query I
  124. SELECT count(*) FROM (SELECT * FROM data WHERE c = 1) AS l NATURAL JOIN data AS r
  125. ----
  126. 1000
  127. statement ok
  128. CREATE TABLE foo (a int, b int)
  129. statement ok
  130. INSERT INTO foo VALUES (0, 1), (0, 2), (1, 1)
  131. statement ok
  132. CREATE TABLE bar (a int PRIMARY KEY, c int)
  133. statement ok
  134. INSERT INTO bar VALUES (0, 1), (1, 2), (2, 1)
  135. query III rowsort
  136. SELECT * FROM foo NATURAL JOIN bar
  137. ----
  138. 0 1 1
  139. 0 2 1
  140. 1 1 2
  141. statement ok
  142. CREATE TABLE books (title STRING, edition INT, shelf INT, PRIMARY KEY (title, edition))
  143. statement ok
  144. INSERT INTO books VALUES
  145. ('SICP', 1, 2),
  146. ('Intro to Algo', 1, 1),
  147. ('Intro to Algo', 2, 1),
  148. ('Intro to Algo', 3, 2),
  149. ('Art of Computer Programming', 1, 2),
  150. ('Art of Computer Programming', 2, 2)
  151. statement ok
  152. CREATE TABLE books2 (title STRING, edition INT, shelf INT, PRIMARY KEY (title, edition))
  153. statement ok
  154. INSERT INTO books2 VALUES
  155. ('SICP', 1, 2),
  156. ('Intro to Algo', 1, 1),
  157. ('Intro to Algo', 2, 1),
  158. ('Intro to Algo', 3, 2),
  159. ('Art of Computer Programming', 1, 2),
  160. ('Art of Computer Programming', 2, 2)
  161. statement ok
  162. ALTER TABLE books INJECT STATISTICS '[
  163. {
  164. "columns": ["title"],
  165. "created_at": "2018-01-01 1:00:00.00000+00:00",
  166. "row_count": 100,
  167. "distinct_count": 100
  168. }
  169. ]'
  170. statement ok
  171. ALTER TABLE books2 INJECT STATISTICS '[
  172. {
  173. "columns": ["title"],
  174. "created_at": "2018-01-01 1:00:00.00000+00:00",
  175. "row_count": 10000,
  176. "distinct_count": 1000
  177. }
  178. ]'
  179. statement ok
  180. CREATE TABLE authors (name STRING, book STRING)
  181. statement ok
  182. INSERT INTO authors VALUES
  183. ('Hal Abelson', 'SICP'),
  184. ('Geral Jay Sussman', 'SICP'),
  185. ('Thomas H Cormen', 'Intro to Algo'),
  186. ('Charles E Leiserson', 'Intro to Algo'),
  187. ('Ronald Rivest', 'Intro to Algo'),
  188. ('Clifford Stein', 'Intro to Algo'),
  189. ('Donald Knuth', 'Art of Computer Programming')
  190. statement ok
  191. ALTER TABLE authors INJECT STATISTICS '[
  192. {
  193. "columns": ["name"],
  194. "created_at": "2018-01-01 1:00:00.00000+00:00",
  195. "row_count": 100,
  196. "distinct_count": 100
  197. }
  198. ]'
  199. # Filter on a column that is not returned or in the equality columns.
  200. query T rowsort
  201. SELECT DISTINCT b1.title FROM books as b1 JOIN books2 as b2 ON b1.title = b2.title WHERE b1.shelf <> b2.shelf
  202. ----
  203. Intro to Algo
  204. query T rowsort
  205. SELECT DISTINCT authors.name FROM books AS b1, books2 as b2, authors WHERE b1.title = b2.title AND authors.book = b1.title AND b1.shelf <> b2.shelf
  206. ----
  207. Thomas H Cormen
  208. Charles E Leiserson
  209. Ronald Rivest
  210. Clifford Stein
  211. # Ensure lookup join preserves ordering from the left side.
  212. query T
  213. SELECT a.name FROM authors AS a JOIN books2 AS b2 ON a.book = b2.title ORDER BY a.name
  214. ----
  215. Charles E Leiserson
  216. Charles E Leiserson
  217. Charles E Leiserson
  218. Clifford Stein
  219. Clifford Stein
  220. Clifford Stein
  221. Donald Knuth
  222. Donald Knuth
  223. Geral Jay Sussman
  224. Hal Abelson
  225. Ronald Rivest
  226. Ronald Rivest
  227. Ronald Rivest
  228. Thomas H Cormen
  229. Thomas H Cormen
  230. Thomas H Cormen
  231. ####################################
  232. # LOOKUP JOIN ON SECONDARY INDEX #
  233. ####################################
  234. statement ok
  235. CREATE TABLE small (a INT PRIMARY KEY, b INT, c INT, d INT)
  236. statement ok
  237. CREATE TABLE large (a INT, b INT, c INT, d INT, PRIMARY KEY (a, b), INDEX bc (b) STORING (c))
  238. # Generate 10 rows for both tables.
  239. statement ok
  240. INSERT INTO small SELECT x, 2*x, 3*x, 4*x FROM
  241. generate_series(1, 10) AS a(x)
  242. statement ok
  243. INSERT INTO large SELECT x, 2*x, 3*x, 4*x FROM
  244. generate_series(1, 10) AS a(x)
  245. statement ok
  246. ALTER TABLE small INJECT STATISTICS '[
  247. {
  248. "columns": ["a"],
  249. "created_at": "2018-01-01 1:00:00.00000+00:00",
  250. "row_count": 100,
  251. "distinct_count": 100
  252. }
  253. ]'
  254. statement ok
  255. ALTER TABLE large INJECT STATISTICS '[
  256. {
  257. "columns": ["a"],
  258. "created_at": "2018-01-01 1:00:00.00000+00:00",
  259. "row_count": 10000,
  260. "distinct_count": 10000
  261. }
  262. ]'
  263. # Lookup join on covering secondary index
  264. query II rowsort
  265. SELECT small.a, large.c FROM small JOIN large ON small.a = large.b
  266. ----
  267. 2 3
  268. 4 6
  269. 6 9
  270. 8 12
  271. 10 15
  272. # Lookup join on non-covering secondary index
  273. query II rowsort
  274. SELECT small.a, large.d FROM small JOIN large ON small.a = large.b
  275. ----
  276. 2 4
  277. 4 8
  278. 6 12
  279. 8 16
  280. 10 20
  281. ############################
  282. # LEFT OUTER LOOKUP JOIN #
  283. ############################
  284. # Left join against primary index
  285. query II rowsort
  286. SELECT small.b, large.a FROM small LEFT JOIN large ON small.b = large.a
  287. ----
  288. 2 2
  289. 4 4
  290. 6 6
  291. 8 8
  292. 10 10
  293. 12 NULL
  294. 14 NULL
  295. 16 NULL
  296. 18 NULL
  297. 20 NULL
  298. # Left join should preserve input order.
  299. query II
  300. SELECT t1.a, t2.b FROM small t1 LEFT JOIN large t2 ON t1.a = t2.a AND t2.b % 6 = 0 ORDER BY t1.a
  301. ----
  302. 1 NULL
  303. 2 NULL
  304. 3 6
  305. 4 NULL
  306. 5 NULL
  307. 6 12
  308. 7 NULL
  309. 8 NULL
  310. 9 18
  311. 10 NULL
  312. # Left join against covering secondary index
  313. query II rowsort
  314. SELECT small.c, large.c FROM small LEFT JOIN large ON small.c = large.b
  315. ----
  316. 3 NULL
  317. 6 9
  318. 9 NULL
  319. 12 18
  320. 15 NULL
  321. 18 27
  322. 21 NULL
  323. 24 NULL
  324. 27 NULL
  325. 30 NULL
  326. # Left join against non-covering secondary index
  327. query II rowsort
  328. SELECT small.c, large.d FROM small LEFT JOIN large ON small.c = large.b
  329. ----
  330. 3 NULL
  331. 6 12
  332. 9 NULL
  333. 12 24
  334. 15 NULL
  335. 18 36
  336. 21 NULL
  337. 24 NULL
  338. 27 NULL
  339. 30 NULL
  340. # Left join with ON filter on covering index
  341. query II rowsort
  342. SELECT small.c, large.c FROM small LEFT JOIN large ON small.c = large.b AND large.c < 20
  343. ----
  344. 3 NULL
  345. 6 9
  346. 9 NULL
  347. 12 18
  348. 15 NULL
  349. 18 NULL
  350. 21 NULL
  351. 24 NULL
  352. 27 NULL
  353. 30 NULL
  354. ## Left join with ON filter on non-covering index
  355. query II rowsort
  356. SELECT small.c, large.d FROM small LEFT JOIN large ON small.c = large.b AND large.d < 30
  357. ----
  358. 3 NULL
  359. 6 12
  360. 9 NULL
  361. 12 24
  362. 15 NULL
  363. 18 NULL
  364. 21 NULL
  365. 24 NULL
  366. 27 NULL
  367. 30 NULL
  368. # Lookup joins against interleaved tables. Regression test for materialize#28981.
  369. # This is now tested more thoroughly by joinreader_test.go.
  370. statement ok
  371. CREATE TABLE parent (a INT, b INT, PRIMARY KEY(a, b))
  372. statement ok
  373. CREATE TABLE child (a INT, b INT, c INT, PRIMARY KEY(a, b, c)) INTERLEAVE IN PARENT parent(a, b)
  374. statement ok
  375. CREATE TABLE source (a INT)
  376. statement ok
  377. ALTER TABLE source INJECT STATISTICS '[
  378. {
  379. "columns": ["a"],
  380. "created_at": "2018-01-01 1:00:00.00000+00:00",
  381. "row_count": 1,
  382. "distinct_count": 1
  383. }
  384. ]'
  385. statement ok
  386. ALTER TABLE child INJECT STATISTICS '[
  387. {
  388. "columns": ["a", "b", "c"],
  389. "created_at": "2018-01-01 1:00:00.00000+00:00",
  390. "row_count": 10,
  391. "distinct_count": 10
  392. }
  393. ]'
  394. statement ok
  395. INSERT INTO child VALUES(1, 2, 3)
  396. statement ok
  397. INSERT INTO source VALUES(1)
  398. query IIII
  399. SELECT * FROM source JOIN child ON source.a = child.a
  400. ----
  401. 1 1 2 3
  402. ###########################################################
  403. # LOOKUP JOINS ON IMPLICIT INDEX KEY COLUMNS #
  404. # https://github.com/cockroachdb/cockroach/issues/31777 #
  405. ###########################################################
  406. statement ok
  407. CREATE TABLE t (a INT, b INT, c INT, d INT, e INT)
  408. statement ok
  409. CREATE TABLE u (a INT, b INT, c INT, d INT, e INT, PRIMARY KEY (a DESC, b, c))
  410. statement ok
  411. INSERT INTO t VALUES
  412. (1, 2, 3, 4, 5)
  413. statement ok
  414. INSERT INTO u VALUES
  415. (1, 2, 3, 4, 5),
  416. (2, 3, 4, 5, 6),
  417. (3, 4, 5, 6, 7)
  418. # Test index with all primary key columns implicit.
  419. statement ok
  420. CREATE INDEX idx ON u (d)
  421. query I
  422. SELECT u.a FROM t JOIN u ON t.d = u.d AND t.a = u.a WHERE t.e = 5
  423. ----
  424. 1
  425. # Test unique version of same index. (Lookup join should not use column a.)
  426. statement ok
  427. DROP INDEX u@idx
  428. statement ok
  429. CREATE UNIQUE INDEX idx ON u (d)
  430. query I
  431. SELECT u.a FROM t JOIN u ON t.d = u.d AND t.a = u.a WHERE t.e = 5
  432. ----
  433. 1
  434. # Test index with first primary key column explicit and the rest implicit.
  435. statement ok
  436. DROP INDEX u@idx CASCADE
  437. statement ok
  438. CREATE INDEX idx ON u (d, a)
  439. query I
  440. SELECT u.a FROM t JOIN u ON t.d = u.d AND t.a = u.a AND t.b = u.b WHERE t.e = 5
  441. ----
  442. 1
  443. # Test index with middle primary key column explicit and the rest implicit.
  444. statement ok
  445. DROP INDEX u@idx
  446. statement ok
  447. CREATE INDEX idx ON u (d, b)
  448. query I
  449. SELECT u.a FROM t JOIN u ON t.d = u.d AND t.a = u.a AND t.b = u.b WHERE t.e = 5
  450. ----
  451. 1
  452. # Test index with last primary key column explicit and the rest implicit.
  453. statement ok
  454. DROP INDEX u@idx
  455. statement ok
  456. CREATE INDEX idx ON u (d, c)
  457. query I
  458. SELECT u.a FROM t JOIN u ON t.d = u.d AND t.a = u.a AND t.d = u.d WHERE t.e = 5
  459. ----
  460. 1
  461. query IIIIII colnames,partialsort(4)
  462. SELECT * FROM def JOIN abc ON a=f ORDER BY a
  463. ----
  464. d e f a b c
  465. 2 1 1 1 1 2
  466. NULL 2 1 1 1 2
  467. 1 1 2 2 NULL 2
  468. 1 1 2 2 1 1