diff --git a/regress/expected/expr.out b/regress/expected/expr.out index 82dd22724..d50862a6e 100644 --- a/regress/expected/expr.out +++ b/regress/expected/expr.out @@ -132,7 +132,7 @@ SELECT * FROM cypher('expr', $$ RETURN $var $$, $1) AS t(i agtype); EXECUTE cypher_parameter_invalid_argument('[1]'); -ERROR: array index must resolve to an integer value +ERROR: cannot access property "var" on a list value -- missing parameters argument PREPARE cypher_missing_params_argument(int) AS SELECT $1, * FROM cypher('expr', $$ @@ -10786,6 +10786,102 @@ NOTICE: graph "issue_2391" has been dropped (1 row) +-- +-- Issue 2623 - property access on a list value +-- +SELECT create_graph('issue_2623'); +NOTICE: graph "issue_2623" has been created + create_graph +-------------- + +(1 row) + +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + RETURN [{a: 1}].a + $cypher$) AS (dot_list agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; +NOTICE: 42804: cannot access property "a" on a list value +SELECT * FROM cypher('issue_2623', $$ + CREATE (:A {x: 1})-[:E {id: 1}]->()-[:F {id: 1}]->(:C) +$$) AS (a agtype); + a +--- +(0 rows) + +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + MATCH (:A {x: 1})-[e:E*0..1]->()-[f:F]->(:C) + WHERE e.id + f.id > 0 + RETURN 1 + $cypher$) AS (d1 agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; +NOTICE: 42804: cannot access property "id" on a list value +-- control: non-integer subscripts are invalid parameter values +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + RETURN [1, 2][true] + $cypher$) AS (bad_index agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; +NOTICE: 22023: array index must resolve to an integer value +-- controls: integer subscripts and property access on a list element +SELECT * FROM cypher('issue_2623', $$ + RETURN [{a: 1}][0].a +$$) AS (dot_list_element agtype); + dot_list_element +------------------ + 1 +(1 row) + +SELECT * FROM cypher('issue_2623', $$ + RETURN [1, 2][0] +$$) AS (list_element agtype); + list_element +-------------- + 1 +(1 row) + +SELECT * FROM drop_graph('issue_2623', true); +NOTICE: drop cascades to 6 other objects +DETAIL: drop cascades to table issue_2623._ag_label_vertex +drop cascades to table issue_2623._ag_label_edge +drop cascades to table issue_2623."A" +drop cascades to table issue_2623."E" +drop cascades to table issue_2623."F" +drop cascades to table issue_2623."C" +NOTICE: graph "issue_2623" has been dropped + drop_graph +------------ + +(1 row) + -- -- End of tests -- diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql index 7d9a13c43..de5ccbba9 100644 --- a/regress/sql/expr.sql +++ b/regress/sql/expr.sql @@ -4232,6 +4232,73 @@ SELECT * FROM cypher('issue_2391', $$ $$) AS (n agtype); SELECT * FROM drop_graph('issue_2391', true); +-- +-- Issue 2623 - property access on a list value +-- +SELECT create_graph('issue_2623'); + +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + RETURN [{a: 1}].a + $cypher$) AS (dot_list agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; + +SELECT * FROM cypher('issue_2623', $$ + CREATE (:A {x: 1})-[:E {id: 1}]->()-[:F {id: 1}]->(:C) +$$) AS (a agtype); + +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + MATCH (:A {x: 1})-[e:E*0..1]->()-[f:F]->(:C) + WHERE e.id + f.id > 0 + RETURN 1 + $cypher$) AS (d1 agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; + +-- control: non-integer subscripts are invalid parameter values +DO $do$ +DECLARE + err_sqlstate text; + err_message text; +BEGIN + PERFORM * FROM cypher('issue_2623', $cypher$ + RETURN [1, 2][true] + $cypher$) AS (bad_index agtype); +EXCEPTION WHEN others THEN + GET STACKED DIAGNOSTICS err_sqlstate = RETURNED_SQLSTATE, + err_message = MESSAGE_TEXT; + RAISE NOTICE '%: %', err_sqlstate, err_message; +END +$do$; + +-- controls: integer subscripts and property access on a list element +SELECT * FROM cypher('issue_2623', $$ + RETURN [{a: 1}][0].a +$$) AS (dot_list_element agtype); +SELECT * FROM cypher('issue_2623', $$ + RETURN [1, 2][0] +$$) AS (list_element agtype); + +SELECT * FROM drop_graph('issue_2623', true); + -- -- End of tests -- diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c index 36ca8b36d..09c5e3da1 100644 --- a/src/backend/utils/adt/agtype.c +++ b/src/backend/utils/adt/agtype.c @@ -3941,15 +3941,26 @@ static agtype_value *execute_array_access_operator(agtype *array, return NULL; } + /* a string index is a property access, which is invalid for a list */ + if (array_index_value->type == AGTV_STRING) + { + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("cannot access property \"%.*s\" on a list value", + array_index_value->val.string.len, + array_index_value->val.string.val))); + } + /* index must be an integer */ if (array_index_value->type != AGTV_INTEGER) { ereport(ERROR, - (errmsg("array index must resolve to an integer value"))); + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("array index must resolve to an integer value"))); } - result = execute_array_access_operator_internal(array, array_value, - array_index_value->val.int_value); + result = execute_array_access_operator_internal(array, array_value, + array_index_value->val.int_value); pfree_agtype_value(array_index_value); return result; @@ -4012,7 +4023,9 @@ static agtype_value *execute_array_access_operator_internal(agtype *array, /* otherwise, we don't know how to process it */ else { - ereport(ERROR, (errmsg("unknown array_value type"))); + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("unknown array_value type"))); } return array_element_value;