-
Notifications
You must be signed in to change notification settings - Fork 606
Add support for generated virtual columns with expression #1051
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9618cda
Add support for generated virtual columns with expression
takluyver 05ac5bc
Extend generated column tests for SQLite & add for MySQL
takluyver ec986b0
Remove outdated comment
takluyver f0d0f5b
Remove redundant check in tests
takluyver 6d69a43
Preserve presence/absence of VIRTUAL for generated columns
takluyver 2887501
Remove some leftover debugging
takluyver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,17 @@ fn parse_create_sqlite_quote() { | |
} | ||
} | ||
|
||
#[test] | ||
fn parse_create_table_gencol() { | ||
let sql_default = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2))"; | ||
let sql_virt = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2) VIRTUAL)"; | ||
sqlite_and_generic().verified_stmt(sql_virt); | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sqlite_and_generic().one_statement_parses_to(sql_default, sql_virt); | ||
|
||
let sql_stored = "CREATE TABLE t1 (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED)"; | ||
sqlite_and_generic().verified_stmt(sql_stored); | ||
} | ||
|
||
#[test] | ||
fn test_placeholder() { | ||
// In postgres, this would be the absolute value operator '@' applied to the column 'xxx' | ||
|
@@ -435,7 +446,6 @@ fn sqlite_with_options(options: ParserOptions) -> TestedDialects { | |
|
||
fn sqlite_and_generic() -> TestedDialects { | ||
TestedDialects { | ||
// we don't have a separate SQLite dialect, so test only the generic dialect for now | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice drive by cleanup |
||
dialects: vec![Box::new(SQLiteDialect {}), Box::new(GenericDialect {})], | ||
options: None, | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding additional support to the AST so you can recover the original SQL --- as implemented now this change will not permit people to know when the original query had
VIRTUAL
or notThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Virtual is the default, so adding the marker doesn't make a logical difference (at least in the databases I found that support this). Elsewhere in the tests, it looks like you don't preserve differences like this - e.g.
SHOW COLUMNS
vsSHOW FIELDS
in the MySQL tests. But if you'd like to preserve this one, I'm happy to do that. 🙂If so, I would probably introduce an enum called something like
GenExpressionQualifier
(though that's an ugly name...), which could be None, Virtual or Stored. The Stored option would then be redundant withGeneratedAs::ExpStored
.(For my next PR, I'm planning to make the
GENERATED ALWAYS
keywords optional - as they are in MySQL and SQLite. Let me know if you want the presence or absence of those to be preserved too.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is true that this crate is not always consistent. However, for newly added code let's try and preserve the ability to round trip the AST (though I see there is no note describing that detail on https://github.com/sqlparser-rs/sqlparser-rs -- I will try and add a note when I have time)
yes, please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, the latest commit (6d69a43) preserves the distinction between
VIRTUAL
,STORED
and no qualifier. As I mentioned, that does mean there are two fields which both record ifSTORED
was used. That's not so tidy, but it preserves compatibility for any code that's checking forGeneratedAs::ExpStored
.I've called the new field & enum
generation_expr_mode
&GeneratedExpressionMode
, which are both quite a mouthful. Let me know if you have a better name for this. 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filed #1052 to document this property