Skip to content

Commit

Permalink
Revert "Support unary '+' operators for strings (#3013)" (#3282)
Browse files Browse the repository at this point in the history
This reverts commit 8ad86ff.
[PR 3013](#3013) incorrectly ended up in BABEL_4_4_STABLE.

Signed-off-by: Rob Verschoor <[email protected]>
  • Loading branch information
robverschoor authored Dec 18, 2024
1 parent 129f2a3 commit d60bd99
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 1,347 deletions.
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

0 comments on commit d60bd99

Please sign in to comment.