Skip to content

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

Closed
samjay000 wants to merge 20 commits into
apache:mainfrom
samjay000:generated_always_as_identity_support
Closed

Support for table column type identity with sequence options, create sequence, drop sequnece.#587
samjay000 wants to merge 20 commits into
apache:mainfrom
samjay000:generated_always_as_identity_support

Conversation

@samjay000

Copy link
Copy Markdown
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
Copy Markdown
Member

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

@coveralls

Copy link
Copy Markdown

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
Copy Markdown
Contributor Author

Fixed issues with check list and added additional test scenarios

@alamb alamb left a comment

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.

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

with:
github-token: ${{ secrets.GITHUB_TOKEN }}


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.

why this change?

Comment thread src/ast/ddl.rs

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

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.

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
Copy Markdown
Contributor

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?

Comment thread src/ast/ddl.rs
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
Copy Markdown
Contributor

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,

Comment thread src/ast/ddl.rs

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

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.

This enum does not appear to be used

Comment thread src/ast/mod.rs
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
Copy Markdown
Contributor

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 thread src/ast/mod.rs
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
Copy Markdown
Contributor

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")

Comment thread src/ast/mod.rs
///MinValue(minvalue, 'NO MINVALUE')
MinValue(Option<Expr>, Option<bool>),
///MaxValue(maxvalue, 'NO MAXVALUE')
MaxValue(Option<Expr>, Option<bool>),

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.

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::ast::Value::Boolean;
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::ParserError;
use test_utils::*;

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.

❤️ very nice tests

}

#[test]
fn parse_array_subquery_expr() {

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.

Can you please explain why this test was deleted?

}

#[test]
fn parse_current_functions() {

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.

likewise, why is this test deleted?

@samjay000

Copy link
Copy Markdown
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.

4 participants