Skip to content

Commit

Permalink
Fix deparse of particular BooleanTest expressions
Browse files Browse the repository at this point in the history
Fixes #206.
  • Loading branch information
lelit committed Aug 26, 2023
1 parent 9b21e32 commit 4c3a7ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/postgres_deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2879,8 +2879,8 @@ static void deparseAExpr(StringInfo str, A_Expr* a_expr, DeparseNodeContext cont
ListCell *lc;
char *name;

bool need_lexpr_parens = a_expr->lexpr != NULL && (IsA(a_expr->lexpr, BoolExpr) || IsA(a_expr->lexpr, NullTest) || IsA(a_expr->lexpr, A_Expr));
bool need_rexpr_parens = a_expr->rexpr != NULL && (IsA(a_expr->rexpr, BoolExpr) || IsA(a_expr->rexpr, NullTest) || IsA(a_expr->rexpr, A_Expr));
bool need_lexpr_parens = a_expr->lexpr != NULL && (IsA(a_expr->lexpr, BoolExpr) || IsA(a_expr->lexpr, BooleanTest) || IsA(a_expr->lexpr, NullTest) || IsA(a_expr->lexpr, A_Expr));
bool need_rexpr_parens = a_expr->rexpr != NULL && (IsA(a_expr->rexpr, BoolExpr) || IsA(a_expr->rexpr, BooleanTest) || IsA(a_expr->rexpr, NullTest) || IsA(a_expr->rexpr, A_Expr));

switch (a_expr->kind) {
case AEXPR_OP: /* normal operator */
Expand Down Expand Up @@ -3950,7 +3950,16 @@ static void deparseMinMaxExpr(StringInfo str, MinMaxExpr *min_max_expr)

static void deparseBooleanTest(StringInfo str, BooleanTest *boolean_test)
{
bool need_parens = IsA(boolean_test->arg, BoolExpr);

if (need_parens)
appendStringInfoChar(str, '(');

deparseExpr(str, (Node *) boolean_test->arg);

if (need_parens)
appendStringInfoChar(str, ')');

switch (boolean_test->booltesttype)
{
case IS_TRUE:
Expand Down
2 changes: 2 additions & 0 deletions test/deparse_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ const char* tests[] = {
"CREATE PROCEDURE returns_one() LANGUAGE sql BEGIN ATOMIC RETURN 1; END",
"CREATE PROCEDURE updates_and_returns_one() LANGUAGE sql BEGIN ATOMIC UPDATE tbl SET a = 1; RETURN 1; END",
"SELECT 1 FROM tbl LIMIT COALESCE($1, $2)",
"SELECT (false AND true) IS FALSE",
"SELECT a = (true IS FALSE)",
};

size_t testsLength = __LINE__ - 4;

0 comments on commit 4c3a7ce

Please sign in to comment.