Skip to content

Added support for star-expanding tuples and numeric tuple indexing#7298

Merged
ericharmeling merged 1 commit intomasterfrom
tuple-selection-extensions
Jun 8, 2020
Merged

Added support for star-expanding tuples and numeric tuple indexing#7298
ericharmeling merged 1 commit intomasterfrom
tuple-selection-extensions

Conversation

@ericharmeling
Copy link
Copy Markdown
Contributor

Fixes #6910.

  • Added description and examples of new tuple selection extensions to Scalar Expressions page.
  • Updated output from examples on Scalar Expressions page.

@ericharmeling ericharmeling requested a review from knz May 6, 2020 20:54
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@cockroach-teamcity
Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with nit

CockroachDB also supports expanding all elements of a tuple as a single row with the `.*` notation. For example:
{% include copy-clipboard.html %}
~~~ sql
> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use an example where the values in the table are named, that will show how the column names are derived from the tuple labels

@ericharmeling ericharmeling requested a review from knz May 12, 2020 17:04
Copy link
Copy Markdown
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


v20.1/scalar-expressions.md, line 742 at r1 (raw file):

Previously, knz (kena) wrote…

maybe use an example where the values in the table are named, that will show how the column names are derived from the tuple labels

That does sound useful. I'm not finding the syntax on naming individual values in a tuple. Could you provide an example?

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling and @knz)


v20.1/scalar-expressions.md, line 742 at r1 (raw file):

SELECT (t).* FROM (SELECT (1,'b',2.3) AS t)

That does sound useful. I'm not finding the syntax on naming individual values in a tuple. Could you provide an example?

Two things:

  1. my comment was suggesting to use a table-generated tuple to explain this. Here's an example:
> create table t(x int, y char);
> select (t).* from t;

or another example, based on yours:

> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t(x,y,z))
  1. your question makes me realize that cockroachdb also enables the user to label their tuples, but we haven't documented that yet! (The reason why is probably because it's very esoteric and probably not very useful in practice). For your entertainment:
> select ((1,'b') as a,b);  -- here's a labeled tuple for you
> select pg_typeof(  ((1,'b') as a,b)   );  -- explains the tuple and its labels
> select (@1).* from (select ((1,'b') as a,b)); -- produces two columns 'a', 'b', as per the section documented in this PR

@ericharmeling ericharmeling requested a review from knz May 13, 2020 15:20
Copy link
Copy Markdown
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


v20.1/scalar-expressions.md, line 742 at r1 (raw file):
Awesome. Thank you!

A few follow-up questions:

  • I'm not seeing how that first example CREATE TABLE statement (create table t(x int, y char);) differs from a normal CREATE TABLE statement. Do all CREATE TABLE statements generate tuples from their column definitions? Also, I'm getting an error when trying to select from that table as if it were a tuple:
> create table t(x int, y char);

CREATE TABLE
> select (t).* from t;

ERROR: column "t" does not exist
SQLSTATE: 42703
  • That second example SELECT statement returns an error for me:
> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t(x,y,z));

invalid syntax: statement ignored: at or near "(": syntax error
SQLSTATE: 42601
DETAIL: source SQL:
SELECT (t).* FROM (SELECT (1,'b',2.3) AS t(x,y,z))
                                          ^
HINT: try \h <SOURCE>
  • Less related to the examples you listed, but significantly, I realized that I was running the original example SELECT statement (SELECT (t).* FROM (SELECT (1,'b',2.3) AS t);) on a build from master that I built 2020/05/12. On the v20.1.0 stable release version available at https://binaries.cockroachdb.com/cockroach-v20.1.0.darwin-10.9-amd64.tgz (CockroachDB CCL v20.1.0 (x86_64-apple-darwin14, built 2020/05/05 00:01:50, go1.13.9)), the output for that statement is different (and likely unexpected):
> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t);

  ?column? | ?column? | ?column?
-----------+----------+-----------
         1 |        1 |        1
(1 row)

The other example statement in this PR works as expected on the stable v20.1.0 release.

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling and @knz)


v20.1/scalar-expressions.md, line 742 at r1 (raw file):
Thanks for double-checking. I was wrong. Serves me well for not checking my examples myself (I was serving from memory).

Let's rewind:

> create table t(x int, y char);
> select (t).* from t;

This one is valid in pg but not yet supported in crdb. I had preferred if we could use it as an example here, but alas we cannot (yet).

> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t(x,y,z);

That was wrong syntax; I really meant this:

> SELECT (t).* FROM (SELECT (1,'b',2.3)) AS t(x,y,z);

However again we're runnign into the problem above, that (t).* does not expand if t points to a table.
However, the following works:

> WITH tuples AS (SELECT (t.*) AS tuple FROM (SELECT 1,'b',2.3) AS t(x,y,z))  -- this builds the tuples, with labels
   SELECT (tuple).* FROM tuples;  -- this expands the tuples and restores the column labels

The argument here is that the notation (t.*) creates tuples and (t).* is its inverse. Just explaining that structuring operators have inverses (like we have for JSON) too is of general interest, I think.

as to your last example:

> SELECT (t).* FROM (SELECT (1,'b',2.3) AS t);

that's valid and expected. The syntax (1,'b',2.3) AS t creates a label-less tuple column called t. So (t).* expands it into label-less columns.

@jseldess
Copy link
Copy Markdown
Contributor

@ericharmeling, @knz, what's left to do on this PR?

@ericharmeling ericharmeling force-pushed the tuple-selection-extensions branch from c744566 to 0a81e0d Compare June 1, 2020 16:21
Copy link
Copy Markdown
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knz

Sorry for the delayed response on this. I updated the PR, following your latest comment. Please re-review - no rush. Thanks!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling)

@cockroach-teamcity
Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling)

@ericharmeling ericharmeling requested a review from lnhsingh June 1, 2020 16:44
@ericharmeling
Copy link
Copy Markdown
Contributor Author

@lnhsingh Ping on review :)

Copy link
Copy Markdown
Contributor

@lnhsingh lnhsingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay! LGTM

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling and @lnhsingh)

@ericharmeling
Copy link
Copy Markdown
Contributor Author

@lnhsingh No worries! TFTR

@ericharmeling ericharmeling merged commit a0891d2 into master Jun 8, 2020
@ericharmeling ericharmeling deleted the tuple-selection-extensions branch June 8, 2020 15:37
rmloveland added a commit that referenced this pull request Jun 8, 2020
rmloveland added a commit that referenced this pull request Jun 8, 2020
Fixes #7427.

Summary of changes:

- Create a new v20.2 folder, and copy the 20.1 content to it; delete the
  various "new or changed in v20.1" callouts from the copied files

- Copy includes to a new '_includes/v20.2' folder.

- Copy images to a new 'images/v20.2' folder.

- Edit image link text in the referencing documents as needed to point
  to the right places.

- Update the various YAML config files with the new version and release
  info.

- Create a new sidebar JSON file for v20.2.

- Do *not* copy over anything related to CockroachCloud for v20.2.  This
  includes editing page frontmatter to remove build directives that pull
  in certain pages for CockroachCloud.

- Update the CockroachDB local YAML config to exclude 19.2 from the
  build process (this file is used to speed up local builds).

- Various edits to the 'Upgrade to CockroachDB v20.2' docs, changing
  version references as appropriate, and removing some version-specific
  limitations.

- Update the base YAML config to use more specific version information
  as required by templates in the `install-cockroachdb-linux` etc. pages

- Miscellaneous edits to CLI output as appropriate.

- Backports: Add Eric's 'scalar expressions' update from #7298; port
  forward Amruta's changes from #7479.
rmloveland added a commit that referenced this pull request Jun 8, 2020
Fixes #7427.

Summary of changes:

- Create a new v20.2 folder, and copy the 20.1 content to it; delete the
  various "new or changed in v20.1" callouts from the copied files

- Copy includes to a new '_includes/v20.2' folder.

- Copy images to a new 'images/v20.2' folder.

- Edit image link text in the referencing documents as needed to point
  to the right places.

- Update the various YAML config files with the new version and release
  info.

- Create a new sidebar JSON file for v20.2.

- Do *not* copy over anything related to CockroachCloud for v20.2.  This
  includes editing page frontmatter to remove build directives that pull
  in certain pages for CockroachCloud.

- Update the CockroachDB local YAML config to exclude 19.2 from the
  build process (this file is used to speed up local builds).

- Various edits to the 'Upgrade to CockroachDB v20.2' docs, changing
  version references as appropriate, and removing some version-specific
  limitations.

- Update the base YAML config to use more specific version information
  as required by templates in the `install-cockroachdb-linux` etc. pages

- Miscellaneous edits to CLI output as appropriate.

- Backports: Add Eric's 'scalar expressions' update from #7298; port
  forward Amruta's changes from #7479.

- Finally: Add stub release notes page for v20.2.0-alpha.1, which will
  be filled in by #7426.
rmloveland added a commit that referenced this pull request Jun 10, 2020
Fixes #7427.

Summary of changes:

- Create a new v20.2 folder, and copy the 20.1 content to it; delete the
  various "new or changed in v20.1" callouts from the copied files

- Copy includes to a new '_includes/v20.2' folder.

- Copy images to a new 'images/v20.2' folder.

- Edit image link text in the referencing documents as needed to point
  to the right places.

- Update the various YAML config files with the new version and release
  info.

- Create a new sidebar JSON file for v20.2.

- Do *not* copy over anything related to CockroachCloud for v20.2.  This
  includes editing page frontmatter to remove build directives that pull
  in certain pages for CockroachCloud.

- Update the CockroachDB local YAML config to exclude 19.2 from the
  build process (this file is used to speed up local builds).

- Various edits to the 'Upgrade to CockroachDB v20.2' docs, changing
  version references as appropriate, and removing some version-specific
  limitations.

- Update the base YAML config to use more specific version information
  as required by templates in the `install-cockroachdb-linux` etc. pages

- Miscellaneous edits to CLI output as appropriate.

- Backports: Add Eric's 'scalar expressions' update from #7298; port
  forward Amruta's changes from #7479.

- Finally: Add stub release notes page for v20.2.0-alpha.1, which will
  be filled in by #7426.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support star-expanding label-less tuples + numeric tuple indexing

5 participants