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

Revert "Support unary '+' operators for strings (#3013)" #3282

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 6 additions & 51 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2483,11 +2483,11 @@ class tsqlBuilder : public tsqlCommonMutator
{
// Check for comparison operators directly followed by an '@@' variable, like =@@
handleAtAtVarInPredicate(ctx);
}
}
void exitUnary_op_expr(TSqlParser::Unary_op_exprContext *ctx) override
{
handleBitNotOperator(ctx);
}
}
void exitPlus_minus_bit_expr(TSqlParser::Plus_minus_bit_exprContext *ctx) override
{
handleBitOperators(ctx);
Expand Down Expand Up @@ -3201,7 +3201,10 @@ class tsqlMutator : public TSqlParserBaseListener
// Check for comparison operators directly followed by an '@@' variable, like =@@
handleAtAtVarInPredicate(ctx);
}

void exitUnary_op_expr(TSqlParser::Unary_op_exprContext *ctx) override
{
handleBitNotOperator(ctx);
}
void exitPlus_minus_bit_expr(TSqlParser::Plus_minus_bit_exprContext *ctx) override
{
handleBitOperators(ctx);
Expand All @@ -3210,54 +3213,6 @@ class tsqlMutator : public TSqlParserBaseListener
{
handleModuloOperator(ctx);
}

void enterUnary_op_expr(TSqlParser::Unary_op_exprContext *ctx) override
{
/*
* The T-SQL grammar allows an arbitrary number of unary '+' operators to precede an expression,
* but PG only supports that for numeric expressions. For string expressions, such a '+' will raise an error in PG.
* In SQL this shows as redundant operators, for example for concatenation: SELECT 'a' ++ 'b'. Expressions
* such as +++(+++@v)) are also valid syntax according to the T-SQL grammar even though they look unusual.
* Here we remove such unary '+' operators, which are redundant anyway.
* However we do not touch numeric constants (e.g. +123) since the '+', although still redundant, may
* have been included for code clarity (e.g. +123 as opposed to -123).
*/
std::string op = getFullText(ctx->op);
if (op.front() == '+') {
auto rhsctx = ctx->expression();
while (true) {
std::string rhs = getFullText(rhsctx);
if (
(rhs.front() == '\'') || // single-quoted strings
(rhs.front() == '"') || // both double-quoted strings and double-quoted identifiers
(rhs.front() == '@') || // variables
(rhs.front() == '(') || // bracketed expressions
(rhs.front() == '[') || // bracket-delimited identifiers
(rhs.front() == '_') || // identifiers starting with an underscore
std::isalpha(rhs.front()) // identifiers as well as the N'...' string notation
) {
stream.setText(ctx->op->getStartIndex(), " ");
break;
}
if (rhs.front() == '+') {
if (dynamic_cast<TSqlParser::Unary_op_exprContext *>(rhsctx)) {
TSqlParser::Unary_op_exprContext *uctx = static_cast<TSqlParser::Unary_op_exprContext *>(rhsctx);
op = getFullText(uctx->op);
if (op.front() == '+') {
rhsctx = uctx->expression();
continue;
}
}
}
break;
}
}
return;
}
void exitUnary_op_expr(TSqlParser::Unary_op_exprContext *ctx) override
{
handleBitNotOperator(ctx);
}

};

Expand Down
11 changes: 0 additions & 11 deletions test/JDBC/expected/unary_plus_op_string-vu-cleanup.out

This file was deleted.

76 changes: 0 additions & 76 deletions test/JDBC/expected/unary_plus_op_string-vu-prepare.out

This file was deleted.

Loading
Loading