Skip to content

Add support for postgres json operators ->, ->>, #>, and #>> #458

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 5 commits into from
Apr 19, 2022

Conversation

poonai
Copy link
Contributor

@poonai poonai commented Apr 13, 2022

arrow are used to represent nested field of json.

SELECT info->'items'->>'product' FROM orders

This PR adds support to parse identifier which are separated using arrow

Signed-off-by: password rbalajis25@gmail.com

Signed-off-by: password <rbalajis25@gmail.com>
@coveralls
Copy link

coveralls commented Apr 13, 2022

Pull Request Test Coverage Report for Build 2187863098

  • 74 of 93 (79.57%) changed or added relevant lines in 4 files are covered.
  • 242 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.1%) to 90.431%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser.rs 13 17 76.47%
src/ast/mod.rs 10 16 62.5%
src/tokenizer.rs 13 22 59.09%
Files with Coverage Reduction New Missed Lines %
src/test_utils.rs 1 83.08%
src/parser.rs 91 83.32%
src/ast/mod.rs 150 77.77%
Totals Coverage Status
Change from base Build 2085061398: -0.1%
Covered Lines: 7957
Relevant Lines: 8799

💛 - Coveralls

Signed-off-by: password <rbalajis25@gmail.com>
Copy link
Contributor

@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.

According to

https://www.postgresql.org/docs/current/functions-json.html

It appears there are also #> and #>> operators. I wonder if you have thought about supporting them?

@poonai
Copy link
Contributor Author

poonai commented Apr 17, 2022

It appears there are also #> and #>> operators. I wonder if you have thought about supporting them?

done 😃

Signed-off-by: poonai <rbalajis25@gmail.com>
@alamb alamb changed the title add support for postgres json selection Add support for postgres json operators ->m ->> Apr 18, 2022
@alamb alamb changed the title Add support for postgres json operators ->m ->> Add support for postgres json operators ->, ->>, #>, and #>> Apr 18, 2022
Copy link
Contributor

@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.

Thanks @poonai -- I think this is quite close (the only thing I think that is required is the Display for Token::LongArrow)

src/ast/mod.rs Outdated
Comment on lines 200 to 205
JsonOperator::LongArrow => {
write!(f, "->>")
}
JsonOperator::Arrow => {
write!(f, "->")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it matters, but it would be nice to keep the order of the variants consistent

Suggested change
JsonOperator::LongArrow => {
write!(f, "->>")
}
JsonOperator::Arrow => {
write!(f, "->")
}
JsonOperator::Arrow => {
write!(f, "->")
}
JsonOperator::LongArrow => {
write!(f, "->>")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/ast/mod.rs Outdated
@@ -192,6 +225,12 @@ pub enum Expr {
Identifier(Ident),
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
CompoundIdentifier(Vec<Ident>),
/// JsonIdentifier eg: data->'tags'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// JsonIdentifier eg: data->'tags'
/// JSON access (postgres) eg: data->'tags'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/tokenizer.rs Outdated
@@ -141,6 +141,14 @@ pub enum Token {
PGCubeRoot,
/// `?` or `$` , a prepared statement arg placeholder
Placeholder(String),
/// ->, used as a operator to extract json field in PostgreSQL
Arrow,
/// -->, used as a operator to extract json field as text in PostgreSQL
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// -->, used as a operator to extract json field as text in PostgreSQL
/// ->>, used as a operator to extract json field as text in PostgreSQL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/tokenizer.rs Outdated
@@ -197,6 +205,10 @@ impl fmt::Display for Token {
Token::PGSquareRoot => f.write_str("|/"),
Token::PGCubeRoot => f.write_str("||/"),
Token::Placeholder(ref s) => write!(f, "{}", s),
Token::Arrow => write!(f, "->"),
Token::LongArrow => write!(f, "-->"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Token::LongArrow => write!(f, "-->"),
Token::LongArrow => write!(f, "->>"),

Copy link
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 tests didn't cover this? I think there is a function that verifies that a parsed sql statement is then displayed in the same way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are printing JsonOperator not the Token. so it escaped 😅

Signed-off-by: password <rbalajis25@gmail.com>
Copy link
Contributor

@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.

Thanks @poonai !

@alamb alamb merged commit d035784 into apache:main Apr 19, 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.

3 participants