From 54555800223fc2db9d26b1ecee1f6b9c8874f9ae Mon Sep 17 00:00:00 2001 From: Dehowe Feng <8065116+dehowef@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:33:57 -0800 Subject: [PATCH] Implement EXISTS subquery for CASE (#1345) Modified logic of EXISTS to wrap itself in a agtype cast. This allows EXISTS to work with in CASE statements, among other implementations that may require an agtype. Also renamed the wrapper function to be more generic to denote its usage in other implementations if need be. Added regression tests for EXISTS --- regress/expected/expr.out | 90 +++++++++++++++++++++++++++----- regress/sql/expr.sql | 50 ++++++++++++++++-- src/backend/parser/cypher_gram.y | 18 ++++--- 3 files changed, 134 insertions(+), 24 deletions(-) diff --git a/regress/expected/expr.out b/regress/expected/expr.out index 7e88001e0..aa7ddfb87 100644 --- a/regress/expected/expr.out +++ b/regress/expected/expr.out @@ -6398,8 +6398,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(*) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6416,8 +6416,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(*) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6434,8 +6434,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(n) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6452,8 +6452,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(n) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6470,8 +6470,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(1) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6488,8 +6488,8 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(1) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); - j | case_statement +$$ ) AS (n agtype, case_statement agtype); + n | case_statement ------------------------------------------------------------------------------------------------+---------------- {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "not count" {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "not count" @@ -6499,6 +6499,72 @@ $$ ) AS (j agtype, case_statement agtype); {"id": 281474976710662, "label": "", "properties": {"i": {}, "j": {"i": 1}, "id": 6}}::vertex | "not count" (6 rows) +--CASE with EXISTS() +--exists(n.property) +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE exists(n.j) + WHEN true THEN 'property j exists' + ELSE 'property j does not exist' + END +$$ ) AS (n agtype, case_statement agtype); + n | case_statement +------------------------------------------------------------------------------------------------+----------------------------- + {"id": 281474976710657, "label": "", "properties": {"i": 1, "id": 1}}::vertex | "property j does not exist" + {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "property j exists" + {"id": 281474976710659, "label": "", "properties": {"i": 0, "j": 1, "id": 3}}::vertex | "property j exists" + {"id": 281474976710660, "label": "", "properties": {"i": true, "j": false, "id": 4}}::vertex | "property j exists" + {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "property j exists" + {"id": 281474976710662, "label": "", "properties": {"i": {}, "j": {"i": 1}, "id": 6}}::vertex | "property j exists" +(6 rows) + +--CASE evaluates to boolean true, is not a boolean, should hit ELSE +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE exists(n.j) + WHEN 1 THEN 'should not output me' + ELSE '1 is not a boolean' + END +$$ ) AS (n agtype, case_statement agtype); + n | case_statement +------------------------------------------------------------------------------------------------+---------------------- + {"id": 281474976710657, "label": "", "properties": {"i": 1, "id": 1}}::vertex | "1 is not a boolean" + {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "1 is not a boolean" + {"id": 281474976710659, "label": "", "properties": {"i": 0, "j": 1, "id": 3}}::vertex | "1 is not a boolean" + {"id": 281474976710660, "label": "", "properties": {"i": true, "j": false, "id": 4}}::vertex | "1 is not a boolean" + {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "1 is not a boolean" + {"id": 281474976710662, "label": "", "properties": {"i": {}, "j": {"i": 1}, "id": 6}}::vertex | "1 is not a boolean" +(6 rows) + +--exists in WHEN, vacuously false because exists(n.j) evaluates to a boolean, n is a vertex +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE n + WHEN exists(n.j) THEN 'should not output me' + ELSE 'n is a vertex, not a boolean' + END +$$ ) AS (j agtype, case_statement agtype); + j | case_statement +------------------------------------------------------------------------------------------------+-------------------------------- + {"id": 281474976710657, "label": "", "properties": {"i": 1, "id": 1}}::vertex | "n is a vertex, not a boolean" + {"id": 281474976710658, "label": "", "properties": {"i": "a", "j": "b", "id": 2}}::vertex | "n is a vertex, not a boolean" + {"id": 281474976710659, "label": "", "properties": {"i": 0, "j": 1, "id": 3}}::vertex | "n is a vertex, not a boolean" + {"id": 281474976710660, "label": "", "properties": {"i": true, "j": false, "id": 4}}::vertex | "n is a vertex, not a boolean" + {"id": 281474976710661, "label": "", "properties": {"i": [], "j": [0, 1, 2], "id": 5}}::vertex | "n is a vertex, not a boolean" + {"id": 281474976710662, "label": "", "properties": {"i": {}, "j": {"i": 1}, "id": 6}}::vertex | "n is a vertex, not a boolean" +(6 rows) + +--exists(*) (should fail) +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE n.j + WHEN 1 THEN exists(*) + ELSE 'not count' + END +$$ ) AS (j agtype, case_statement agtype); +ERROR: syntax error at or near "*" +LINE 4: WHEN 1 THEN exists(*) + ^ -- RETURN * and (u)--(v) optional forms SELECT create_graph('opt_forms'); NOTICE: graph "opt_forms" has been created diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql index dbaef17cc..f801c4488 100644 --- a/regress/sql/expr.sql +++ b/regress/sql/expr.sql @@ -2682,7 +2682,7 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(*) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); +$$ ) AS (n agtype, case_statement agtype); --concatenated SELECT * FROM cypher('case_statement', $$ @@ -2691,7 +2691,7 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(*) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); +$$ ) AS (n agtype, case_statement agtype); --count(n) SELECT * FROM cypher('case_statement', $$ @@ -2700,7 +2700,7 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(n) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); +$$ ) AS (n agtype, case_statement agtype); --concatenated SELECT * FROM cypher('case_statement', $$ @@ -2709,7 +2709,7 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(n) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); +$$ ) AS (n agtype, case_statement agtype); --count(1) SELECT * FROM cypher('case_statement', $$ @@ -2718,7 +2718,7 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(1) ELSE 'not count' END -$$ ) AS (j agtype, case_statement agtype); +$$ ) AS (n agtype, case_statement agtype); --concatenated SELECT * FROM cypher('case_statement', $$ @@ -2727,8 +2727,48 @@ SELECT * FROM cypher('case_statement', $$ WHEN 1 THEN count(1) ELSE 'not count' END +$$ ) AS (n agtype, case_statement agtype); + +--CASE with EXISTS() + +--exists(n.property) +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE exists(n.j) + WHEN true THEN 'property j exists' + ELSE 'property j does not exist' + END +$$ ) AS (n agtype, case_statement agtype); + +--CASE evaluates to boolean true, is not a boolean, should hit ELSE +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE exists(n.j) + WHEN 1 THEN 'should not output me' + ELSE '1 is not a boolean' + END +$$ ) AS (n agtype, case_statement agtype); + +--exists in WHEN, vacuously false because exists(n.j) evaluates to a boolean, n is a vertex +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE n + WHEN exists(n.j) THEN 'should not output me' + ELSE 'n is a vertex, not a boolean' + END $$ ) AS (j agtype, case_statement agtype); +--exists(*) (should fail) +SELECT * FROM cypher('case_statement', $$ + MATCH (n) + RETURN n, CASE n.j + WHEN 1 THEN exists(*) + ELSE 'not count' + END +$$ ) AS (j agtype, case_statement agtype); + + + -- RETURN * and (u)--(v) optional forms SELECT create_graph('opt_forms'); diff --git a/src/backend/parser/cypher_gram.y b/src/backend/parser/cypher_gram.y index 1ec932e7e..273ae6d4b 100644 --- a/src/backend/parser/cypher_gram.y +++ b/src/backend/parser/cypher_gram.y @@ -227,7 +227,7 @@ static Node *make_typecast_expr(Node *expr, char *typecast, int location); static Node *make_function_expr(List *func_name, List *exprs, int location); static Node *make_star_function_expr(List *func_name, List *exprs, int location); static Node *make_distinct_function_expr(List *func_name, List *exprs, int location); -static FuncCall *wrap_pg_funccall_to_agtype(Node* fnode, char *type, int location); +static FuncCall *node_to_agtype(Node* fnode, char *type, int location); // setops static Node *make_set_op(SetOperation op, bool all_or_distinct, List *larg, @@ -1728,12 +1728,16 @@ expr_func_subexpr: n->operName = NIL; n->subselect = (Node *) sub; n->location = @1; - $$ = (Node *) n; + $$ = (Node *)node_to_agtype((Node *)n, "boolean", @1); } | EXISTS '(' property_value ')' { - $$ = make_function_expr(list_make1(makeString("exists")), + FuncCall *n; + n = makeFuncCall(list_make1(makeString("exists")), list_make1($3), @2); + + $$ = (Node *)node_to_agtype((Node *)n, "boolean", @2); + } ; @@ -2266,7 +2270,7 @@ static Node *make_function_expr(List *func_name, List *exprs, int location) fnode = makeFuncCall(funcname, exprs, location); /* build the cast to wrap the function call to return agtype. */ - fnode = wrap_pg_funccall_to_agtype((Node *)fnode, "integer", location); + fnode = node_to_agtype((Node *)fnode, "integer", location); return (Node *)fnode; } @@ -2322,7 +2326,7 @@ static Node *make_star_function_expr(List *func_name, List *exprs, int location) fnode->agg_star = true; /* build the cast to wrap the function call to return agtype. */ - fnode = wrap_pg_funccall_to_agtype((Node *)fnode, "integer", location); + fnode = node_to_agtype((Node *)fnode, "integer", location); return (Node *)fnode; } @@ -2380,7 +2384,7 @@ static Node *make_distinct_function_expr(List *func_name, List *exprs, int locat fnode->agg_distinct = true; /* build the cast to wrap the function call to return agtype. */ - fnode = wrap_pg_funccall_to_agtype((Node *)fnode, "integer", location); + fnode = node_to_agtype((Node *)fnode, "integer", location); return (Node *)fnode; } else @@ -2411,7 +2415,7 @@ static Node *make_distinct_function_expr(List *func_name, List *exprs, int locat * helper function to wrap pg_function in the appropiate typecast function to * interface with AGE components */ -static FuncCall *wrap_pg_funccall_to_agtype(Node * fnode, char *type, int location) +static FuncCall *node_to_agtype(Node * fnode, char *type, int location) { List *funcname = list_make1(makeString("ag_catalog"));