Skip to content
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

Fix a corner case '=' not properly rewritten to AS #3405

Merged

Conversation

forestkeeper
Copy link
Contributor

Previously we rewrite "SELECT COL=expr" to "SELECT expr as "COL"", but there's a corner case that for if condition, it didn't do the rewrite, because different parser path from if condition to dml statement.

This commit fix this issue by adding the mutator logic to if statement rewrite.

Task: BABEL-5320
Signed-off-by: Zhibai Song [email protected]

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.

Zhibai Song added 2 commits January 15, 2025 07:29
Previously we rewrite "SELECT COL=expr" to "SELECT expr as "COL"", but
there's a corner case that for if condition, it didn't do the rewrite,
because different parser path from if condition to dml statement.

This commit fix this issue by adding the mutator logic to if statement
rewrite.

Task: BABEL-5320
Signed-off-by: Zhibai Song <[email protected]>
@coveralls
Copy link
Collaborator

coveralls commented Jan 15, 2025

Pull Request Test Coverage Report for Build 12796200197

Details

  • 19 of 19 (100.0%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.002%) to 74.961%

Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tsql/runtime/functions.c 1 83.16%
contrib/babelfishpg_tds/src/backend/tds/tds.c 1 92.39%
contrib/babelfishpg_tds/src/backend/tds/tdscomm.c 2 76.03%
Totals Coverage Status
Change from base Build 12782058233: -0.002%
Covered Lines: 46996
Relevant Lines: 62694

💛 - Coveralls

@forestkeeper forestkeeper merged commit 2634a7f into babelfish-for-postgresql:BABEL_5_X_DEV Jan 15, 2025
46 checks passed
forestkeeper added a commit to amazon-aurora/babelfish_extensions that referenced this pull request Jan 15, 2025
…tgresql#3405)


Previously we rewrite "SELECT COL=expr" to "SELECT expr as "COL"", but
there's a corner case that for if condition, it didn't do the rewrite,
because different parser path from if condition to dml statement.

This commit fix this issue by adding the mutator logic to if statement
rewrite.

Task: BABEL-5320
Signed-off-by: Zhibai Song <[email protected]>
Comment on lines 201 to 205
IF EXISTS
(
SELECT tblnam = a.name FROM sysobjects a where a.name = 'tablename'
) PRINT 'Exists'; ELSE PRINT 'Not Exists';
GO
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add test cases where we have similar statement inside procedure?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can you add tests where if condition itself gets re-written? For example,

IF EXISTS(
select trim(col) = '' from tab1 where ...
) PRINT 'Exists'; ELSE PRINT 'Not Exists';

{
PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = makeTsqlExpr(ctx, false);
clear_rewritten_query_fragment();
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we calling clear_rewritten_query_fragment without calling add_rewritten_query_fragment_to_mutator?

clear_rewritten_query_fragment();

/* Now we can prepend SELECT to rewritten search_condition */
expr->query = strdup((std::string("SELECT ") + std::string(expr->query)).c_str());
return expr;
}

void enterSearch_condition(TSqlParser::Search_conditionContext *ctx) override
{
if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->if_statement())
Copy link
Contributor

Choose a reason for hiding this comment

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

Need Sanity check?

		if (!ctx->parent || !ctx->parent->parent)
			return;

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should use dynamic_cast function instead of directly casting it

clear_rewritten_query_fragment();

/* Now we can prepend SELECT to rewritten search_condition */
expr->query = strdup((std::string("SELECT ") + std::string(expr->query)).c_str());
return expr;
}

void enterSearch_condition(TSqlParser::Search_conditionContext *ctx) override
Copy link
Contributor

Choose a reason for hiding this comment

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

Didnt get why we need this change?

{
if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->if_statement())
{
PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a naive question, I see there are multiple possible parent->parent of search_condition like

table_sources->table_source_item->search_condition

So are we saying that only in few conditions there will be parse tree path to if_condition which will always be parent->parent?

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.

5 participants