Skip to content

Commit

Permalink
Fix parsing of negative integer constants
Browse files Browse the repository at this point in the history
Currently the value of negative constants are omitted. Only omit *0*
constants.

An earlier attempt (pganalyze#244) caused a mismatch with the protobuf json
output, since that wants to omit values that are equal to the default
value.

I don't *really* think that is particularly sensible behavior for an
integer constant in an AST, but oh well.

Fixes pganalyze#243.
  • Loading branch information
msullivan committed May 9, 2024
1 parent 0779c5e commit c773fdd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/pg_query_outfuncs_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ _outOidList(StringInfo out, const List *node)
static void
_outInteger(StringInfo out, const Integer *node)
{
if (node->ival > 0)
/* Don't output anything if the value is the default (0), to match
* protobuf's behavior. */
if (node->ival != 0)
appendStringInfo(out, "\"ival\":%d", node->ival);
}

Expand Down
4 changes: 4 additions & 0 deletions test/parse_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ const char* tests[] = {
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"boolval\":{},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"SELECT NULL",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"isnull\":true,\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"SELECT 0",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"SELECT 1",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":1},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"SELECT -1",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":-1},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"SELECT 1; SELECT 2",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":1},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}},\"stmt_len\":8},{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":2},\"location\":17}},\"location\":17}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}},\"stmt_location\":9}]}",
"select sum(unique1) FILTER (WHERE unique1 IN (SELECT unique1 FROM onek where unique1 < 100)) FROM tenk1",
Expand Down

0 comments on commit c773fdd

Please sign in to comment.