Skip to content

Parse cursor queries with antlr before executing #3777

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 12 commits into
base: BABEL_5_X_DEV
Choose a base branch
from

Conversation

sharathbp
Copy link
Contributor

@sharathbp sharathbp commented May 22, 2025

Description

Cursor queries are not parsed with antlr parser before sending it to Postgres parser, which leads to syntax error if there is any psql related keywords in the query. Antlr parser transforms the query by delimit the psql keywords in the query. Adding antlr parser logic for CURSOR queries in Pabelfish before sending it to Postgres for execution.

Issues Resolved

BABEL-5743

Test Scenarios Covered

  • Use case based -
    Yes

  • Boundary conditions -
    Yes

  • Arbitrary inputs -
    Yes

  • Negative test cases -
    Yes

  • Minor version upgrade tests -
    NA

  • Major version upgrade tests -
    NA

  • Performance tests -
    NA

  • Tooling impact -
    NA

  • Client tests -
    NA

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@coveralls
Copy link
Collaborator

coveralls commented May 22, 2025

Pull Request Test Coverage Report for Build 15712163874

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 14 of 15 (93.33%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 75.575%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contrib/babelfishpg_tsql/src/cursor.c 10 11 90.91%
Totals Coverage Status
Change from base Build 15711252052: 0.02%
Covered Lines: 48899
Relevant Lines: 64703

💛 - Coveralls

@sharathbp sharathbp requested review from Deepesh125, sumitj824, tanyagupta17, rohit01010 and Yvinayak07 and removed request for tanyagupta17 and Yvinayak07 May 23, 2025 04:41
@sharathbp sharathbp requested a review from tanscorpio7 May 29, 2025 07:49
* Antlr parser will converts TSQL query into PSQL syntax and properly handles idenfier delimiters,
* allowing PostgreSQL reserved words to be used as column aliases.
*/
if (stmt != NULL && sql_dialect == SQL_DIALECT_TSQL)
Copy link
Contributor

Choose a reason for hiding this comment

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

ANTLR can be a pretty heavy parser, so we maintain a session level cache for the ANTLR parse result for a procedure's body. Again invoking ANTLR here for sp_cursor procs would mean we always ANTLR parse during runtime.
Can we avoid that by doing this in the first parse of the statement itself ?

@sharathbp sharathbp requested a review from KushaalShroff June 10, 2025 09:20
* Antlr parser will converts TSQL query into PSQL syntax and properly handles idenfier delimiters,
* allowing PostgreSQL reserved words to be used as column aliases.
*/
if (prepare && stmt != NULL && sql_dialect == SQL_DIALECT_TSQL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we merge this block with the prepare block below ? Also why do we need dialect check here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't merge it because we want to parse before creating SPI connection because if parse fails SPI connection is not required. Since SPI connection is used outside prepare if statement added it separately before SPI connection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dialect I added as safety as antlr parser should be used only for TSQL context right

Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think we will ever reach here in non TSQL dialect, can you please confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verified its only getting called through ProcessTDSRequest->ProcessRPCRequest->HandleSPCursorOpenCommon

Copy link
Contributor

Choose a reason for hiding this comment

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

Then we dont need the TSQL Dialect check, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I can remove that.

* Antlr parser will converts TSQL query into PSQL syntax and properly handles idenfier delimiters,
* allowing PostgreSQL reserved words to be used as column aliases.
*/
if (prepare && stmt != NULL && sql_dialect == SQL_DIALECT_TSQL)
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think we will ever reach here in non TSQL dialect, can you please confirm?

*/
if (prepare && stmt != NULL && sql_dialect == SQL_DIALECT_TSQL)
{
pltsql_curr_compile = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to set this locally?

@sharathbp sharathbp force-pushed the babel-5743 branch 2 times, most recently from 4d4b7c1 to 8e44cf5 Compare June 17, 2025 15:38
* Antlr parser will converts TSQL query into PSQL syntax and properly handles idenfier delimiters,
* allowing PostgreSQL reserved words to be used as column aliases.
*/
if (prepare && stmt != NULL && sql_dialect == SQL_DIALECT_TSQL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Then we dont need the TSQL Dialect check, right?

Comment on lines +7269 to +7270
if (pltsql_curr_compile)
collation = pltsql_curr_compile->fn_input_collation;
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 possibility that pltsql_curr_compile is NULL here? Thats serious isn't it?

Copy link
Contributor Author

@sharathbp sharathbp Jun 17, 2025

Choose a reason for hiding this comment

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

If its NULL then we will not access it. It will be the assigned value above InvalidOid. Before it was causing seg fault because we were accessing it without null check below so added this.

@tanscorpio7
Copy link
Contributor

tanscorpio7 commented Jun 17, 2025

Please also add test cases for sp_cursor procedures. sp_cursoropen, sp_cursorexecute, etc. Thanks.

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