cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
29.93k stars 3.78k forks source link

sql: support more values for `DateStyle` session parameter #41773

Open rafiss opened 4 years ago

rafiss commented 4 years ago

full docs: https://www.postgresql.org/docs/9.5/runtime-config-client.html#GUC-DATESTYLE

Sets the display format for date and time values, as well as the rules for interpreting ambiguous date input values. For historical reasons, this variable contains two independent components: the output format specification (ISO, Postgres, SQL, or German) and the input/output specification for year/month/day ordering (DMY, MDY, or YMD). These can be set separately or together. The keywords Euro and European are synonyms for DMY; the keywords US, NonEuro, and NonEuropean are synonyms for MDY. See Section 8.5 for more information. The built-in default is ISO, MDY, but initdb will initialize the configuration file with a setting that corresponds to the behavior of the chosen lc_time locale.

Epic CRDB-2508

Jira issue: CRDB-6339

jjathman commented 3 years ago

As another data point for this issue, we are attempting to use Oracle Golden Gate replication to move data from an existing Oracle DB to CockroachDB. OGG uses the Progress ODBC driver which appears to issue a SET DateStyle='iso, ymd' command when initiating a connection and isn't possible to disable. This appears to be a blocker for us that we can't work around.

dbist commented 3 years ago

we can't even pass datestyle as connection variable.

sh-4.4# cockroach sql --url 'postgresql://root@lb:26257?sslcert=%2Fcerts%2Fclient.root.crt&sslkey=%2Fcerts%2Fclient.root.key&sslmode=verify-full&sslrootcert=%2Fcerts%2Fca.crt&application_name=test&database=bank&datestyle=ISO, MDY'
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v20.2.5 (x86_64-unknown-linux-gnu, built 2021/02/16 12:52:58, go1.13.14) (same version as client)
# Cluster ID: 22c50ba7-1579-4b8b-8e8e-641a98b25f6e
#
# Enter \? for a brief introduction.
#
root@lb:26257/bank> \q
sh-4.4# cockroach sql --url 'postgresql://root@lb:26257?sslcert=%2Fcerts%2Fclient.root.crt&sslkey=%2Fcerts%2Fclient.root.key&sslmode=verify-full&sslrootcert=%2Fcerts%2Fca.crt&application_name=test&database=bank&datestyle=MDY, ISO'
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
ERROR: setting datestyle must be absent or ISO, MDY; got MDY, ISO
Failed running "sql"
vy-ton commented 3 years ago

@rafiss Can we revisit this for the May milestone?

rafiss commented 3 years ago

I hope that we can get started on it, but this will have the same complexities as described in https://github.com/cockroachdb/cockroach/pull/62313#pullrequestreview-618881295 -- will need a potentially big refactor/new interfaces.

otan commented 3 years ago

I started working on a library to work across all styles (ISO,Postgres,SQL,German), but it turns out we only need the order components {MDY,YMD,DMY} to work maintaining the ISO style. That's a lot simpler, I'll be doing that instead.

If anyone wants to embark on this for future work, I've started that work here https://github.com/cockroachdb/pgdatetime. I have basically attempted to copy Postgres's parse logic, I have the tokens, formatting and some parsing logic working. But there are many road bumps to go:

otan commented 3 years ago

Support for ISO / MDY,DMY,YMD is complete. Other formats are not yet supported as they cannot be parsed, see previous comment.

otan commented 3 years ago

heads up stuff like https://github.com/cockroachdb/cockroach/blob/d6b2ecb2d87bc1dd27a1c0915fff2859f6e71ec7/pkg/sql/roleoption/role_option.go#L68 needs to always needs to convert as ISO if we decide to support a non-ISO DateStyle.