Skip to content

Commit

Permalink
Fix a corner case '=' not properly rewritten to AS (#3411)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
forestkeeper authored Jan 15, 2025
1 parent 9b3dfa9 commit 05e3fa4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
36 changes: 28 additions & 8 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2936,34 +2936,54 @@ class tsqlBuilder : public tsqlCommonMutator
clear_rewritten_query_fragment();
}

PLtsql_expr *rewrite_if_condition(TSqlParser::Search_conditionContext *ctx)
PLtsql_expr *rewrite_if_condition(TSqlParser::Search_conditionContext *ctx, PLtsql_expr *expr)
{
PLtsql_expr *expr = makeTsqlExpr(ctx, false);
PLtsql_expr_query_mutator mutator(expr, ctx);
add_rewritten_query_fragment_to_mutator(&mutator);
mutator.run();
if (!expr)
expr = makeTsqlExpr(ctx, false);
if (statementMutator)
{
add_rewritten_query_fragment_to_mutator(statementMutator.get());
statementMutator->run();
statementMutator = nullptr;
}
else
{
PLtsql_expr_query_mutator mutator(expr, ctx);
add_rewritten_query_fragment_to_mutator(&mutator);
mutator.run();
}
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())
{
PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = makeTsqlExpr(ctx, false);
clear_rewritten_query_fragment();
statementMutator = std::make_unique<PLtsql_expr_query_mutator>(fragment->cond, ctx);
}
}

void exitSearch_condition(TSqlParser::Search_conditionContext *ctx) override
{
if (!ctx->parent || !ctx->parent->parent)
return;

if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->if_statement())
{

PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = rewrite_if_condition(ctx);
fragment->cond = rewrite_if_condition(ctx, fragment->cond);
}
else if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->while_statement())
{
PLtsql_stmt_while *fragment = (PLtsql_stmt_while *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = rewrite_if_condition(ctx);
fragment->cond = rewrite_if_condition(ctx, fragment->cond);
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions test/JDBC/expected/BABEL-2514.out
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,14 @@ int#!#int

drop view if exists babel_2514_complex_view;
go

IF EXISTS
(
SELECT tblnam = a.name FROM sysobjects a where a.name = 'tablename'
) select 'Exists'; ELSE select 'Not Exists';
GO
~~START~~
varchar
Not Exists
~~END~~

8 changes: 7 additions & 1 deletion test/JDBC/input/BABEL-2514.sql
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ go
select * from babel_2514_complex_view order by b;
go
drop view if exists babel_2514_complex_view;
go
go

IF EXISTS
(
SELECT tblnam = a.name FROM sysobjects a where a.name = 'tablename'
) select 'Exists'; ELSE select 'Not Exists';
GO

0 comments on commit 05e3fa4

Please sign in to comment.