Skip to content

Commit

Permalink
Merge #60154 #60469
Browse files Browse the repository at this point in the history
60154: sql: add crdb_internal.show_create_all_tables builtin r=rafiss a=RichardJCai

sql: add crdb_internal.show_create_all_tables builtin

Making this a PR first before continuing with #53488, we can alias this builtin with SHOW CREATE ALL TABLES.

Example output:
```
query T
SELECT crdb_internal.show_create_all_tables('d')
----
CREATE TABLE public.parent (
  x INT8 NULL,
  y INT8 NULL,
  z INT8 NULL,
  UNIQUE INDEX parent_x_y_z_key (x ASC, y ASC, z ASC),
  UNIQUE INDEX parent_x_key (x ASC),
  FAMILY f1 (x, y, z, rowid)
);
CREATE TABLE public.full_test (
  x INT8 NULL,
  y INT8 NULL,
  z INT8 NULL,
  UNIQUE INDEX full_test_x_key (x ASC),
  FAMILY f1 (x, y, z, rowid)
);
CREATE SEQUENCE public.s MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1;
CREATE VIEW public.vx ("?column?") AS SELECT 1;
ALTER TABLE public.full_test ADD CONSTRAINT fk_x_ref_parent FOREIGN KEY (x, y, z) REFERENCES public.parent(x, y, z) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE public.full_test ADD CONSTRAINT test_fk FOREIGN KEY (x) REFERENCES public.parent(x) ON DELETE CASCADE;
-- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
ALTER TABLE public.full_test VALIDATE CONSTRAINT fk_x_ref_parent;
ALTER TABLE public.full_test VALIDATE CONSTRAINT test_fk;
```

Release note (sql change): crdb_internal.show_create_all_tables is
a new builtin that takes in a database name (string) and returns
a flat log of all the CREATE TABLE statements in the database followed
by alter statements to add constraints. The output can be used
to recreate a database. This builtin was added to replace old dump logic.

60469: opt: prefer sorting fewer columns r=RaduBerinde a=RaduBerinde

Currently, if we have to sort results and project a new column, there
is no cost difference between the two orders and we happen to prefer
the sort on top. It is preferable to sort before adding new columns to
avoid storing the extra value in memory or on disk.

This change improves the sort costing by adding a cost proportional to
the total number of values.

Fixes #32952.

Co-authored-by: richardjcai <caioftherichard@gmail.com>
Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
  • Loading branch information
3 people committed Feb 11, 2021
3 parents 32ffe26 + e77a25a + c140110 commit 5db2e58
Show file tree
Hide file tree
Showing 38 changed files with 1,572 additions and 800 deletions.
4 changes: 4 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,10 @@ The swap_ordinate_string parameter is a 2-character string naming the ordinates
</span></td></tr>
<tr><td><a name="convert_to"></a><code>convert_to(str: <a href="string.html">string</a>, enc: <a href="string.html">string</a>) &rarr; <a href="bytes.html">bytes</a></code></td><td><span class="funcdesc"><p>Encode the string <code>str</code> as a byte array using encoding <code>enc</code>. Supports encodings ‘UTF8’ and ‘LATIN1’.</p>
</span></td></tr>
<tr><td><a name="crdb_internal.show_create_all_tables"></a><code>crdb_internal.show_create_all_tables(dbName: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns a flat log of CREATE table statements followed by
ALTER table statements that add table constraints. The flat log can be used
to recreate a database.’</p>
</span></td></tr>
<tr><td><a name="decode"></a><code>decode(text: <a href="string.html">string</a>, format: <a href="string.html">string</a>) &rarr; <a href="bytes.html">bytes</a></code></td><td><span class="funcdesc"><p>Decodes <code>data</code> using <code>format</code> (<code>hex</code> / <code>escape</code> / <code>base64</code>).</p>
</span></td></tr>
<tr><td><a name="difference"></a><code>difference(source: <a href="string.html">string</a>, target: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert two strings to their Soundex codes and then reports the number of matching code positions.</p>
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ go_test(
"scrub_test.go",
"sequence_test.go",
"set_zone_config_test.go",
"show_create_all_tables_builtin_test.go",
"show_fingerprints_test.go",
"show_ranges_test.go",
"show_stats_test.go",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ vectorized: true
• filter
│ filter: (b @> '[[1, 2]]') OR (b @> '[[3, 4]]')
└── • sort
order: +a
└── • index join
table: json_tab@primary
└── • index join
table: json_tab@primary
└── • sort
order: +a
└── • inverted filter
│ inverted column: b_inverted_key
Expand All @@ -186,7 +186,7 @@ vectorized: true
table: json_tab@json_inv
spans: 4 spans
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUk9Fv2j4Qx99_f8XpXkp_85Q4oarkp7RrqlExYAnSNkE0GXLrMlE7s03FhPjfpyS0W7oSwUMi350_d1_f6bZof65QYPx5MrwajKB3M0in6cfhOaTxMH43BQm3yfgD_LBafXVyAZ_ex0kMvQVE87XvhwRnsxlnEGTZ2TmMkxeRkEF_H7mJE7j-AhIZKp3TSD6QRTFDjhnD0uglWatN5drWFwb5BoXPsFDl2lXujOFSG0KxRVe4FaHAqVysKCGZk_F8ZJiTk8WqTvskOKoPhXpEhmkplRXgcd-7Mqb--NuW5U0MfSs2scpBqhwuQbvvZCwyHK-dgIizKMBsx1Cv3R9Z1sl7QsF37HjpA_VIxlF-W6wcGTIeb-t_iseb0oBWEHEBttIP1knjxBwv53Pf96tf-MqJ_1_9g8ZGIJWfgvA5Qt2B8J8OHHx-cMrz73Sh9oMLDgyuNMWDNL-O6314SvFUG0fGC9uFI_7mYPr-KemfR9pvF2j8AnpR8Mr2CCHu0vHoutmi9pX9Gj1fOWIaF6coTsiWWllq6T2U2d9lDCm_p2ZXrV6bJU2MXtZlGnNcc7UjJ-uaKG-MgWpClcC_Yd4JB91w0AmH3XDYCfe74X4nfPECznb__Q4AAP__3TS2RA==
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUk9Fv2j4Qx99_f8XpXkp_85Q4oarkp7RrqlExYAnSNkE0GXLrMlE7s03FhPjfpyS0W7oSwUMi350_5-_5fFu0P1coMP48GV4NRtC7GaTT9OPwHNJ4GL-bgoTbZPwBflitvjq5gE_v4ySG3gKi-dr3Q4Kz2YwzCLLs7BzGyYtIyKC_j9zECVx_AYkMlc5pJB_Iopghx4xhafSSrNWmcm3rDYN8g8JnWKhy7Sp3xnCpDaHYoivcilDgVC5WlJDMyXg-MszJyWJVp30SHNWLQj0iw7SUygrwuO9dGVN__G3L8iaGvhWbWOUgVQ6XoN13MhYZjtdOQMRZFGC2Y6jX7o8s6-Q9oeA7drz0gXok4yi_LVaODBmPt_U_xeNNaUAriLgAW-kH66RxYo6X87nv-9UvfGXF_6_-QWMjkMpPQfgcob6B8J8bOFh-cEr5qTaOjBe0i474m4Ppw1PS3-lC7d9FeOBdlKZ4kObXca3tn3L4c0v77aMbv4BeFLwyPUKIu3Q8um6mqL1lP0bPW47oxsUpihOypVaWWnoPZfZ3GUPK76mZVavXZkkTo5f1MY05rrnakZN1TZQ3xkA1oUrg3zDvhINuOOiEw2447IT73XC_E754AWe7_34HAAD__4w7tkQ=

# Combine predicates with OR.
query T
Expand Down Expand Up @@ -221,11 +221,11 @@ vectorized: true
• filter
│ filter: (b @> '[3]') OR (b @> '[[1, 2]]')
└── • sort
order: +a
└── • index join
table: json_tab@primary
└── • index join
table: json_tab@primary
└── • sort
order: +a
└── • inverted filter
│ inverted column: b_inverted_key
Expand All @@ -236,7 +236,7 @@ vectorized: true
table: json_tab@json_inv
spans: 3 spans
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUU02P0zAQvfMrRnPZD4wSJ12QfMoumxVdlbYklQC1EXKbYQnq2sF2V0VV_ztKQgsBJdCLNV9v3huPZof22xoFxh-mo-vhGM5vh-ksfTe6gDQexa9ncAl3yeQtfLVafXJyCe_fxEkM50uIFhvfDwnO5mF2dgGTpB2ccwZB1mRu4wRuPoJEhkrnNJaPZFHMkWPGsDR6RdZqU4V2dcEw36LwGRaq3LgqnDFcaUModugKtyYUOJPLNSUkczKejwxzcrJY120PWqPaKNQTMkxLqawAj_vetTFe-OJoeVNDn4ttrHKQKocr0O4LGYsMJxsnIOIsCjDbM9Qb90uOdfKBUPA9-3_JQ_VExlF-V6wdGTIeb-s-5ONtaUAriLgAW-kG66RxYoGvFgvf96snPFr8snpfNj4CqfzfhXyBUE8b_DVt56jBKaPe60L9XE7QsZzSFI_SfG_9M4vCTv7wFP5UG0fGC9vcEX_e2X5wSvvjBgdtgiYu4DwK2vchhLhPJ-Ob5k5a2cOhHEs6JV6dIjEhW2plqSWwq7O_zxhS_kDNDVq9MSuaGr2qaRp3UuPqQE7WNVneOEPVpCqBv4N5LzjoBwe94LAfHPaCB_3gQS_46g9wtn_2IwAA___7DKoE
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUk99v0zAQx9_5K073sh8YJU46kPyUjWWiU2lLUglQGyG3OUZQZwfbnYqq_u8oCS0ElEBfLN-Pz933dLod2m9rFBh_mI6uh2M4vx2ms_Td6ALSeBS_nsEl3CWTt_DVavXJySW8fxMnMZwvIVpsfD8kOJuH2dkFTJK2c84ZBFkTuY0TuPkIEhkqndNYPpJFMUeOGcPS6BVZq03l2tUJw3yLwmdYqHLjKnfGcKUNodihK9yaUOBMLteUkMzJeD4yzMnJYl2XPWiN6k-hnpBhWkplBXjc966N8cIXx583NfS52MYqB6lyuALtvpCxyHCycQIizqIAsz1DvXG_5FgnHwgF37P_lzxUT2Qc5XfF2pEh4_G27kM83pYGtIKIC7CVbrBOGicW-Gqx8H2_esLjj19W78vGRiCV_zuRLxDqaYO_pu0cNThl1FQbR8YL2gNG_Hln-fCU8ve6UD93H3bsvjTFozTfW2tkUdjZf3BK_-MGB-3ujV_AeRS070MIcZ9OxjfNnbSih0M5pnRKvDpFYkK21MpSS2BXZX-fMaT8gZobtHpjVjQ1elW3acxJzdWOnKxrorwxhqoJVQJ_h3kvHPTDQS8c9sNhLzzohwe98NUfcLZ_9iMAAP__ygWqBA==

# More complex combination.
query T
Expand Down
Loading

0 comments on commit 5db2e58

Please sign in to comment.