Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support different USE statement syntaxes #1387

Merged
merged 2 commits into from
Aug 23, 2024

Conversation

kacpermuda
Copy link
Contributor

I'd like to add support for different syntaxes of the USE statement, used to set a default DATABASE/SCHEMA for set of queries.

Some implementations are calling their top level structure CATALOG (e.g. Databricks, Trino), but most of them stick to DATABASE so I assumed that we should stick to naming: top-level container = DATABASE, lower-level container = SCHEMA. For the full identifier we end up with: DATABASE.SCHEMA.TABLE.

In some implementations DATABASE and SCHEMA are aliases, but usually these implementations have a two-layer structure (DATABASE.TABLE, without SCHEMA) - in that case we should keep DATABASE as DATABASE and leave SCHEMA empty.

There are different syntaxes of USE statement across dialects, but I implemented it like:
(use syntax => resolved db and schema names)

  1. USE x; -> DATABASE=x; SCHEMA=null; (Databricks is adjusted to DATABASE=null; SCHEMA=x;)
  2. USE x.y; -> DATABASE=x; SCHEMA=y;
  3. USE DATABASE x; -> DATABASE=x; SCHEMA=null; (Databricks is adjusted to DATABASE=null; SCHEMA=x; In Databricks DATABASE is an alias for SCHEMA and they both mean lower level structure while the top level structure is called CATALOG)
  4. USE SCHEMA y; -> DATABASE=null; SCHEMA=y;
  5. USE SCHEMA x.y; -> DATABASE=x; SCHEMA=y; (Snowflake specific syntax, SCHEMA keyword is optional and should not influence the parsing of fully qualified name)
  6. USE CATALOG x; -> DATABASE=x; SCHEMA=null; (Databricks-specific keyword: CATALOG)
  7. USE DEFAULT; -> DATABASE=null; SCHEMA=null; (Hive specific, means going back to default database set - we don't know what it is just by looking at the query.)

Docs about USE statement in different dialects:

Some comments:

  • Bigquery, PostgreSQL, Redshift, SQLite do not allow USE statements;

src/parser/mod.rs Outdated Show resolved Hide resolved
tests/sqlparser_databricks.rs Show resolved Hide resolved
src/parser/mod.rs Outdated Show resolved Hide resolved
src/parser/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! cc @alamb

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @kacpermuda and @iffyio

This change makes sense to me

@coveralls
Copy link

Pull Request Test Coverage Report for Build 10509933459

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 186 of 189 (98.41%) changed or added relevant lines in 10 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 89.191%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/dcl.rs 8 9 88.89%
src/parser/mod.rs 15 16 93.75%
tests/sqlparser_hive.rs 14 15 93.33%
Totals Coverage Status
Change from base Build 10418247467: 0.05%
Covered Lines: 28468
Relevant Lines: 31918

💛 - Coveralls

@alamb alamb merged commit 7282ce2 into apache:main Aug 23, 2024
10 checks passed
@alamb
Copy link
Contributor

alamb commented Aug 23, 2024

🚀

@kacpermuda kacpermuda deleted the feat-expand-use-syntax branch August 25, 2024 09:14
ayman-sigma pushed a commit to sigmacomputing/sqlparser-rs that referenced this pull request Nov 19, 2024
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.

5 participants