-
Notifications
You must be signed in to change notification settings - Fork 605
Fix parsing of negative values #1419
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
Changes from all commits
a0873ca
216d721
99b98ca
accee6d
4eef71b
826fb60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2819,6 +2819,18 @@ fn parse_window_function_null_treatment_arg() { | |
); | ||
} | ||
|
||
#[test] | ||
fn parse_negative_value() { | ||
let sql1 = "SELECT -1"; | ||
one_statement_parses_to(sql1, "SELECT -1"); | ||
Comment on lines
+2824
to
+2825
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. My code doesn't affect this case, but I wrote a test for it so that the logic doesn't break in the future. |
||
|
||
let sql2 = "CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100;"; | ||
one_statement_parses_to( | ||
sql2, | ||
"CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100", | ||
); | ||
} | ||
|
||
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. Can you also please add a test for your original reproducer? CREATE SEQUENCE seq START -100 INCREMENT -10 MINVALUE -1000 MAXVALUE 1; I tried adding it locally and it doesn't seem to pass for me
I tested like this: andrewlamb@Andrews-MacBook-Pro-2:~/Software/sqlparser-rs$ git diff 99b98ca86cf0591a89a644e06742758a70aa04ea
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index 1ebb5d5..89aee71 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -277,6 +277,14 @@ fn parse_create_sequence() {
"CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE",
);
+ // negative increments
+ let sql7 = "CREATE SEQUENCE seq START -100 INCREMENT -10 MINVALUE -1000 MAXVALUE 1;";
+ pg().one_statement_parses_to(
+ sql7,
+ "CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE",
+ );
+
+
assert!(matches!(
pg().parse_sql_statements("CREATE SEQUENCE foo INCREMENT 1 NO MINVALUE NO"),
Err(ParserError::ParserError(_)) Then ran with cargo test --test sqlparser_postgres 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. The example from the task description does not work, since the order of options is important for sqlparser-rs. Try running it like this
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. Thanks -- could you please add at least one test with this syntax (no space between 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. I added a test, but @git-hulk is comment made me think about the correctness of the solution. Perhaps the function really should return By the way, do tests really need to be moved to a common module? Tests for 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.
That seems like it might be a good idea to make them more discoverable |
||
#[test] | ||
fn parse_create_table() { | ||
let sql = "CREATE TABLE uk_cities (\ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,6 +277,26 @@ fn parse_create_sequence() { | |
"CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE", | ||
); | ||
|
||
let sql7 = "CREATE SEQUENCE name4 | ||
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. This change didn't guard with the PostgreSQL dialect guard, so should move test case to common. 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. Can I put the entire "parse_create_sequence" function in a common module? 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. I think it should be good to keep it inside PostgreSQL unless more dialects support this syntax. |
||
AS BIGINT | ||
INCREMENT -15 | ||
MINVALUE - 2000 MAXVALUE -50 | ||
START WITH - 60"; | ||
pg().one_statement_parses_to( | ||
sql7, | ||
"CREATE SEQUENCE name4 AS BIGINT INCREMENT -15 MINVALUE -2000 MAXVALUE -50 START WITH -60", | ||
); | ||
|
||
let sql8 = "CREATE SEQUENCE name5 | ||
AS BIGINT | ||
INCREMENT +10 | ||
MINVALUE + 30 MAXVALUE +5000 | ||
START WITH + 45"; | ||
pg().one_statement_parses_to( | ||
sql8, | ||
"CREATE SEQUENCE name5 AS BIGINT INCREMENT +10 MINVALUE +30 MAXVALUE +5000 START WITH +45", | ||
); | ||
|
||
assert!(matches!( | ||
pg().parse_sql_statements("CREATE SEQUENCE foo INCREMENT 1 NO MINVALUE NO"), | ||
Err(ParserError::ParserError(_)) | ||
|
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.
This is done to avoid code duplication.
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.
If there is no sign, it will not turn into "UnaryOp" so as not to break the old behavior.