Skip to content

Commit

Permalink
Implement chained expression order of operations (apache#1402)
Browse files Browse the repository at this point in the history
Implements functionality for order of operations (parentheses) in
chained operations.

Added new nodes and functions to accomodate this feature.

Added relevant regression tests for order of operations in chained
expressions.

Modified a previous regression test case that had its location
reporting changed by this implementation.
  • Loading branch information
dehowef committed Nov 18, 2023
1 parent 0c264d7 commit 689b456
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 27 deletions.
4 changes: 2 additions & 2 deletions regress/expected/cypher_call.out
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ HINT: No function matches the given name and argument types. You might need to
/* CALL YIELD WHERE, should fail */
SELECT * FROM cypher('cypher_call', $$CALL sqrt(64) YIELD sqrt WHERE sqrt > 1$$) as (sqrt agtype);
ERROR: Cannot use standalone CALL with WHERE
LINE 2: ...r('cypher_call', $$CALL sqrt(64) YIELD sqrt WHERE sqrt > 1$$...
^
LINE 2: SELECT * FROM cypher('cypher_call', $$CALL sqrt(64) YIELD sq...
^
HINT: Instead use `CALL ... WITH * WHERE ... RETURN *`
/*
* subquery
Expand Down
129 changes: 129 additions & 0 deletions regress/expected/expr.out
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,135 @@ SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE NOT 35 < u.age + 1 <=
{"id": 844424930131973, "label": "people", "properties": {"age": 15, "name": "David"}}::vertex
(3 rows)

-- order of operations
-- expressions
SELECT * FROM cypher('chained', $$ RETURN 1 = 1 = 1 $$) AS (result agtype);
result
--------
true
(1 row)

SELECT * FROM cypher('chained', $$ RETURN 1 = 2 = 1 $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN (1 = 1) = 1 $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN 1 = (1 = 1) $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN 1 = 1 = true $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN (1 = 1) = true $$) AS (result agtype);
result
--------
true
(1 row)

SELECT * FROM cypher('chained', $$ RETURN true = ((1 = 1) = true) $$) AS (result agtype);
result
--------
true
(1 row)

SELECT * FROM cypher('chained', $$ RETURN ((1 = 1) = 1) = 1 $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN (1 = (1 = 1)) = 1 $$) AS (result agtype);
result
--------
false
(1 row)

SELECT * FROM cypher('chained', $$ RETURN ((1 = (1 = 1)) = 1) = 1 $$) AS (result agtype);
result
--------
false
(1 row)

-- in clause
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE 35 = u.age = 35 RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------
{"id": 844424930131971, "label": "people", "properties": {"age": 35, "name": "Samantha"}}::vertex
(1 row)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (35 = u.age) = 35 RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (35 = 35) = u.age RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = u.age = u.age RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------
{"id": 844424930131969, "label": "people", "properties": {"age": 50, "name": "Jason"}}::vertex
{"id": 844424930131970, "label": "people", "properties": {"age": 25, "name": "Amy"}}::vertex
{"id": 844424930131971, "label": "people", "properties": {"age": 35, "name": "Samantha"}}::vertex
{"id": 844424930131972, "label": "people", "properties": {"age": 40, "name": "Mark"}}::vertex
{"id": 844424930131973, "label": "people", "properties": {"age": 15, "name": "David"}}::vertex
(5 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (u.age = u.age) = u.age RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = (u.age = u.age) RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (u.age = u.age) = (u.age = u.age) RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------
{"id": 844424930131969, "label": "people", "properties": {"age": 50, "name": "Jason"}}::vertex
{"id": 844424930131970, "label": "people", "properties": {"age": 25, "name": "Amy"}}::vertex
{"id": 844424930131971, "label": "people", "properties": {"age": 35, "name": "Samantha"}}::vertex
{"id": 844424930131972, "label": "people", "properties": {"age": 40, "name": "Mark"}}::vertex
{"id": 844424930131973, "label": "people", "properties": {"age": 15, "name": "David"}}::vertex
(5 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = (u.age = (u.age = u.age)) RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = 35 = ((u.age = u.age) = u.age) RETURN u $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE ((u.age = u.age) = (u.age = u.age)) = (u.age = u.age) RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------
{"id": 844424930131969, "label": "people", "properties": {"age": 50, "name": "Jason"}}::vertex
{"id": 844424930131970, "label": "people", "properties": {"age": 25, "name": "Amy"}}::vertex
{"id": 844424930131971, "label": "people", "properties": {"age": 35, "name": "Samantha"}}::vertex
{"id": 844424930131972, "label": "people", "properties": {"age": 40, "name": "Mark"}}::vertex
{"id": 844424930131973, "label": "people", "properties": {"age": 15, "name": "David"}}::vertex
(5 rows)

--
-- Test transform logic for IS NULL & IS NOT NULL
--
Expand Down
25 changes: 25 additions & 0 deletions regress/sql/expr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,31 @@ SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE 35 < u.age + 1 <= 50 R
-- should return 3
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE NOT 35 < u.age + 1 <= 50 RETURN u $$) AS (result agtype);

-- order of operations
-- expressions
SELECT * FROM cypher('chained', $$ RETURN 1 = 1 = 1 $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN 1 = 2 = 1 $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN (1 = 1) = 1 $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN 1 = (1 = 1) $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN 1 = 1 = true $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN (1 = 1) = true $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN true = ((1 = 1) = true) $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN ((1 = 1) = 1) = 1 $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN (1 = (1 = 1)) = 1 $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ RETURN ((1 = (1 = 1)) = 1) = 1 $$) AS (result agtype);

-- in clause
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE 35 = u.age = 35 RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (35 = u.age) = 35 RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (35 = 35) = u.age RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = u.age = u.age RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (u.age = u.age) = u.age RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = (u.age = u.age) RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE (u.age = u.age) = (u.age = u.age) RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = (u.age = (u.age = u.age)) RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE u.age = 35 = ((u.age = u.age) = u.age) RETURN u $$) AS (result agtype);
SELECT * FROM cypher('chained', $$ MATCH (u:people) WHERE ((u.age = u.age) = (u.age = u.age)) = (u.age = u.age) RETURN u $$) AS (result agtype);

--
-- Test transform logic for IS NULL & IS NOT NULL
--
Expand Down
4 changes: 4 additions & 0 deletions src/backend/nodes/ag_nodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const char *node_names[] = {
"cypher_param",
"cypher_map",
"cypher_list",
"cypher_comparison_aexpr",
"cypher_comparison_boolexpr",
"cypher_string_match",
"cypher_typecast",
"cypher_integer_const",
Expand Down Expand Up @@ -111,6 +113,8 @@ const ExtensibleNodeMethods node_methods[] = {
DEFINE_NODE_METHODS(cypher_param),
DEFINE_NODE_METHODS(cypher_map),
DEFINE_NODE_METHODS(cypher_list),
DEFINE_NODE_METHODS(cypher_comparison_aexpr),
DEFINE_NODE_METHODS(cypher_comparison_boolexpr),
DEFINE_NODE_METHODS(cypher_string_match),
DEFINE_NODE_METHODS(cypher_typecast),
DEFINE_NODE_METHODS(cypher_integer_const),
Expand Down
22 changes: 22 additions & 0 deletions src/backend/nodes/cypher_outfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ void out_cypher_list(StringInfo str, const ExtensibleNode *node)
WRITE_LOCATION_FIELD(location);
}

// serialization function for the cypher_comparison_aexpr ExtensibleNode.
void out_cypher_comparison_aexpr(StringInfo str, const ExtensibleNode *node)
{
DEFINE_AG_NODE(cypher_comparison_aexpr);

WRITE_ENUM_FIELD(kind, A_Expr_Kind);
WRITE_NODE_FIELD(name);
WRITE_NODE_FIELD(lexpr);
WRITE_NODE_FIELD(rexpr);
WRITE_LOCATION_FIELD(location);
}

// serialization function for the cypher_comparison_boolexpr ExtensibleNode.
void out_cypher_comparison_boolexpr(StringInfo str, const ExtensibleNode *node)
{
DEFINE_AG_NODE(cypher_comparison_boolexpr);

WRITE_ENUM_FIELD(boolop, BoolExprType);
WRITE_NODE_FIELD(args);
WRITE_LOCATION_FIELD(location);
}

// serialization function for the cypher_string_match ExtensibleNode.
void out_cypher_string_match(StringInfo str, const ExtensibleNode *node)
{
Expand Down
47 changes: 47 additions & 0 deletions src/backend/parser/cypher_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ static Node *transform_ColumnRef(cypher_parsestate *cpstate, ColumnRef *cref);
static Node *transform_A_Indirection(cypher_parsestate *cpstate,
A_Indirection *a_ind);
static Node *transform_AEXPR_OP(cypher_parsestate *cpstate, A_Expr *a);
static Node *transform_cypher_comparison_aexpr_OP(cypher_parsestate *cpstate,
cypher_comparison_aexpr *a);
static Node *transform_BoolExpr(cypher_parsestate *cpstate, BoolExpr *expr);
static Node *transform_cypher_comparison_boolexpr(cypher_parsestate *cpstate,
cypher_comparison_boolexpr *b);
static Node *transform_cypher_bool_const(cypher_parsestate *cpstate,
cypher_bool_const *bc);
static Node *transform_cypher_integer_const(cypher_parsestate *cpstate,
Expand Down Expand Up @@ -193,6 +197,12 @@ static Node *transform_cypher_expr_recurse(cypher_parsestate *cpstate,
if (is_ag_node(expr, cypher_typecast))
return transform_cypher_typecast(cpstate,
(cypher_typecast *)expr);
if (is_ag_node(expr, cypher_comparison_aexpr))
return transform_cypher_comparison_aexpr_OP(cpstate,
(cypher_comparison_aexpr *)expr);
if (is_ag_node(expr, cypher_comparison_boolexpr))
return transform_cypher_comparison_boolexpr(cpstate,
(cypher_comparison_boolexpr *)expr);
ereport(ERROR,
(errmsg_internal("unrecognized ExtensibleNode: %s",
((ExtensibleNode *)expr)->extnodename)));
Expand Down Expand Up @@ -477,6 +487,25 @@ static Node *transform_AEXPR_OP(cypher_parsestate *cpstate, A_Expr *a)
a->location);
}

/*
* function for transforming cypher comparision A_Expr. Since this node is a
* wrapper to let us know when a comparison occurs in a chained comparison,
* we convert it to a regular A_expr and transform it.
*/
static Node *transform_cypher_comparison_aexpr_OP(cypher_parsestate *cpstate,
cypher_comparison_aexpr *a)
{
A_Expr *n = makeNode(A_Expr);
n->kind = a->kind;
n->name = a->name;
n->lexpr = a->lexpr;
n->rexpr = a->rexpr;
n->location = a->location;

return (Node *)transform_AEXPR_OP(cpstate, n);
}


static Node *transform_AEXPR_IN(cypher_parsestate *cpstate, A_Expr *a)
{
Oid func_in_oid;
Expand Down Expand Up @@ -535,6 +564,24 @@ static Node *transform_BoolExpr(cypher_parsestate *cpstate, BoolExpr *expr)
return (Node *)makeBoolExpr(expr->boolop, args, expr->location);
}

/*
* function for transforming cypher_comparison_boolexpr. Since this node is a
* wrapper to let us know when a comparison occurs in a chained comparison,
* we convert it to a PG BoolExpr and transform it.
*/
static Node *transform_cypher_comparison_boolexpr(cypher_parsestate *cpstate,
cypher_comparison_boolexpr *b)
{
BoolExpr *n = makeNode(BoolExpr);

n->boolop = b->boolop;
n->args = b->args;
n->location = b->location;

return transform_BoolExpr(cpstate, n);
}


static Node *transform_cypher_bool_const(cypher_parsestate *cpstate,
cypher_bool_const *bc)
{
Expand Down
Loading

0 comments on commit 689b456

Please sign in to comment.