-
Notifications
You must be signed in to change notification settings - Fork 26
Implement generalized subscript operators #192
Copy link
Copy link
Closed
Description
Currrently, ast.IndexExpr only supports [OFFSET(n)] or [ORDINAL(n)], but it is enhanced by JSON subscript operators.
Syntax
https://cloud.google.com/spanner/docs/reference/standard-sql/operators#json_subscript_operator
json_expression "[" array_element_id "]"
json_expression "[" field_name "]"
Examples:
SELECT json_value.class.students[0]['name'] AS first_student
FROM
UNNEST(
[
JSON '{"class" : {"students" : [{"name" : "Jane"}]}}',
JSON '{"class" : {"students" : []}}',
JSON '{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'])
AS json_value;
Actually, OFFSET() is already optional in the array subscript operator.
> SELECT [1, 2, 3][0];
+---+
| |
+---+
| 1 |
+---+
1 rows in set (0.89 msecs)
So, it is the current spec of array subscript operator.
array_expression "[" array_subscript_specifier "]"
array_subscript_specifier:
{ index | position_keyword(index) }
position_keyword:
{ OFFSET | SAFE_OFFSET | ORDINAL | SAFE_ORDINAL }
Note: Struct subscript operator is also supported.
spanner> select
-> STRUCT(1, 2, 3)[offset(1)],
-> STRUCT(1, 2, 3)[ordinal(1)],
-> STRUCT(1, 2, 3)[safe_offset(1)],
-> STRUCT(1, 2, 3)[ordinal(1)],
-> STRUCT(1, 2, 3)[1],
-> ;
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| 2 | 1 | 2 | 1 | 2 |
+---+---+---+---+---+
1 rows in set (1.97 msecs)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels