Skip to content

Implement generalized subscript operators #192

@apstndb

Description

@apstndb

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.

BigQuery reference

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions