Skip to content

Add support for named argument functions #250

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 1 commit into from
Aug 2, 2020

Conversation

eyalleshem
Copy link
Contributor

@eyalleshem eyalleshem commented Jul 29, 2020

Add support for named argument function

This commit support functions with argument names.
the format is :
"Select some_function( a => exp, b => exp2 .. ) FROM table1
OR
"select * from table(function(a => exp)) f;"

see:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#named-argument-assignment-token
or example is snwflake db :
https://docs.snowflake.com/en/sql-reference/functions/flatten.html
This commit does not belong to any branch on this repository.

@eyalleshem eyalleshem changed the title Add support for named argument functiona Add support for named argument functions Jul 29, 2020
@eyalleshem
Copy link
Contributor Author

Sorry about some style changes that made by cargo fmt

@coveralls
Copy link

coveralls commented Jul 29, 2020

Pull Request Test Coverage Report for Build 191486507

  • 48 of 50 (96.0%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 91.88%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/tokenizer.rs 16 18 88.89%
Totals Coverage Status
Change from base Build 190279595: 0.03%
Covered Lines: 4481
Relevant Lines: 4877

💛 - Coveralls

@nickolay
Copy link
Contributor

I find it weird that you added NamedArgument as a new variant of Expr. This makes the parser accept many weird constructs, which shouldn't be accepted. I expected this to be a new SelectItem-like enum. You can use peek_nth_token(1) == RArrow to determine if it's a named argument.

The argument name is a simple identifier, not an ObjectName.

Please check the PR for typos (FUCNTION, rigth, agument, // vs /// (which led to rustfmt reformatting the whole enum))

the format is : "Select * from ( a => exp, b=> exp2 .. )

Surely you meant SELECT myfunc(args) rather than FROM (args)

@eyalleshem
Copy link
Contributor Author

Sorry for all the typos , I fixed them (Hope that all of them..)
I also changed the the object name to ident - and fixed the commit message with the correct query .

But i don't sure what you means when you say that the named-argument should be SelectItem
It's a comma-separated argument list that passed to function, In the struct Function the args is comma-separated Vec<Expr> , And because that my thought was that creating new Expr type will be the best suit .

@nickolay
Copy link
Contributor

I said a new enum, a little like SelectItem (currently with two variants: unnamed argument, named argument).

@eyalleshem eyalleshem force-pushed the table_function branch 2 times, most recently from a6d8f94 to 37a4d49 Compare July 31, 2020 07:21
@eyalleshem
Copy link
Contributor Author

I done the changes according to comments .

src/ast/mod.rs Outdated
Comment on lines 876 to 888
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FunctionNamedArg {
pub name: Ident,
pub arg: Expr,
}

impl fmt::Display for FunctionNamedArg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} => {}", self.name, self.arg)?;
Ok(())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to make this a separate struct, rather than FunctionArg::Named { name, arg }?

(Another option is to make the argument type a simple struct with an optional name, and the reason I suggested enum initially because of the other possible kinds of SQL arguments...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed ,
I agree that enum is better.

src/ast/mod.rs Outdated
Comment on lines 900 to 901
FunctionArg::Named(named_arg) => named_arg.fmt(f),
FunctionArg::Unnamed(unnamed_arg) => unnamed_arg.fmt(f),
Copy link
Contributor

Choose a reason for hiding this comment

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

We use write!(f, "{}", ...) in other places for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

src/parser.rs Outdated
Comment on lines 2207 to 2208
// the `=>`
let _ = self.next_token();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use expect_token instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

src/tokenizer.rs Outdated
Comment on lines 326 to 329
// identifier or keyword
// identifier or Keyword
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry , by mistake ..

src/ast/mod.rs Outdated
/// The parser does not distinguish between expressions of different types
/// The parser does not distinguish between qessions of different types
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry , by mistake ..

This commit support functions with argument names.
the format is :
"Select some_function( a => exp, b => exp2 .. ) FROM table1
OR
"select * from table(function(a => exp)) f;"

see:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#named-argument-assignment-token
or example is snwflake db :
https://docs.snowflake.com/en/sql-reference/functions/flatten.html
@eyalleshem eyalleshem requested a review from nickolay August 1, 2020 19:48
@nickolay nickolay merged commit 1cc3bf4 into apache:main Aug 2, 2020
@nickolay
Copy link
Contributor

nickolay commented Aug 2, 2020

Thanks!

@nickolay nickolay removed their request for review August 2, 2020 19:03
eyalleshem added a commit to eyalleshem/sqlparser-rs that referenced this pull request Aug 4, 2020
This commit supports functions with argument names.

the format is :
"Select some_function( a => exp, b => exp2 .. ) FROM table1
OR
"select * from table(function(a => exp)) f;"

see:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#named-argument-assignment-token
or the motivating example from snowflake:
https://docs.snowflake.com/en/sql-reference/functions/flatten.html
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