upsert.slt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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/upsert
  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. # not supported yet
  20. halt
  21. mode cockroach
  22. subtest strict
  23. statement ok
  24. CREATE TABLE ex(
  25. foo INT PRIMARY KEY,
  26. bar INT UNIQUE,
  27. baz INT
  28. )
  29. statement count 1
  30. INSERT INTO ex(foo,bar,baz) VALUES (1,1,1)
  31. statement count 0
  32. INSERT INTO ex(foo,bar,baz) VALUES (1,1,1) ON CONFLICT DO NOTHING
  33. statement count 0
  34. INSERT INTO ex(foo,bar,baz) VALUES (2,1,1) ON CONFLICT DO NOTHING
  35. # Do not insert conflicting first and last rows.
  36. statement count 2
  37. INSERT INTO ex(foo,bar,baz) VALUES (1,2,1), (3,2,2), (6,6,2), (2,1,1) ON CONFLICT DO NOTHING
  38. query III colnames
  39. SELECT * from ex ORDER BY foo
  40. ----
  41. foo bar baz
  42. 1 1 1
  43. 3 2 2
  44. 6 6 2
  45. query III colnames
  46. INSERT INTO ex(foo,bar,baz) VALUES (4,3,1), (5,2,1) ON CONFLICT DO NOTHING RETURNING *
  47. ----
  48. foo bar baz
  49. 4 3 1
  50. statement ok
  51. CREATE TABLE ex2(
  52. a INT PRIMARY KEY,
  53. b INT UNIQUE,
  54. c INT,
  55. d INT,
  56. e INT,
  57. UNIQUE (c,d)
  58. )
  59. statement count 1
  60. INSERT INTO ex2(a,b,c,d,e) VALUES (0,0,0,0,0)
  61. statement count 0
  62. INSERT INTO ex2(a,b,c,d,e) VALUES (1,0,1,1,0), (2,4,0,0,5) ON CONFLICT DO NOTHING
  63. statement count 3
  64. INSERT INTO ex2(a,b,c,d,e) VALUES (3,4,5,6,7), (8,9,10,11,12), (13,14,15,16,17) ON CONFLICT DO NOTHING
  65. statement count 0
  66. INSERT INTO ex2(a,b,c,d,e) VALUES (3,4,5,6,7), (8,9,10,11,12) ON CONFLICT DO NOTHING
  67. statement ok
  68. CREATE TABLE no_unique(
  69. a INT,
  70. b INT
  71. )
  72. statement count 1
  73. INSERT INTO no_unique(a,b) VALUES (1,2)
  74. statement count 1
  75. INSERT INTO no_unique(a,b) VALUES (1,2) ON CONFLICT DO NOTHING
  76. statement count 3
  77. INSERT INTO no_unique(a,b) VALUES (1,2), (1,3), (3,2) ON CONFLICT DO NOTHING
  78. query II colnames
  79. SELECT * from no_unique ORDER BY a, b
  80. ----
  81. a b
  82. 1 2
  83. 1 2
  84. 1 2
  85. 1 3
  86. 3 2
  87. statement count 3
  88. INSERT INTO no_unique(a,b) VALUES (1,2), (1,2), (1,2) ON CONFLICT DO NOTHING
  89. subtest notstrict
  90. statement ok
  91. CREATE TABLE kv (
  92. k INT PRIMARY KEY,
  93. v INT
  94. )
  95. statement count 3
  96. INSERT INTO kv VALUES (1, 1), (2, 2), (3, 3) ON CONFLICT (k) DO UPDATE SET v = excluded.v
  97. query II
  98. SELECT * FROM kv ORDER BY (k, v)
  99. ----
  100. 1 1
  101. 2 2
  102. 3 3
  103. statement error multiple assignments to the same column
  104. INSERT INTO kv VALUES (4, 4), (2, 5), (6, 6) ON CONFLICT (k) DO UPDATE SET v = 1, v = 1
  105. statement count 3
  106. INSERT INTO kv VALUES (4, 4), (2, 5), (6, 6) ON CONFLICT (k) DO UPDATE SET v = excluded.v
  107. statement count 3
  108. UPSERT INTO kv VALUES (7, 7), (3, 8), (9, 9)
  109. statement count 1
  110. INSERT INTO kv VALUES (1, 10) ON CONFLICT (k) DO UPDATE SET v = (SELECT CAST(sum(k) AS INT) FROM kv)
  111. statement error column reference "v" is ambiguous \(candidates: excluded.v, kv.v\)
  112. INSERT INTO kv VALUES (4, 10) ON CONFLICT (k) DO UPDATE SET v = v + 1
  113. statement count 1
  114. INSERT INTO kv VALUES (4, 10) ON CONFLICT (k) DO UPDATE SET v = kv.v + 20
  115. statement error there is no unique or exclusion constraint matching the ON CONFLICT specification
  116. INSERT INTO kv VALUES (4, 10) ON CONFLICT DO UPDATE SET v = kv.v + 20
  117. statement error duplicate key value \(k\)=\(3\) violates unique constraint "primary"
  118. INSERT INTO kv VALUES (2, 10) ON CONFLICT (k) DO UPDATE SET k = 3, v = 10
  119. statement count 1
  120. INSERT INTO kv VALUES (9, 9) ON CONFLICT (k) DO UPDATE SET (k, v) = (excluded.k + 2, excluded.v + 3)
  121. statement count 1
  122. UPSERT INTO kv VALUES (10, 10)
  123. statement count 2
  124. UPSERT INTO kv VALUES (10, 11), (10, 12)
  125. query II rowsort
  126. UPSERT INTO kv VALUES (11, 11), (10, 13) RETURNING k, v
  127. ----
  128. 11 11
  129. 10 13
  130. query I
  131. UPSERT INTO kv VALUES (11) RETURNING k
  132. ----
  133. 11
  134. query I
  135. UPSERT INTO kv VALUES (11, 12) RETURNING v
  136. ----
  137. 12
  138. statement count 1
  139. INSERT INTO kv VALUES (13, 13), (7, 8) ON CONFLICT (k) DO NOTHING RETURNING *
  140. statement count 0
  141. INSERT INTO kv VALUES (13, 13), (7, 8) ON CONFLICT DO NOTHING
  142. statement count 2
  143. INSERT INTO kv VALUES (14, 14), (13, 15) ON CONFLICT (k) DO UPDATE SET v = excluded.v + 1
  144. statement count 2
  145. INSERT INTO kv VALUES (15, 15), (14, 16) ON CONFLICT (k) DO UPDATE SET k = excluded.k * 10
  146. statement count 2
  147. INSERT INTO kv VALUES (16, 16), (15, 17) ON CONFLICT (k) DO UPDATE SET k = excluded.k * 10, v = excluded.v
  148. query II
  149. SELECT * FROM kv ORDER BY (k, v)
  150. ----
  151. 1 32
  152. 2 5
  153. 3 8
  154. 4 24
  155. 6 6
  156. 7 7
  157. 10 13
  158. 11 12
  159. 13 16
  160. 16 16
  161. 140 14
  162. 150 17
  163. # TODO(knz): Enable the 1st statement and remove the 2nd once cockroach#33313 is fixed.
  164. #query II rowsort
  165. #UPSERT INTO kv(k) VALUES (6), (8) RETURNING k,v
  166. #----
  167. #6 6
  168. #8 NULL
  169. query II rowsort
  170. UPSERT INTO kv(k) VALUES (8) RETURNING k,v
  171. ----
  172. 8 NULL
  173. query II rowsort
  174. INSERT INTO kv VALUES (10, 10), (11, 11) ON CONFLICT (k) DO UPDATE SET v = excluded.v RETURNING *
  175. ----
  176. 10 10
  177. 11 11
  178. query II rowsort
  179. INSERT INTO kv VALUES (10, 2), (11, 3) ON CONFLICT (k) DO UPDATE SET v = excluded.v + kv.v RETURNING *
  180. ----
  181. 10 12
  182. 11 14
  183. query II rowsort
  184. INSERT INTO kv VALUES (10, 14), (15, 15) ON CONFLICT (k) DO NOTHING RETURNING *
  185. ----
  186. 15 15
  187. statement ok
  188. CREATE TABLE abc (
  189. a INT,
  190. b INT,
  191. c INT DEFAULT 7,
  192. PRIMARY KEY (a, b),
  193. INDEX y (b),
  194. UNIQUE INDEX z (c)
  195. )
  196. statement error missing "b" primary key column
  197. UPSERT INTO abc (a, c) VALUES (1, 1)
  198. statement error missing "a" primary key column
  199. UPSERT INTO abc (b, c) VALUES (1, 1)
  200. statement count 1
  201. INSERT INTO abc VALUES (1, 2, 3)
  202. statement count 1
  203. INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT (c) DO UPDATE SET a = 4
  204. query III
  205. SELECT * FROM abc
  206. ----
  207. 4 2 3
  208. statement count 1
  209. INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT (c) DO UPDATE SET b = 5
  210. statement count 1
  211. INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT (c) DO UPDATE SET c = 6
  212. query III
  213. SELECT * FROM abc
  214. ----
  215. 4 5 6
  216. statement count 1
  217. INSERT INTO abc (a, b) VALUES (1, 2) ON CONFLICT (a, b) DO UPDATE SET a = 1, b = 2
  218. statement count 1
  219. INSERT INTO abc (a, b) VALUES (4, 5) ON CONFLICT (a, b) DO UPDATE SET a = 7, b = 8
  220. query III
  221. SELECT * FROM abc ORDER BY (a, b, c)
  222. ----
  223. 1 2 7
  224. 7 8 6
  225. statement count 1
  226. DELETE FROM abc where a = 1
  227. statement count 1
  228. UPSERT INTO abc VALUES (1, 2)
  229. query III
  230. SELECT * FROM abc ORDER BY (a, b, c)
  231. ----
  232. 1 2 7
  233. 7 8 6
  234. statement count 1
  235. UPSERT INTO abc VALUES (1, 2, 5)
  236. query III
  237. SELECT * FROM abc ORDER BY (a, b, c)
  238. ----
  239. 1 2 5
  240. 7 8 6
  241. statement count 1
  242. UPSERT INTO abc VALUES (1, 2)
  243. query III
  244. SELECT * FROM abc ORDER BY (a, b, c)
  245. ----
  246. 1 2 7
  247. 7 8 6
  248. statement count 1
  249. DELETE FROM abc where a = 1
  250. statement count 1
  251. INSERT INTO abc VALUES (7, 8, 9) ON CONFLICT (a, b) DO UPDATE SET c = DEFAULT
  252. query III
  253. SELECT * FROM abc ORDER BY (a, b, c)
  254. ----
  255. 7 8 7
  256. statement ok
  257. CREATE TABLE excluded (a INT PRIMARY KEY, b INT)
  258. statement error ambiguous source name: "excluded"
  259. INSERT INTO excluded VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b
  260. # Tests for upsert/on conflict returning
  261. statement ok
  262. CREATE TABLE upsert_returning (a INT PRIMARY KEY, b INT, c INT, d INT DEFAULT -1)
  263. statement count 1
  264. INSERT INTO upsert_returning VALUES (1, 1, NULL)
  265. # Handle INSERT ... ON CONFLICT ... RETURNING
  266. query IIII rowsort
  267. INSERT INTO upsert_returning (a, c) VALUES (1, 1), (2, 2) ON CONFLICT (a) DO UPDATE SET c = excluded.c RETURNING *
  268. ----
  269. 1 1 1 -1
  270. 2 NULL 2 -1
  271. # Handle INSERT ... ON CONFLICT DO NOTHING ... RETURNING
  272. query IIII
  273. INSERT INTO upsert_returning (a, c) VALUES (1, 1), (3, 3) ON CONFLICT (a) DO NOTHING RETURNING *
  274. ----
  275. 3 NULL 3 -1
  276. # Handle UPSERT ... RETURNING
  277. query IIII rowsort
  278. UPSERT INTO upsert_returning (a, c) VALUES (1, 10), (3, 30) RETURNING *
  279. ----
  280. 1 1 10 -1
  281. 3 NULL 30 -1
  282. # Ensure returned values are inserted values after conflict resolution
  283. query I
  284. SELECT b FROM upsert_returning WHERE a = 1
  285. ----
  286. 1
  287. query I
  288. INSERT INTO upsert_returning (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b + upsert_returning.b + 1 RETURNING b
  289. ----
  290. 3
  291. # Handle expressions within returning clause
  292. query I rowsort
  293. UPSERT INTO upsert_returning (a, b) VALUES (1, 2), (2, 3), (4, 3) RETURNING a+b+d
  294. ----
  295. 2
  296. 4
  297. 6
  298. # Handle upsert fast path with autocommit
  299. query IIII rowsort
  300. UPSERT INTO upsert_returning VALUES (1, 2, 3, 4), (5, 6, 7, 8) RETURNING *
  301. ----
  302. 1 2 3 4
  303. 5 6 7 8
  304. # Handle upsert fast path without autocommit
  305. statement ok
  306. BEGIN
  307. query IIII rowsort
  308. upsert INTO upsert_returning VALUES (1, 5, 4, 3), (6, 5, 4, 3) RETURNING *
  309. ----
  310. 1 5 4 3
  311. 6 5 4 3
  312. statement ok
  313. COMMIT
  314. # For materialize#22300. Test UPSERT ... RETURNING with UNION.
  315. query I rowsort
  316. SELECT a FROM [UPSERT INTO upsert_returning VALUES (7) RETURNING a] UNION VALUES (8)
  317. ----
  318. 7
  319. 8
  320. # For materialize#6710. Add an unused column to disable the fast path which doesn't have this bug.
  321. statement ok
  322. CREATE TABLE issue_6710 (a INT PRIMARY KEY, b STRING, c INT)
  323. statement count 2
  324. INSERT INTO issue_6710 (a, b) VALUES (1, 'foo'), (2, 'bar')
  325. statement count 2
  326. UPSERT INTO issue_6710 (a, b) VALUES (1, 'test1'), (2, 'test2')
  327. query IT rowsort
  328. SELECT a, b from issue_6710
  329. ----
  330. 1 test1
  331. 2 test2
  332. statement ok
  333. CREATE TABLE issue_13962 (a INT PRIMARY KEY, b INT, c INT)
  334. statement count 1
  335. INSERT INTO issue_13962 VALUES (1, 1, 1)
  336. statement count 1
  337. INSERT INTO issue_13962 VALUES (1, 2, 2) ON CONFLICT (a) DO UPDATE SET b = excluded.b
  338. query III
  339. SELECT * FROM issue_13962
  340. ----
  341. 1 2 1
  342. statement ok
  343. CREATE TABLE issue_14052 (a INT PRIMARY KEY, b INT, c INT)
  344. statement count 2
  345. INSERT INTO issue_14052 (a, b) VALUES (1, 1), (2, 2)
  346. statement count 2
  347. UPSERT INTO issue_14052 (a, c) (SELECT a, b from issue_14052)
  348. statement ok
  349. CREATE TABLE issue_14052_2 (
  350. id SERIAL PRIMARY KEY,
  351. name VARCHAR(255),
  352. createdAt INT,
  353. updatedAt INT
  354. )
  355. statement count 1
  356. INSERT INTO issue_14052_2 (id, name, createdAt, updatedAt) VALUES
  357. (1, 'original', 1, 1)
  358. # Make sure the fast path isn't taken (createdAt is not in the ON CONFLICT clause)
  359. statement count 1
  360. INSERT INTO issue_14052_2 (id, name, createdAt, updatedAt) VALUES
  361. (1, 'UPDATED', 2, 2)
  362. ON CONFLICT (id) DO UPDATE
  363. SET id = excluded.id, name = excluded.name, updatedAt = excluded.updatedAt
  364. query ITII
  365. SELECT * FROM issue_14052_2;
  366. ----
  367. 1 UPDATED 1 2
  368. statement error multiple assignments to the same column
  369. INSERT INTO issue_14052_2 (id, name, createdAt, updatedAt) VALUES
  370. (1, 'FOO', 3, 3)
  371. ON CONFLICT (id) DO UPDATE
  372. SET id = excluded.id, name = excluded.name, name = excluded.name, name = excluded.name
  373. # Make sure the fast path isn't taken (all clauses in the set must be of the form x = excluded.x)
  374. statement count 1
  375. INSERT INTO issue_14052_2 (id, name, createdAt, updatedAt) VALUES
  376. (1, 'BAR', 4, 5)
  377. ON CONFLICT (id) DO UPDATE
  378. SET name = excluded.name, createdAt = excluded.updatedAt, updatedAt = excluded.updatedAt
  379. query ITII
  380. SELECT * FROM issue_14052_2;
  381. ----
  382. 1 BAR 5 5
  383. # Make sure the column types are propagated when type checking the ON CONFLICT
  384. # expressions. See materialize#16873.
  385. statement ok
  386. CREATE TABLE issue_16873 (col int PRIMARY KEY, date TIMESTAMP);
  387. # n.b. the fully-qualified names below are required, as there are two providers of
  388. # the column named `col` here, the original table and the `excluded` pseudo-table.
  389. statement count 1
  390. INSERT INTO issue_16873 VALUES (1,clock_timestamp())
  391. ON CONFLICT (col) DO UPDATE SET date = clock_timestamp() WHERE issue_16873.col = 1;
  392. statement count 1
  393. INSERT INTO issue_16873 VALUES (1,clock_timestamp())
  394. ON CONFLICT (col) DO UPDATE SET date = clock_timestamp() WHERE issue_16873.col = 1;
  395. # For materialize#17339. Support WHERE clause in ON CONFLICT handling.
  396. statement ok
  397. CREATE TABLE issue_17339 (a int primary key, b int);
  398. statement count 2
  399. INSERT INTO issue_17339 VALUES (1, 1), (2, 0);
  400. statement count 1
  401. INSERT INTO issue_17339 VALUES (1, 0), (2, 2)
  402. ON CONFLICT (a) DO UPDATE SET b = excluded.b WHERE excluded.b > issue_17339.b;
  403. query II
  404. SELECT * FROM issue_17339 ORDER BY a;
  405. ----
  406. 1 1
  407. 2 2
  408. statement count 2
  409. INSERT INTO issue_17339 VALUES (1, 0), (2, 1)
  410. ON CONFLICT (a) DO UPDATE SET b = excluded.b WHERE TRUE;
  411. query II
  412. SELECT * FROM issue_17339 ORDER BY a;
  413. ----
  414. 1 0
  415. 2 1
  416. # Regression test for materialize#25726.
  417. # UPSERT over tables with column families, on the fast path, use the
  418. # INSERT logic. This has special casing for column families of 1
  419. # column, and another special casing for column families of 2+
  420. # columns. The special casing is only for families that do not include
  421. # the primary key. So we need a table with 3 families: 1 for the PK, 1
  422. # with just 1 col, and 1 with 2+ cols.
  423. statement ok
  424. CREATE TABLE tu (a INT PRIMARY KEY, b INT, c INT, d INT, FAMILY (a), FAMILY (b), FAMILY (c,d));
  425. INSERT INTO tu VALUES (1, 2, 3, 4)
  426. statement ok
  427. UPSERT INTO tu VALUES (1, NULL, NULL, NULL)
  428. query IIII rowsort
  429. SELECT * FROM tu
  430. ----
  431. 1 NULL NULL NULL
  432. subtest check
  433. statement ok
  434. CREATE TABLE ab(
  435. a INT PRIMARY KEY,
  436. b INT, CHECK (b < 1)
  437. )
  438. statement count 1
  439. INSERT INTO ab(a, b) VALUES (1, 0);
  440. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  441. INSERT INTO ab(a, b) VALUES (1, 0) ON CONFLICT(a) DO UPDATE SET b=12312313;
  442. statement count 1
  443. INSERT INTO ab(a, b) VALUES (1, 0) ON CONFLICT(a) DO UPDATE SET b=-1;
  444. statement ok
  445. CREATE TABLE abc_check(
  446. a INT PRIMARY KEY,
  447. b INT,
  448. c INT,
  449. CHECK (b < 1),
  450. CHECK (c > 1)
  451. )
  452. statement count 1
  453. INSERT INTO abc_check(a, b, c) VALUES (1, 0, 2);
  454. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  455. INSERT INTO abc_check(a, b, c) VALUES (1, 0, 2) ON CONFLICT(a) DO UPDATE SET b=12312313;
  456. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  457. INSERT INTO abc_check(a, b, c) VALUES (1, 0, 2) ON CONFLICT(a) DO UPDATE SET (b, c) = (1, 1);
  458. statement error pq: failed to satisfy CHECK constraint \(c > 1\)
  459. INSERT INTO abc_check(a, b, c) VALUES (1, 0, 2) ON CONFLICT(a) DO UPDATE SET (b, c) = (-1, 1);
  460. statement count 1
  461. INSERT INTO abc_check(a, b, c) VALUES (2, 0, 3);
  462. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  463. INSERT INTO abc_check(c, a, b) VALUES (3, 2, 0) ON CONFLICT(a) DO UPDATE SET b=12312313;
  464. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  465. INSERT INTO abc_check(a, c) VALUES (2, 3) ON CONFLICT(a) DO UPDATE SET b=12312313;
  466. statement error pq: failed to satisfy CHECK constraint \(c > 1\)
  467. INSERT INTO abc_check(a, c) VALUES (2, 3) ON CONFLICT(a) DO UPDATE SET c=1;
  468. statement error pq: failed to satisfy CHECK constraint \(c > 1\)
  469. INSERT INTO abc_check(c, a) VALUES (3, 2) ON CONFLICT(a) DO UPDATE SET c=1;
  470. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  471. INSERT INTO abc_check(c, a) VALUES (3, 2) ON CONFLICT(a) DO UPDATE SET b=123123123;
  472. statement error pq: failed to satisfy CHECK constraint \(b < 1\)
  473. INSERT INTO abc_check(c, a) VALUES (3, 2) ON CONFLICT(a) DO UPDATE SET b=123123123;
  474. subtest 29495
  475. statement ok
  476. CREATE TABLE IF NOT EXISTS example (
  477. id SERIAL PRIMARY KEY
  478. ,value string NOT NULL
  479. );
  480. query B
  481. UPSERT INTO example (value) VALUES ('foo') RETURNING id > 0
  482. ----
  483. true
  484. statement ok
  485. DROP TABLE example
  486. subtest contraint_check_validation_ordering
  487. # Verification of column constraints vs CHECK handling. The column
  488. # constraint verification must take place first.
  489. #
  490. # This test requires that the error message for a CHECK constraint
  491. # validation error be different than a column validation error. So we
  492. # test the former first, as a sanity check.
  493. statement ok
  494. CREATE TABLE tn(x INT NULL CHECK(x IS NOT NULL), y CHAR(4) CHECK(length(y) < 4));
  495. statement error failed to satisfy CHECK constraint
  496. UPSERT INTO tn(x) VALUES (NULL)
  497. statement error failed to satisfy CHECK constraint
  498. UPSERT INTO tn(y) VALUES ('abcd')
  499. # Now we test that the column validation occurs before the CHECK constraint.
  500. statement ok
  501. CREATE TABLE tn2(x INT NOT NULL CHECK(x IS NOT NULL), y CHAR(3) CHECK(length(y) < 4));
  502. statement error null value in column "x" violates not-null constraint
  503. UPSERT INTO tn2(x) VALUES (NULL)
  504. statement error value too long for type CHAR\(3\)
  505. UPSERT INTO tn2(x, y) VALUES (123, 'abcd')
  506. subtest regression_29494
  507. statement ok
  508. CREATE TABLE t29494(x INT); INSERT INTO t29494 VALUES (12)
  509. statement ok
  510. BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
  511. # Check that the new column is not visible
  512. query T
  513. SELECT create_statement FROM [SHOW CREATE t29494]
  514. ----
  515. CREATE TABLE t29494 (
  516. x INT8 NULL,
  517. FAMILY "primary" (x, rowid)
  518. )
  519. # Check that the new column is not usable in RETURNING
  520. statement error column "y" does not exist
  521. UPSERT INTO t29494(x) VALUES (123) RETURNING y
  522. # Ditto for INSERT ON CONFLICT
  523. statement ok
  524. ROLLBACK; BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
  525. statement error column "y" does not exist
  526. INSERT INTO t29494(x) VALUES (123) ON CONFLICT(rowid) DO UPDATE SET x = 400 RETURNING y
  527. statement ok
  528. ROLLBACK
  529. statement ok
  530. BEGIN; ALTER TABLE t29494 ADD COLUMN y INT NOT NULL DEFAULT 123
  531. query I
  532. UPSERT INTO t29494(x) VALUES (12) RETURNING *
  533. ----
  534. 12
  535. query I
  536. UPSERT INTO t29494(x) VALUES (123) RETURNING *
  537. ----
  538. 123
  539. query I
  540. INSERT INTO t29494(x) VALUES (123) ON CONFLICT(rowid) DO UPDATE SET x = 400 RETURNING *
  541. ----
  542. 123
  543. statement ok
  544. COMMIT
  545. subtest regression_31255
  546. statement ok
  547. CREATE TABLE tc(x INT PRIMARY KEY, y INT AS (x+1) STORED)
  548. statement error cannot write directly to computed column "y"
  549. INSERT INTO tc(x) VALUES (1) ON CONFLICT(x) DO UPDATE SET y = 123
  550. statement error cannot write directly to computed column "y"
  551. UPSERT INTO tc(x,y) VALUES (1,2)
  552. statement error cannot write directly to computed column "y"
  553. UPSERT INTO tc VALUES (1,2)
  554. subtest regression_29497
  555. statement ok
  556. CREATE TABLE t29497(x INT PRIMARY KEY); BEGIN; ALTER TABLE t29497 ADD COLUMN y INT NOT NULL DEFAULT 123
  557. statement error [UPSERT has more expressions than target columns | column "y" is being backfilled]
  558. UPSERT INTO t29497 VALUES (1, 2)
  559. statement ok
  560. ROLLBACK; BEGIN; ALTER TABLE t29497 ADD COLUMN y INT NOT NULL DEFAULT 123
  561. statement error [column "y" does not exist | column "y" is being backfilled]
  562. INSERT INTO t29497(x) VALUES (1) ON CONFLICT (x) DO UPDATE SET y = 456
  563. statement ok
  564. ROLLBACK
  565. subtest visible_returning_columns
  566. statement ok
  567. BEGIN; ALTER TABLE tc DROP COLUMN y
  568. query I colnames,rowsort
  569. UPSERT INTO tc VALUES (1), (2) RETURNING *
  570. ----
  571. x
  572. 1
  573. 2
  574. statement ok
  575. COMMIT
  576. subtest regression_32762
  577. statement ok
  578. CREATE TABLE t32762(x INT, y INT, UNIQUE (x,y), CONSTRAINT y_not_null CHECK (y IS NOT NULL))
  579. statement ok
  580. INSERT INTO t32762(x,y) VALUES (1,2) ON CONFLICT (x,y) DO UPDATE SET x = t32762.x;
  581. statement ok
  582. INSERT INTO t32762(x,y) VALUES (1,2) ON CONFLICT (x,y) DO UPDATE SET x = t32762.x
  583. subtest regression_33313
  584. statement ok
  585. CREATE TABLE ex33313(foo INT PRIMARY KEY, bar INT UNIQUE, baz INT);
  586. INSERT INTO ex33313 VALUES (1,1,1);
  587. statement count 1
  588. INSERT INTO ex33313(foo,bar,baz) VALUES (1,2,1), (3,2,2) ON CONFLICT DO NOTHING;
  589. query III colnames
  590. SELECT * FROM ex33313 ORDER BY foo
  591. ----
  592. foo bar baz
  593. 1 1 1
  594. 3 2 2
  595. # Use Upsert with indexed table, default columns, computed columns, and check
  596. # columns.
  597. statement ok
  598. CREATE TABLE indexed (
  599. a DECIMAL PRIMARY KEY,
  600. b DECIMAL,
  601. c DECIMAL DEFAULT(10.0),
  602. d DECIMAL AS (a + c) STORED,
  603. UNIQUE INDEX secondary (d, b),
  604. CHECK (c > 0)
  605. )
  606. statement ok
  607. INSERT INTO indexed VALUES (1, 1, 1); INSERT INTO indexed VALUES (2, 2, 2)
  608. # Use implicit target columns (should set default and computed values).
  609. statement ok
  610. UPSERT INTO indexed VALUES (1.0)
  611. query TTTT colnames
  612. SELECT * FROM indexed@secondary ORDER BY d, b
  613. ----
  614. a b c d
  615. 2 2 2 4
  616. 1 NULL 10.0 11.0
  617. # Explicitly specify all target columns. Ensure that primary key is not updated,
  618. # even though an alternate but equal decimal form is in use (1.0 vs. 1).
  619. statement ok
  620. UPSERT INTO indexed (a, b, c) VALUES (1.0, 1.0, 1.0)
  621. query TTTT colnames
  622. SELECT * FROM indexed@secondary ORDER BY d, b
  623. ----
  624. a b c d
  625. 1 1.0 1.0 2.0
  626. 2 2 2 4
  627. # Ensure that explicit target column does not disturb existing "b" value, but
  628. # does update the computed column.
  629. statement ok
  630. UPSERT INTO indexed (c, a) VALUES (2, 1)
  631. query TTTT colnames
  632. SELECT * FROM indexed@secondary ORDER BY d, b
  633. ----
  634. a b c d
  635. 1 1.0 2 3
  636. 2 2 2 4
  637. # Final check to ensure that primary index is correct.
  638. query TTTT colnames
  639. SELECT * FROM indexed@primary ORDER BY a
  640. ----
  641. a b c d
  642. 1 1.0 2 3
  643. 2 2 2 4
  644. # Drop the secondary index, allowing the "blind upsert" path to run.
  645. statement ok
  646. DROP INDEX indexed@secondary CASCADE
  647. # Use implicit target columns (should set default and computed values).
  648. statement ok
  649. UPSERT INTO indexed VALUES (1, 1)
  650. query TTTT colnames,rowsort
  651. SELECT * FROM indexed
  652. ----
  653. a b c d
  654. 1 1 10.0 11.0
  655. 2 2 2 4
  656. # Explicitly specify all target columns.
  657. statement ok
  658. UPSERT INTO indexed (a, b, c) SELECT 1, 2, 3
  659. query TTTT colnames,rowsort
  660. SELECT * FROM indexed
  661. ----
  662. a b c d
  663. 2 2 2 4
  664. 1 2 3 4
  665. # Ensure that explicit target column does not disturb existing "b" value, but
  666. # does update the computed column.
  667. query TTTT
  668. UPSERT INTO indexed (c, a) VALUES (2.0, 1.0) RETURNING *
  669. ----
  670. 1 2 2.0 3.0
  671. query TTTT colnames,rowsort
  672. SELECT * FROM indexed
  673. ----
  674. a b c d
  675. 1 2 2.0 3.0
  676. 2 2 2 4
  677. statement ok
  678. DROP TABLE indexed
  679. subtest regression_35040
  680. statement ok
  681. CREATE TABLE test35040(a INT PRIMARY KEY, b INT NOT NULL, c INT2)
  682. statement ok
  683. INSERT INTO test35040(a,b) VALUES(0,0) ON CONFLICT(a) DO UPDATE SET b = NULL
  684. statement error null value in column "b" violates not-null constraint
  685. INSERT INTO test35040(a,b) VALUES(0,0) ON CONFLICT(a) DO UPDATE SET b = NULL
  686. statement error integer out of range for type int2
  687. INSERT INTO test35040(a,b) VALUES (0,1) ON CONFLICT(a) DO UPDATE SET c = 111111111;
  688. statement ok
  689. DROP TABLE test35040
  690. # ------------------------------------------------------------------------------
  691. # Regression for cockroach#35364.
  692. # ------------------------------------------------------------------------------
  693. subtest regression_35364
  694. statement ok
  695. CREATE TABLE t35364(x INT PRIMARY KEY, y DECIMAL(10,1) CHECK(y >= 8.0), UNIQUE INDEX (y))
  696. statement ok
  697. INSERT INTO t35364(x, y) VALUES (1, 10.2)
  698. # 10.18 should be mapped to 10.2 before the left outer join so that the conflict
  699. # can be detected, and 7.95 should be mapped to 8.0 so that check constraint
  700. # will pass.
  701. statement ok
  702. INSERT INTO t35364(x, y) VALUES (2, 10.18) ON CONFLICT (y) DO UPDATE SET y=7.95
  703. query IT
  704. SELECT * FROM t35364
  705. ----
  706. 1 8.0
  707. statement ok
  708. DROP TABLE t35364
  709. # Check UPSERT syntax.
  710. statement ok
  711. CREATE TABLE t35364(
  712. x DECIMAL(10,0) CHECK (x >= 0) PRIMARY KEY,
  713. y DECIMAL(10,0) CHECK (y >= 0)
  714. )
  715. statement ok
  716. UPSERT INTO t35364 (x) VALUES (-0.1)
  717. query TT
  718. SELECT * FROM t35364
  719. ----
  720. -0 NULL
  721. statement ok
  722. UPSERT INTO t35364 (x, y) VALUES (-0.2, -0.3)
  723. query TT
  724. SELECT * FROM t35364
  725. ----
  726. -0 -0
  727. statement ok
  728. UPSERT INTO t35364 (x, y) VALUES (1.5, 2.5)
  729. query TT rowsort
  730. SELECT * FROM t35364
  731. ----
  732. -0 -0
  733. 2 3
  734. statement ok
  735. INSERT INTO t35364 (x) VALUES (1.5) ON CONFLICT (x) DO UPDATE SET x=2.5, y=3.5
  736. query TT rowsort
  737. SELECT * FROM t35364
  738. ----
  739. -0 -0
  740. 3 4
  741. # ------------------------------------------------------------------------------
  742. # Regression for cockroach#35970.
  743. # ------------------------------------------------------------------------------
  744. statement ok
  745. CREATE TABLE table35970 (
  746. a DECIMAL(10,1) PRIMARY KEY,
  747. b DECIMAL(10,1),
  748. c DECIMAL(10,0),
  749. FAMILY fam0 (a, b),
  750. FAMILY fam1 (c)
  751. )
  752. query I
  753. UPSERT INTO table35970 (a) VALUES (1.5) RETURNING b
  754. ----
  755. NULL
  756. query I
  757. INSERT INTO table35970 VALUES (1.5, 1.5, NULL)
  758. ON CONFLICT (a)
  759. DO UPDATE SET c = table35970.a+1
  760. RETURNING b
  761. ----
  762. NULL