Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/sem/tree: parse ISO8601 format with decimal places for interval types #59720

Closed
keithdoggett opened this issue Feb 2, 2021 · 5 comments · Fixed by #60570
Closed

sql/sem/tree: parse ISO8601 format with decimal places for interval types #59720

keithdoggett opened this issue Feb 2, 2021 · 5 comments · Fixed by #60570
Labels
A-sql-datatypes SQL column types usable in table descriptors. A-tools-activerecord C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-easy Easy issue to tackle, requires little or no CockroachDB experience good first issue O-community Originated from the community X-blathers-triaged blathers was able to find an owner

Comments

@keithdoggett
Copy link

keithdoggett commented Feb 2, 2021

We do not parse SELECT INTERVAL 'P1Y2M3DT4H5M6.235S'; correctly on CockroachDB, but this successfully parses in Postgres:

psql (13.1)
Type "help" for help.

otan=# SELECT INTERVAL 'P1Y2M3DT4H5M6.235S';
             interval
-----------------------------------
 1 year 2 mons 3 days 04:05:06.235
(1 row)

Changes should be made in

func sqlStdToDuration(s string, itm types.IntervalTypeMetadata) (duration.Duration, error) {

With unit tests added here:

{`1`, types.IntervalTypeMetadata{}, `00:00:01`},


Original:

There are two errors coming from the PostgresqlIntervalTest (test/cases/adapters/postgesql/interval_test.rb) in ActiveRecord.

Error:
PostgresqlIntervalTest#test_interval_type:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  error in argument for $2: could not parse string "P1Y2M3DT4H5M6.235S" as interval
Error:
PostgresqlIntervalTest#test_interval_type_cast_from_numeric:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  error in argument for $1: could not parse string "PT36000.000S" as interval

Running SELECT INTERVAL 'P1Y2M3DT4H5M6.235S'; in a SQL shell gives the following response:

ERROR: could not parse "P1Y2M3DT4H5M6.235S" as type interval: interval: unknown unit . in ISO-8601 duration P1Y2M3DT4H5M6.235S

This indicates that using a decimal for seconds is causing the error.

@otan otan transferred this issue from cockroachdb/activerecord-cockroachdb-adapter Feb 2, 2021
@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Feb 2, 2021
@otan otan changed the title [ActiveRecord 6.1] Iso8601 parse error on Interval columns sql/sem/tree: parse ISO8601 format with decimal places for interval types Feb 2, 2021
@otan otan added A-sql-datatypes SQL column types usable in table descriptors. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. good first issue labels Feb 2, 2021
@cockroachdb cockroachdb deleted a comment from blathers-crl bot Feb 2, 2021
@rafiss rafiss added the E-easy Easy issue to tackle, requires little or no CockroachDB experience label Feb 5, 2021
@rafiss
Copy link
Collaborator

rafiss commented Feb 5, 2021

Looks like https://github.com/senseyeio/duration/blob/7c2a214ada46/duration.go#L31 is a reasonable (and MIT license) reference we could use. just a simple regex. (we should reimplement ourselves since there's no need to add a dependency for something so small)

@shikamaru-cc
Copy link
Contributor

hi @keithdoggett @rafiss
im new to the community
this seems so great to be my first issue!
If nobody is working on it, I would like to try

@rafiss
Copy link
Collaborator

rafiss commented Feb 6, 2021

@shikamaru404 welcome! thank you for your interest! please go ahead and try it out. when you submit a PR, please add @otan and me as reviewers.

@shikamaru-cc
Copy link
Contributor

hi @rafiss
Should the changes be in function sqlStdToDuration (cockroach/pkg/sql/sem/tree/interval.go)?
Seems that function iso8601ToDuration (cockroach/pkg/sql/sem/tree/interval.go) is responsible for parsing iso8601.

@otan
Copy link
Contributor

otan commented Feb 7, 2021

@shikamaru404 seems like you're right :)

craig bot pushed a commit that referenced this issue Feb 15, 2021
60570: sql/sem/tree: parse ISO8601 format with decimal places for interval types r=otan a=shikamaru404

Previously, CockroachDB can not parse ISO8601 format interval type with
decimals, but Postgres can parse successfully. For example, running
"SELECT INTERVAL 'P1Y2M3DT4H5M6.235S'" in CockroachDB will cause an error
like:
ERROR: could not parse "P1Y2M3DT4H5M6.235S" as type interval: interval:
unknown unit . in ISO-8601 duration P1Y2M3DT4H5M6.235S

This patch fixes this issue. Now cockroachDB can parse ISO8601 format
using decimals correctly and return the same result as Postgres.

Fixes: #59720

Release note: none

Co-authored-by: shikamaru <jpchen78@foxmail.com>
@craig craig bot closed this as completed in ec68040 Feb 15, 2021
HonoreDB pushed a commit to HonoreDB/cockroach that referenced this issue Jul 9, 2021
…ypes

Previously, CockroachDB can not parse ISO8601 format interval type with
decimals, but Postgres can parse successfully. For example, running
"SELECT INTERVAL 'P1Y2M3DT4H5M6.235S'" in CockroachDB will cause an error
like:
ERROR: could not parse "P1Y2M3DT4H5M6.235S" as type interval: interval:
unknown unit . in ISO-8601 duration P1Y2M3DT4H5M6.235S

This patch fixes this issue. Now cockroachDB can parse ISO8601 format
using decimals correctly and return the same result as Postgres.

Fixes: cockroachdb#59720

Release note: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-datatypes SQL column types usable in table descriptors. A-tools-activerecord C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-easy Easy issue to tackle, requires little or no CockroachDB experience good first issue O-community Originated from the community X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants