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

Wrong deparse of particular BooleanTest #206

Closed
lelit opened this issue Aug 24, 2023 · 1 comment · Fixed by #207
Closed

Wrong deparse of particular BooleanTest #206

lelit opened this issue Aug 24, 2023 · 1 comment · Fixed by #207

Comments

@lelit
Copy link
Contributor

lelit commented Aug 24, 2023

Yesterday I fixed this issue in pglast, and trying now I see that the same problem afflicts also the deparse() machinery.

Basically,

SELECT (false AND true) IS FALSE

and

SELECT a = (true IS FALSE)

come out without the needed parens.

I was able to mimick the fix for the first case, but I'm not sure how to address the second case: in pglast I consulted the BooleanTest's parent node, and when its a A_Expr I encoded it within parens.

Here is a diff, with test cases and the mentioned fix:

diff --git a/src/postgres_deparse.c b/src/postgres_deparse.c
index e4cac50..a9c5bd0 100644
--- a/src/postgres_deparse.c
+++ b/src/postgres_deparse.c
@@ -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:
diff --git a/test/deparse_tests.c b/test/deparse_tests.c
index 2860797..8ac26bf 100644
--- a/test/deparse_tests.c
+++ b/test/deparse_tests.c
@@ -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;

Maybe if you can gimme some hint I may find some time to implement a complete PR fixing the issue.

Thanks&bye!

@lelit
Copy link
Contributor Author

lelit commented Aug 26, 2023

Well, dunno what confused me at the first attempt, but I found my way... going the other way around! 😋

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 a pull request may close this issue.

1 participant