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

Avoid json outfuncs key duplication #246

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 1 addition & 10 deletions src/pg_query_outfuncs_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ _outOidList(StringInfo out, const List *node)
static void
_outInteger(StringInfo out, const Integer *node)
{
if (node->ival > 0)
appendStringInfo(out, "\"ival\":%d", node->ival);
appendStringInfo(out, "\"ival\":%d", node->ival);
}

static void
Expand Down Expand Up @@ -242,27 +241,19 @@ _outAConst(StringInfo out, const A_Const *node)
} else {
switch (node->val.node.type) {
case T_Integer:
appendStringInfoString(out, "\"ival\":{");
_outInteger(out, &node->val.ival);
appendStringInfoChar(out, '}');
break;
case T_Float:
appendStringInfoString(out, "\"fval\":{");
_outFloat(out, &node->val.fval);
appendStringInfoChar(out, '}');
break;
case T_Boolean:
appendStringInfo(out, "\"boolval\":{%s}", node->val.boolval.boolval ? "\"boolval\":true" : "");
break;
case T_String:
appendStringInfoString(out, "\"sval\":{");
_outString(out, &node->val.sval);
appendStringInfoChar(out, '}');
break;
case T_BitString:
appendStringInfoString(out, "\"bsval\":{");
_outBitString(out, &node->val.bsval);
appendStringInfoChar(out, '}');
break;

// Unreachable, A_Const cannot contain any other nodes.
Expand Down
12 changes: 6 additions & 6 deletions test/parse_opts_tests.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const char* tests[] = {
"SELECT 1",
"0",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":1},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":1,\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"integer",
"1",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"List\":{\"items\":[{\"String\":{\"sval\":\"pg_catalog\"}},{\"String\":{\"sval\":\"int4\"}}]}}}]}",
Expand All @@ -10,19 +10,19 @@ const char* tests[] = {
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"List\":{\"items\":[{\"String\":{\"sval\":\"pg_catalog\"}},{\"String\":{\"sval\":\"varchar\"}}]}}}]}",
"EXISTS(SELECT 1)",
"2",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"SubLink\":{\"subLinkType\":\"EXISTS_SUBLINK\",\"subselect\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":1},\"location\":14}},\"location\":14}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"SubLink\":{\"subLinkType\":\"EXISTS_SUBLINK\",\"subselect\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":1,\"location\":14}},\"location\":14}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"v_version IS NULL",
"2",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"NullTest\":{\"arg\":{\"ColumnRef\":{\"fields\":[{\"String\":{\"sval\":\"v_version\"}}]}},\"nulltesttype\":\"IS_NULL\",\"location\":10}}}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"pos:= instr($1, $2, 1)",
"3",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"pos\",\"nnames\":1,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"instr\"}}],\"args\":[{\"ParamRef\":{\"number\":1,\"location\":12}},{\"ParamRef\":{\"number\":2,\"location\":16}},{\"A_Const\":{\"ival\":{\"ival\":1},\"location\":20}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":6}},\"location\":6}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"pos\",\"nnames\":1,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"instr\"}}],\"args\":[{\"ParamRef\":{\"number\":1,\"location\":12}},{\"ParamRef\":{\"number\":2,\"location\":16}},{\"A_Const\":{\"ival\":1,\"location\":20}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":6}},\"location\":6}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"temp_str := substring(string FROM beg_index)",
"3",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"temp_str\",\"nnames\":1,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"pg_catalog\"}},{\"String\":{\"sval\":\"substring\"}}],\"args\":[{\"ColumnRef\":{\"fields\":[{\"String\":{\"sval\":\"string\"}}],\"location\":22}},{\"ColumnRef\":{\"fields\":[{\"String\":{\"sval\":\"beg_index\"}}],\"location\":34}}],\"funcformat\":\"COERCE_SQL_SYNTAX\",\"location\":12}},\"location\":12}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"v3.c1 := 4",
"4",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"v3\",\"indirection\":[{\"String\":{\"sval\":\"c1\"}}],\"nnames\":2,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":{\"ival\":4},\"location\":9}},\"location\":9}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"v3\",\"indirection\":[{\"String\":{\"sval\":\"c1\"}}],\"nnames\":2,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"ival\":4,\"location\":9}},\"location\":9}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"NEW.name = upper(cleanString(NEW.name))",
"4",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"new\",\"indirection\":[{\"String\":{\"sval\":\"name\"}}],\"nnames\":2,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"upper\"}}],\"args\":[{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"cleanstring\"}}],\"args\":[{\"ColumnRef\":{\"fields\":[{\"String\":{\"sval\":\"new\"}},{\"String\":{\"sval\":\"name\"}}],\"location\":29}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":17}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":11}},\"location\":11}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
Expand All @@ -31,10 +31,10 @@ const char* tests[] = {
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"new\",\"indirection\":[{\"String\":{\"sval\":\"author\"}},{\"String\":{\"sval\":\"first_name\"}}],\"nnames\":3,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"upper\"}}],\"args\":[{\"FuncCall\":{\"funcname\":[{\"String\":{\"sval\":\"cleanstring\"}}],\"args\":[{\"ColumnRef\":{\"fields\":[{\"String\":{\"sval\":\"new\"}},{\"String\":{\"sval\":\"author\"}},{\"String\":{\"sval\":\"first_name\"}}],\"location\":42}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":30}}],\"funcformat\":\"COERCE_EXPLICIT_CALL\",\"location\":24}},\"location\":24}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"SELECT '\\''",
"96",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"sval\":{\"sval\":\"'\"},\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"SelectStmt\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"sval\":\"'\",\"location\":7}},\"location\":7}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}]}",
"v3.c1 := '\\''",
"100",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"v3\",\"indirection\":[{\"String\":{\"sval\":\"c1\"}}],\"nnames\":2,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"sval\":{\"sval\":\"'\"},\"location\":9}},\"location\":9}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
"{\"version\":160001,\"stmts\":[{\"stmt\":{\"PLAssignStmt\":{\"name\":\"v3\",\"indirection\":[{\"String\":{\"sval\":\"c1\"}}],\"nnames\":2,\"val\":{\"targetList\":[{\"ResTarget\":{\"val\":{\"A_Const\":{\"sval\":\"'\",\"location\":9}},\"location\":9}}],\"limitOption\":\"LIMIT_OPTION_DEFAULT\",\"op\":\"SETOP_NONE\"}}}}]}",
};

size_t testsLength = __LINE__ - 4;
Loading
Loading