github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/logictest/testdata/logic_test/computed (about) 1 # LogicTest: !3node-tenant 2 statement ok 3 CREATE TABLE with_no_column_refs ( 4 a INT, 5 b INT, 6 c INT AS (3) STORED, 7 FAMILY "primary" (a, b, c, rowid) 8 ) 9 10 query TT 11 SHOW CREATE TABLE with_no_column_refs 12 ---- 13 with_no_column_refs CREATE TABLE with_no_column_refs ( 14 a INT8 NULL, 15 b INT8 NULL, 16 c INT8 NULL AS (3:::INT8) STORED, 17 FAMILY "primary" (a, b, c, rowid) 18 ) 19 20 statement ok 21 CREATE TABLE extra_parens ( 22 a INT, 23 b INT, 24 c INT AS ((3)) STORED, 25 FAMILY "primary" (a, b, c, rowid) 26 ) 27 28 query TT 29 SHOW CREATE TABLE extra_parens 30 ---- 31 extra_parens CREATE TABLE extra_parens ( 32 a INT8 NULL, 33 b INT8 NULL, 34 c INT8 NULL AS (3:::INT8) STORED, 35 FAMILY "primary" (a, b, c, rowid) 36 ) 37 38 39 statement error cannot write directly to computed column "c" 40 INSERT INTO with_no_column_refs VALUES (1, 2, 3) 41 42 statement error cannot write directly to computed column "c" 43 INSERT INTO with_no_column_refs (SELECT 1, 2, 3) 44 45 statement error cannot write directly to computed column "c" 46 INSERT INTO with_no_column_refs (a, c) (SELECT 1, 3) 47 48 statement error cannot write directly to computed column "c" 49 INSERT INTO with_no_column_refs (c) VALUES (1) 50 51 statement ok 52 INSERT INTO with_no_column_refs (a, b) VALUES (1, 2) 53 54 statement ok 55 INSERT INTO with_no_column_refs VALUES (1, 2) 56 57 statement error cannot write directly to computed column "c" 58 UPDATE with_no_column_refs SET c = 1 59 60 statement error cannot write directly to computed column "c" 61 UPDATE with_no_column_refs SET (a, b, c) = (1, 2, 3) 62 63 statement error cannot write directly to computed column "c" 64 UPDATE with_no_column_refs SET (a, b, c) = (SELECT 1, 2, 3) 65 66 query I 67 SELECT c FROM with_no_column_refs 68 ---- 69 3 70 3 71 72 statement ok 73 CREATE TABLE x ( 74 a INT DEFAULT 3, 75 b INT DEFAULT 7, 76 c INT AS (a) STORED, 77 d INT AS (a + b) STORED, 78 FAMILY "primary" (a, b, c, d, rowid) 79 ) 80 81 query TT 82 SHOW CREATE TABLE x 83 ---- 84 x CREATE TABLE x ( 85 a INT8 NULL DEFAULT 3:::INT8, 86 b INT8 NULL DEFAULT 7:::INT8, 87 c INT8 NULL AS (a) STORED, 88 d INT8 NULL AS (a + b) STORED, 89 FAMILY "primary" (a, b, c, d, rowid) 90 ) 91 92 query TTBTTTB colnames 93 SHOW COLUMNS FROM x 94 ---- 95 column_name data_type is_nullable column_default generation_expression indices is_hidden 96 a INT8 true 3:::INT8 · {} false 97 b INT8 true 7:::INT8 · {} false 98 c INT8 true NULL a {} false 99 d INT8 true NULL a + b {} false 100 rowid INT8 false unique_rowid() · {primary} true 101 102 statement error cannot write directly to computed column "c" 103 INSERT INTO x (c) VALUES (1) 104 105 statement ok 106 INSERT INTO x (a, b) VALUES (1, 2) 107 108 query II 109 SELECT c, d FROM x 110 ---- 111 1 3 112 113 statement ok 114 DELETE FROM x 115 116 statement ok 117 DELETE FROM x 118 119 statement ok 120 DROP TABLE x 121 122 statement ok 123 CREATE TABLE x ( 124 a INT NOT NULL, 125 b INT, 126 c INT AS (a) STORED, 127 d INT AS (a + b) STORED 128 ) 129 130 statement ok 131 INSERT INTO x (a) VALUES (1) 132 133 statement error null value in column "a" violates not-null constraint 134 INSERT INTO x (b) VALUES (1) 135 136 query II 137 SELECT c, d FROM x 138 ---- 139 1 NULL 140 141 statement ok 142 DROP TABLE x 143 144 # Check with upserts 145 statement ok 146 CREATE TABLE x ( 147 a INT PRIMARY KEY, 148 b INT, 149 c INT AS (b + 1) STORED, 150 d INT AS (b - 1) STORED 151 ) 152 153 statement ok 154 INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b + 1 155 156 query II 157 SELECT c, d FROM x 158 ---- 159 2 0 160 161 statement ok 162 INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b + 1 163 164 query IIII 165 SELECT a, b, c, d FROM x 166 ---- 167 1 2 3 1 168 169 statement ok 170 INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = x.b + 1 171 172 query III 173 SELECT a, b, c FROM x 174 ---- 175 1 3 4 176 177 # Update. 178 179 statement ok 180 UPDATE x SET b = 3 181 182 query III 183 SELECT a, b, c FROM x 184 ---- 185 1 3 4 186 187 # Update/self-reference. 188 189 statement ok 190 UPDATE x SET b = c 191 192 query III 193 SELECT a, b, c FROM x 194 ---- 195 1 4 5 196 197 # Updating with default is not allowed. 198 199 statement error cannot write directly to computed column "c" 200 UPDATE x SET (b, c) = (1, DEFAULT) 201 202 # Upsert using the UPSERT shorthand. 203 204 statement ok 205 UPSERT INTO x (a, b) VALUES (1, 2) 206 207 query IIII 208 SELECT a, b, c, d FROM x 209 ---- 210 1 2 3 1 211 212 statement ok 213 TRUNCATE x 214 215 # statement ok 216 # INSERT INTO x VALUES (2, 3) ON CONFLICT (a) DO UPDATE SET a = 2, b = 3 217 218 statement ok 219 UPSERT INTO x VALUES (2, 3) 220 221 query IIII 222 SELECT a, b, c, d FROM x 223 ---- 224 2 3 4 2 225 226 statement ok 227 TRUNCATE x 228 229 statement error cannot write directly to computed column "c" 230 UPSERT INTO x VALUES (2, 3, 12) 231 232 statement ok 233 UPSERT INTO x (a, b) VALUES (2, 3) 234 235 query IIII 236 SELECT a, b, c, d FROM x 237 ---- 238 2 3 4 2 239 240 statement ok 241 DROP TABLE x 242 243 # TODO(justin): #22434 244 # statement ok 245 # CREATE TABLE x ( 246 # b INT AS a STORED, 247 # a INT 248 # ) 249 # 250 # statement ok 251 # INSERT INTO x VALUES (DEFAULT, 1) 252 # 253 # statement ok 254 # INSERT INTO x VALUES (DEFAULT, '2') 255 # 256 # query I 257 # SELECT b FROM x ORDER BY b 258 # ---- 259 # 1 260 # 2 261 # 262 # statement ok 263 # DROP TABLE x 264 265 statement error use AS \( <expr> \) STORED 266 CREATE TABLE y ( 267 a INT AS 3 STORED 268 ) 269 270 statement error use AS \( <expr> \) STORED 271 CREATE TABLE y ( 272 a INT AS (3) 273 ) 274 275 statement error at or near "virtual": syntax error: unimplemented 276 CREATE TABLE y ( 277 a INT AS (3) VIRTUAL 278 ) 279 280 statement error expected computed column expression to have type int, but .* has type string 281 CREATE TABLE y ( 282 a INT AS ('not an integer!'::STRING) STORED 283 ) 284 285 # We utilize the types from other columns. 286 287 statement error expected computed column expression to have type int, but 'a' has type string 288 CREATE TABLE y ( 289 a STRING, 290 b INT AS (a) STORED 291 ) 292 293 statement error impure functions are not allowed in computed column 294 CREATE TABLE y ( 295 a TIMESTAMP AS (now()) STORED 296 ) 297 298 statement error impure functions are not allowed in computed column 299 CREATE TABLE y ( 300 a STRING AS (concat(now()::STRING, uuid_v4()::STRING)) STORED 301 ) 302 303 statement error computed columns cannot reference other computed columns 304 CREATE TABLE y ( 305 a INT AS (3) STORED, 306 b INT AS (a) STORED 307 ) 308 309 statement error column "a" does not exist 310 CREATE TABLE y ( 311 b INT AS (a) STORED 312 ) 313 314 statement error aggregate functions are not allowed in computed column 315 CREATE TABLE y ( 316 b INT AS (count(1)) STORED 317 ) 318 319 statement error computed columns cannot have default values 320 CREATE TABLE y ( 321 a INT AS (3) STORED DEFAULT 4 322 ) 323 324 # TODO(justin,bram): this should be allowed. 325 statement ok 326 CREATE TABLE x (a INT PRIMARY KEY) 327 328 statement error computed columns cannot reference non-restricted FK columns 329 CREATE TABLE y ( 330 q INT REFERENCES x (a) ON UPDATE CASCADE, 331 r INT AS (q) STORED 332 ) 333 334 statement error computed columns cannot reference non-restricted FK columns 335 CREATE TABLE y ( 336 q INT REFERENCES x (a) ON DELETE CASCADE, 337 r INT AS (q) STORED 338 ) 339 340 statement error computed column "r" cannot be a foreign key reference 341 CREATE TABLE y ( 342 r INT AS (1) STORED REFERENCES x (a) 343 ) 344 345 statement error computed column "r" cannot be a foreign key reference 346 CREATE TABLE y ( 347 r INT AS (1) STORED REFERENCES x 348 ) 349 350 statement error computed column "r" cannot be a foreign key reference 351 CREATE TABLE y ( 352 a INT, 353 r INT AS (1) STORED REFERENCES x 354 ) 355 356 # Regression test for #36036. 357 statement ok 358 CREATE TABLE tt (i INT8 AS (1) STORED) 359 360 statement error variable sub-expressions are not allowed in computed column 361 ALTER TABLE tt ADD COLUMN c STRING AS ((SELECT NULL)) STORED 362 363 statement error computed columns cannot reference other computed columns 364 ALTER TABLE tt ADD COLUMN c INT8 AS (i) STORED 365 366 # Composite FK. 367 368 statement ok 369 CREATE TABLE xx ( 370 a INT, 371 b INT, 372 UNIQUE (a, b) 373 ) 374 375 statement error computed column "y" cannot be a foreign key reference 376 CREATE TABLE yy ( 377 x INT, 378 y INT AS (3) STORED, 379 FOREIGN KEY (x, y) REFERENCES xx (a, b) 380 ) 381 382 statement error computed column "y" cannot be a foreign key reference 383 CREATE TABLE yy ( 384 x INT, 385 y INT AS (3) STORED, 386 FOREIGN KEY (y, x) REFERENCES xx (a, b) 387 ) 388 389 statement ok 390 DROP TABLE xx 391 392 statement ok 393 CREATE TABLE y ( 394 r INT AS (1) STORED, 395 INDEX (r) 396 ) 397 398 statement error computed column "r" cannot be a foreign key reference 399 ALTER TABLE y ADD FOREIGN KEY (r) REFERENCES x (a) 400 401 statement ok 402 DROP TABLE y 403 404 statement error variable sub-expressions are not allowed in computed column 405 CREATE TABLE y ( 406 r INT AS ((SELECT 1)) STORED 407 ) 408 409 statement error no data source matches prefix: x 410 CREATE TABLE y ( 411 r INT AS (x.a) STORED 412 ) 413 414 statement error no data source matches prefix: x 415 CREATE TABLE y ( 416 q INT, 417 r INT AS (x.q) STORED 418 ) 419 420 statement ok 421 CREATE TABLE y ( 422 q INT, 423 r INT AS (y.q) STORED 424 ) 425 426 statement ok 427 DROP TABLE y 428 429 # It's ok if they exist and we don't reference them. 430 statement ok 431 CREATE TABLE y ( 432 q INT REFERENCES x (a) ON UPDATE CASCADE, 433 r INT AS (3) STORED 434 ) 435 436 statement ok 437 DROP TABLE y 438 439 statement ok 440 DROP TABLE x 441 442 # Indexes on computed columns 443 statement ok 444 CREATE TABLE x ( 445 k INT PRIMARY KEY, 446 a JSON, 447 b TEXT AS (a->>'q') STORED, 448 INDEX (b) 449 ) 450 451 statement error cannot write directly to computed column 452 INSERT INTO x (k, a, b) VALUES (1, '{"q":"xyz"}', 'not allowed!'), (2, '{"q":"abc"}', 'also not allowed') 453 454 statement error cannot write directly to computed column 455 UPDATE x SET (k, a, b) = (1, '{"q":"xyz"}', 'not allowed!') 456 457 statement ok 458 INSERT INTO x (k, a) VALUES (1, '{"q":"xyz"}'), (2, '{"q":"abc"}') 459 460 query IT 461 SELECT k, b FROM x ORDER BY b 462 ---- 463 2 abc 464 1 xyz 465 466 statement ok 467 DROP TABLE x 468 469 statement ok 470 CREATE TABLE x ( 471 k INT AS ((data->>'id')::INT) STORED PRIMARY KEY, 472 data JSON 473 ) 474 475 statement ok 476 INSERT INTO x (data) VALUES 477 ('{"id": 1, "name": "lucky"}'), 478 ('{"id": 2, "name": "rascal"}'), 479 ('{"id": 3, "name": "captain"}'), 480 ('{"id": 4, "name": "lola"}') 481 482 # ON CONFLICT that modifies a PK. 483 statement ok 484 INSERT INTO x (data) VALUES ('{"id": 1, "name": "ernie"}') 485 ON CONFLICT (k) DO UPDATE SET data = '{"id": 5, "name": "ernie"}' 486 487 # ON CONFLICT that modifies a PK which then also conflicts. 488 statement error duplicate key value 489 INSERT INTO x (data) VALUES ('{"id": 5, "name": "oliver"}') 490 ON CONFLICT (k) DO UPDATE SET data = '{"id": 2, "name": "rascal"}' 491 492 # Updating a non-PK column. 493 statement ok 494 UPDATE x SET data = data || '{"name": "carl"}' WHERE k = 2 495 496 query T 497 SELECT data->>'name' FROM x WHERE k = 2 498 ---- 499 carl 500 501 query T 502 SELECT data->>'name' FROM x WHERE k = 5 503 ---- 504 ernie 505 506 # Referencing a computed column. 507 statement ok 508 create table y ( 509 a INT REFERENCES x (k) 510 ) 511 512 statement ok 513 INSERT INTO y VALUES (5) 514 515 statement error foreign key 516 INSERT INTO y VALUES (100) 517 518 statement ok 519 DROP TABLE x CASCADE 520 521 statement ok 522 CREATE TABLE x ( 523 a INT, 524 b INT, 525 c INT, 526 d INT[] AS (ARRAY[a, b, c]) STORED 527 ) 528 529 statement ok 530 INSERT INTO x (a, b, c) VALUES (1, 2, 3) 531 532 query T 533 SELECT d FROM x 534 ---- 535 {1,2,3} 536 537 statement ok 538 TRUNCATE x 539 540 # Make sure we get the permutation on the inserts correct. 541 542 statement ok 543 INSERT INTO x (b, a, c) VALUES (1, 2, 3) 544 545 query T 546 SELECT d FROM x 547 ---- 548 {2,1,3} 549 550 # Make sure we get the permutation on the updates correct. 551 statement ok 552 UPDATE x SET (c, a, b) = (1, 2, 3) 553 554 query T 555 SELECT d FROM x 556 ---- 557 {2,3,1} 558 559 statement ok 560 UPDATE x SET (a, c) = (1, 2) 561 562 query T 563 SELECT d FROM x 564 ---- 565 {1,3,2} 566 567 statement ok 568 UPDATE x SET c = 2, a = 3, b = 1 569 570 query T 571 SELECT d FROM x 572 ---- 573 {3,1,2} 574 575 # Make sure we get the permutation on upserts correct. 576 statement ok 577 INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (a, b, c) = (1, 2, 3) 578 579 query T 580 SELECT d FROM x 581 ---- 582 {1,2,3} 583 584 statement ok 585 INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (c, a, b) = (1, 2, 3) 586 587 query T 588 SELECT d FROM x 589 ---- 590 {2,3,1} 591 592 statement ok 593 INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (c, a) = (1, 2) 594 595 query T 596 SELECT d FROM x 597 ---- 598 {2,3,1} 599 600 statement ok 601 DROP TABLE x 602 603 statement ok 604 CREATE TABLE x ( 605 a INT, 606 b INT as (x.a) STORED, 607 FAMILY "primary" (a, b, rowid) 608 ) 609 610 query TT 611 SHOW CREATE TABLE x 612 ---- 613 x CREATE TABLE x ( 614 a INT8 NULL, 615 b INT8 NULL AS (a) STORED, 616 FAMILY "primary" (a, b, rowid) 617 ) 618 619 statement ok 620 DROP TABLE x 621 622 # Check that computed columns are resilient to column renames. 623 statement ok 624 CREATE TABLE x ( 625 a INT, 626 b INT AS (a) STORED, 627 FAMILY "primary" (a, b, rowid) 628 ) 629 630 statement ok 631 ALTER TABLE x RENAME COLUMN a TO c 632 633 query TT 634 SHOW CREATE TABLE x 635 ---- 636 x CREATE TABLE x ( 637 c INT8 NULL, 638 b INT8 NULL AS (c) STORED, 639 FAMILY "primary" (c, b, rowid) 640 ) 641 642 statement ok 643 DROP TABLE x 644 645 statement ok 646 CREATE TABLE x ( 647 a INT, 648 b INT AS (a * 2) STORED, 649 FAMILY "primary" (a, b, rowid) 650 ) 651 652 query T colnames 653 SELECT generation_expression FROM information_schema.columns 654 WHERE table_name = 'x' and column_name = 'b' 655 ---- 656 generation_expression 657 a * 2:::INT8 658 659 query I 660 SELECT count(*) FROM information_schema.columns 661 WHERE table_name = 'x' and generation_expression = '' 662 ---- 663 2 664 665 statement ok 666 INSERT INTO x VALUES (3) 667 668 # Verify computed columns work. 669 statement ok 670 ALTER TABLE x ADD COLUMN c INT NOT NULL AS (a + 4) STORED 671 672 query TT 673 SHOW CREATE TABLE x 674 ---- 675 x CREATE TABLE x ( 676 a INT8 NULL, 677 b INT8 NULL AS (a * 2:::INT8) STORED, 678 c INT8 NOT NULL AS (a + 4:::INT8) STORED, 679 FAMILY "primary" (a, b, rowid, c) 680 ) 681 682 statement ok 683 INSERT INTO x VALUES (6) 684 685 query III 686 SELECT * FROM x ORDER BY a 687 ---- 688 3 6 7 689 6 12 10 690 691 # Verify a bad statement fails. 692 statement error pq: could not parse "a" as type int 693 ALTER TABLE x ADD COLUMN d INT AS (a + 'a') STORED 694 695 statement error could not parse "a" as type int 696 ALTER TABLE x ADD COLUMN d INT AS ('a') STORED 697 698 statement error unsupported binary operator 699 ALTER TABLE x ADD COLUMN d INT AS (a / 0) STORED 700 701 # Verify an error during computation fails. 702 statement error division by zero 703 ALTER TABLE x ADD COLUMN d INT AS (a // 0) STORED 704 705 statement ok 706 DROP TABLE x 707 708 # Regression test for #23109 709 statement ok 710 CREATE TABLE x ( 711 a INT DEFAULT 1, 712 b INT AS (2) STORED 713 ) 714 715 statement ok 716 INSERT INTO x (a) SELECT 1 717 718 statement ok 719 DROP TABLE x 720 721 statement ok 722 CREATE TABLE x ( 723 b INT AS (2) STORED, 724 a INT DEFAULT 1 725 ) 726 727 statement ok 728 INSERT INTO x (a) SELECT 1 729 730 statement ok 731 DROP TABLE x 732 733 # Verify errors emitted from computed columns contain the column name 734 statement ok 735 CREATE TABLE error_check (k INT PRIMARY KEY, s STRING, i INT AS (s::INT) STORED) 736 737 statement ok 738 INSERT INTO error_check VALUES(1, '1') 739 740 statement error could not parse "foo" as type int: strconv.ParseInt 741 INSERT INTO error_check VALUES(2, 'foo') 742 743 statement error could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax 744 UPDATE error_check SET s = 'foo' WHERE k = 1 745 746 # Upsert -> update 747 # NOTE: The CBO cannot show the name of the computed column in the error message 748 # because the computation is part of an overall SQL statement. 749 statement error could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax 750 UPSERT INTO error_check VALUES (1, 'foo') 751 752 # Upsert -> insert 753 statement error could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax 754 UPSERT INTO error_check VALUES (3, 'foo') 755 756 statement ok 757 CREATE TABLE x ( 758 a INT PRIMARY KEY, 759 b INT AS (a+1) STORED 760 ) 761 762 query error value type decimal doesn't match type int of column "a" 763 INSERT INTO x VALUES(1.4) 764 765 # Regression test for #34901: verify that builtins can be used in computed 766 # column expressions without a "memory budget exceeded" error while backfilling 767 statement ok 768 CREATE TABLE t34901 (x STRING) 769 770 statement ok 771 INSERT INTO t34901 VALUES ('a') 772 773 statement ok 774 ALTER TABLE t34901 ADD COLUMN y STRING AS (concat(x, 'b')) STORED 775 776 query TT 777 SELECT * FROM t34901 778 ---- 779 a ab 780 781 # Regression test for #42418. 782 statement ok 783 CREATE TABLE t42418 (x INT GENERATED ALWAYS AS (1) STORED); 784 ALTER TABLE t42418 ADD COLUMN y INT GENERATED ALWAYS AS (1) STORED 785 786 query TT 787 SHOW CREATE t42418 788 ---- 789 t42418 CREATE TABLE t42418 ( 790 x INT8 NULL AS (1:::INT8) STORED, 791 y INT8 NULL AS (1:::INT8) STORED, 792 FAMILY "primary" (x, rowid, y) 793 )