Skip to content

Support INTERVAL type #213

@apstndb

Description

@apstndb

INTERVAL type is officially released.

https://cloud.google.com/spanner/docs/reference/standard-sql/lexical#interval_literals
https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#interval_type

Current Behavior

spanner> SELECT INTERVAL 1 DAY AS `interval`;
+--------------------+
| interval           |
| UNKNOWN            |
+--------------------+
| string_value:"P1D" |
+--------------------+
1 rows in set (3.28 msecs)

Format

I recommend to use ISO 8601 duration format because:
It is used as internal representation of INTERVAL.

https://github.com/googleapis/googleapis/blob/1270a1ba7ae32ce486b28d00ced9575c29d9ff75/google/spanner/v1/type.proto#L172-L177

// Encoded as `string`, in `ISO8601` duration format -
// `P[n]Y[n]M[n]DT[n]H[n]M[n[.fraction]]S`
// where `n` is an integer.
// For example, `P1Y2M3DT4H5M6.5S` represents time duration of 1 year, 2
// months, 3 days, 4 hours, 5 minutes, and 6.5 seconds.
INTERVAL = 16;

Spanner Studio and gcloud spanner databases execute-sql uses the same format.

Image

Implementation

$ gcloud spanner databases execute-sql ${SPANNER_DATABASE_ID} --sql 'SELECT INTERVAL 1 DAY AS `interval`'
interval
P1D

I think it is good enough to use Interval.String() and NullInterval.String() because it format value as ISO 8601 duration.
https://pkg.go.dev/cloud.google.com/go/spanner#Interval.String

String returns the ISO8601 duration format string representation of the interval.

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