Skip to content

Resolved bug in parse_function_arg #1826

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LucaCappelletti94
Copy link
Contributor

This pull request resolves the bug described in issue #1825, which was caused by an incorrect implementation of the named argument parsing. It also adds a few tests to verify that the new implementation is correct.

The previous implementation made the incorrect assumption that arguments name cannot have the same name as types, but the set of types that are parsed as types in sqlparser is a superset of the types that are present in each dialect. Therefore, it is correct syntax to use as argument name for instance int2 for PostgreSQL, while this same argument name would be interpreted as a type elsewhere.

I have changed the parsing to determine via a look-ahead whether the name is a type or not.

Best,
Luca

Comment on lines 5205 to 5217
// It may appear that the first token can be converted into a known
// type, but this could also be a collision as some types are only
// present in some dialects and therefore some type reserved keywords
// may be freely used as argument names in other dialects.

// To check whether the first token is a name or a type, we need to
// peek the next token, which if it is another type keyword, then the
// first token is a name and not a type in itself.
let potential_tokens = [Token::Eq, Token::RParen, Token::Comma];
if !self.peek_keyword(Keyword::DEFAULT)
&& !potential_tokens.contains(&self.peek_token().token)
{
name = Some(Ident::new(next_token.to_string()));
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering if something like this work instead?

if let DataType::Custom(n, _) = &data_type {
  if let Some(dt) = self.maybe_parse(|parser| parser.parse_data_type())? {
    match n.0[0].clone() {
      ObjectNamePart::Identifier(ident) => name = Some(ident),
    }
    data_type = dt;
  }
}

thinking if so it would closer match the desired goal to parse an optional datatype if the first token is regular identifier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can try it out, I wasn't aware of maybe_parse which certainly seems to make it less confusing to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code you proposed does not work as Int2 (or any analogous such type) does not fall in if let DataType::Custom(n, _) = &data_type {, but other variants. I am trying to update my own version using maybe_parse instead of the keywords check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See commit 1801b2a

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.

2 participants