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

Support for table column type identity with sequence options, create sequence, drop sequnece. #587

Conversation

samjay000
Copy link
Contributor

Support added for

Only tested in regard to PostgreSQL documentation

Test Cases:

  • sqlparser_postgres::parse_create_table_generated_always_as_identity
  • sqlparser_postgres::parse_create_sequence
  • sqlparser_postgres::parse_drop_sequence

@andygrove
Copy link
Collaborator

Thanks @sam-mmm I will try and make time to review this next week.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 2893987821

  • 197 of 273 (72.16%) changed or added relevant lines in 4 files are covered.
  • 168 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-1.5%) to 83.977%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/ddl.rs 11 16 68.75%
src/ast/mod.rs 49 65 75.38%
src/parser.rs 90 145 62.07%
Files with Coverage Reduction New Missed Lines %
src/ast/mod.rs 2 77.25%
src/parser.rs 6 82.58%
src/lib.rs 160 7.49%
Totals Coverage Status
Change from base Build 2889184458: -1.5%
Covered Lines: 9670
Relevant Lines: 11515

💛 - Coveralls

@samjay000
Copy link
Contributor Author

Fixed issues with check list and added additional test scenarios

Copy link
Collaborator

@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 for the contribution @sam-mmm -- this is a great start. With a few more doc comments and some cleanup I think this PR will be ready to go

I apologize for the belated review

@@ -74,6 +74,7 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}


Copy link
Collaborator

Choose a reason for hiding this comment

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

why this change?


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AlwaysOrByDefaultOrAlwaysAs {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please add docstrings here (following the examples above) that explain the SQL syntax this enum represents?

Likewise each of the enum variants should have documentation as well.

The same comment applies to AsIdentityOrStored

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if calling this enum Generated would better match the SQL it comes from and be more in line with the conventions of the rest of this crate?

@@ -411,6 +433,28 @@ impl fmt::Display for ColumnOption {
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
CharacterSet(n) => write!(f, "CHARACTER SET {}", n),
Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
Generated {
always_or_by_default_or_always_as: always_or_by_default_or_stored,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why the different names here?

Like why not

Suggested change
always_or_by_default_or_always_as: always_or_by_default_or_stored,
always_or_by_default_or_always_as,


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AsIdentityOrStored {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This enum does not appear to be used

Comment on lines +2202 to +2205
let mut as_type: String = String::from("");
if let Some(dt) = data_type.as_ref() {
as_type = " AS ".to_string() + &dt.to_string();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

As written this looks like it will do a bunch of string creation and copying that is unnecessary. Perhaps something like this would be less code and more efficient

Suggested change
let mut as_type: String = String::from("");
if let Some(dt) = data_type.as_ref() {
as_type = " AS ".to_string() + &dt.to_string();
}
let as_type = if let Some(dt) = data_type.as_ref() {
format!(" AS {}", dt);
} else {
"".to_string()
}

Comment on lines +2257 to +2263
if let Some(minvalue) = minvalue.as_ref() {
write!(f, " MINVALUE {minvalue}", minvalue = minvalue)
} else if no.is_some() {
write!(f, " NO MINVALUE")
} else {
write!(f, "")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can write this type of code more concisely (and have the compiler verify you covered all the cases) like:

Suggested change
if let Some(minvalue) = minvalue.as_ref() {
write!(f, " MINVALUE {minvalue}", minvalue = minvalue)
} else if no.is_some() {
write!(f, " NO MINVALUE")
} else {
write!(f, "")
}
match minvalue.as_ref() {
(Some(minvalue), _) => write!(f, " MINVALUE {minvalue}", minvalue = minvalue),
(None, Some(_)) => write!(f, " NO MINVALUE")
(None, _) => write!(f, " NO MINVALUE")

///MinValue(minvalue, 'NO MINVALUE')
MinValue(Option<Expr>, Option<bool>),
///MaxValue(maxvalue, 'NO MAXVALUE')
MaxValue(Option<Expr>, Option<bool>),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of encoding this as Option<Expr> and an `Option which can have 4 possible states, only three of which are valid, how about encoding it as an enum:

enum MinMaxValue {
  // clause is not specified
  Empty,
  // NO MINVALUE/NO MAX VALUE
  None, 
  // MINVALUE <expr> / MAXVALUE <expr>
  Some(Expr)
}

use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::ParserError;
use test_utils::*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️ very nice tests

}),
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_array_subquery_expr() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please explain why this test was deleted?

@@ -1618,52 +1808,6 @@ fn parse_declare() {
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" BINARY INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT * FROM table_name LIMIT 2222");
}

#[test]
fn parse_current_functions() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

likewise, why is this test deleted?

@samjay000
Copy link
Contributor Author

@alamb I have created PR #673, will create series of small PR's for rest of features.
So I'm closing this PR.

@samjay000 samjay000 closed this Oct 14, 2022
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.

None yet

4 participants