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

Immutable casting of string > timestamp #60578

Closed
nandanrao opened this issue Feb 15, 2021 · 12 comments · Fixed by #60772
Closed

Immutable casting of string > timestamp #60578

nandanrao opened this issue Feb 15, 2021 · 12 comments · Fixed by #60772
Assignees
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) O-community Originated from the community X-blathers-triaged blathers was able to find an owner

Comments

@nandanrao
Copy link

I have two needs for my computed columns that worked in v20.1.4 but no longer work in v20.2.4 due to the better checking of function volatility in computed columns (#51886):

  1. timestamptz + interval
  2. string > timestamptz

Now, the first I can solve by changing timestamptz to timestamp (not an issue, all my clients run in UTC). Given that both timestamp and timestamptz are stored in UTC under the hood and only displayed to the client in local time, I'm not sure what context the operation is dependent on, but I can work around it.

The second I cannot work around because string > timestamp is also not Immutable:

{from: types.StringFamily, to: types.TimestampFamily, volatility: VolatilityStable},

I'm sure there are issues that I'm not aware of, but it seems to me that it aught to be possible to express a timestamp in a string format in a way that corresponds to a UTC time in a non-context-dependent way, after all, the whole purpose of ISO formats and the like is to encode timestamps into strings such that they do indeed represent a single point in time.

Also, thanks for all the hard work :)

@nandanrao nandanrao added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Feb 15, 2021
@blathers-crl
Copy link

blathers-crl bot commented Feb 15, 2021

Hello, I am Blathers. I am here to help you get the issue triaged.

It looks like you have not filled out the issue in the format of any of our templates. To best assist you, we advise you to use one of these templates.

I have CC'd a few people who may be able to assist you:

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Feb 15, 2021
@nandanrao nandanrao changed the title Non-volatile casting of string > timestamp Immutable casting of string > timestamp Feb 15, 2021
@RaduBerinde
Copy link
Member

CC @otan

Sorry for the apparent regression, unfortunately these changes were necessary to ensure correctness in all cases.

I am pretty sure we had an example for 1 where the result was different (it was surprising to me too). I can try to dig it out.

With 2 the problem is that unfortunately things like 'now', 'tomorrow' can be cast to timestamps.. When the string is a constant literal, we make a best effort to determine if we're in one of these cases. But we can't do anything for variable inputs. I see two options: stop supporting 'now' etc when they are not constants (though this is fragile, as what is constant sometimes depends on how smart the optimizer is; and might break other uses). Or, add a special function to convert strings to timestamp which only supports absolute values.

@RaduBerinde RaduBerinde self-assigned this Feb 15, 2021
@RaduBerinde
Copy link
Member

Also, I don't know if it's possible in your usecase, but consider using non-computed columns with default values instead.

@RaduBerinde
Copy link
Member

Here's an example for 1, with postgres:

 radu=> set time zone 'Europe/Berlin';
 SET
 radu=> select extract(epoch FROM TIMESTAMP WITH TIME ZONE '2010-11-06 23:59:00-05:00' + INTERVAL '1 day');
  date_part  
 ------------
  1289192340
 (1 row)
 
 radu=> set time zone 'America/Chicago';
 SET
 radu=> select extract(epoch FROM TIMESTAMP WITH TIME ZONE '2010-11-06 23:59:00-05:00' + INTERVAL '1 day');
  date_part  
 ------------
  1289195940
 (1 row)

@nandanrao
Copy link
Author

Woah!!!

postgres=# set time zone 'America/Chicago';
SET
postgres=# select '2010-11-06 12:59:00-05:00'::TIMESTAMPTZ + INTERVAL '24 hours';
        ?column?
------------------------
 2010-11-07 11:59:00-06
(1 row)

postgres=# select '2010-11-06 12:59:00-05:00'::TIMESTAMPTZ + INTERVAL '1 day';
        ?column?
------------------------
 2010-11-07 12:59:00-06
(1 row)

postgres=# set time zone 'Europe/Berlin';
SET
postgres=# select '2010-11-06 12:59:00-05:00'::TIMESTAMPTZ + INTERVAL '24 hours';
        ?column?
------------------------
 2010-11-07 18:59:00+01
(1 row)

postgres=# select '2010-11-06 12:59:00-05:00'::TIMESTAMPTZ + INTERVAL '1 day';
        ?column?
------------------------
 2010-11-07 18:59:00+01
(1 row)

I see now that, indeed, "a day" is a localized interval! In some moments in time, in some places, a day is less than 24 hours. Fair enough!

As for 2 -

Hrm, yes if "now" can be cast into a timestamp, then indeed, I see that string > timestamp is not viable as an immutable construct. And while this is a crazy design choice, in keeping with PSQL compatibility, I see that it's necessary.

A pure function that casts strings into timestamps sounds like a totally reasonable thing to exist, to be honest! It would logically be very simple and maintainable and would be a matter of just extracting the default clause (https://github.com/cockroachdb/cockroach/blob/master/pkg/util/timeutil/pgdate/field_extract.go#L164) of the casting logic and exposing that for use by a wrapper function, no?

@nandanrao
Copy link
Author

(Ah, I'm afraid non-computed columns don't work for my use-case, as it's pulling from a quite flexible JSON object, but to be honest, my case my not be too representative. I've been pushing a lot of logic onto Cockroach via computed columns, which simplifies my life a lot and has so far been a quite elegant and relatively scalable, just write times slow down but that's easy to deal with, but it's not exactly traditional by any means)

@RaduBerinde
Copy link
Member

Indeed. I can add that, unless you want to work on it - you seem to be familiar with the code :)
I know that we have a facility for parsing timestamps which returns a flag indicating if the context was used; we could simply error out in those cases.

@RaduBerinde
Copy link
Member

By the way, the next major release will likely add virtual computed columns (sounds like it might be useful to you).

@nandanrao
Copy link
Author

Ha, it would be fun for me to play around with the Cockroach code but I suspect you and your team will get to it before me if you think it's useful, I dunno when I would be able to prioritize this!

Virtual? As in, computed at query time rather than write time?

@RaduBerinde
Copy link
Member

Yeah, exactly.

RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Feb 19, 2021
Add a builtin that can be used to parse timestamp strings. This is
like a cast, but it does not accept relative timestamps so it can be
immutable.

Only immutable expressions are allowed in computed column expressions
or partial index predicates; unlike casts, the new function can be
used in such expressions.

Fixes cockroachdb#60578.

Release notes (sql change): A new parse_timestamp function can be used
to parse absolute timestamp strings in computed column expressions or
partial index predicates.
RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Feb 19, 2021
Add a builtin that can be used to parse timestamp strings. This is
like a cast, but it does not accept relative timestamps so it can be
immutable.

Only immutable expressions are allowed in computed column expressions
or partial index predicates; unlike casts, the new function can be
used in such expressions.

Fixes cockroachdb#60578.

Release notes (sql change): A new parse_timestamp function can be used
to parse absolute timestamp strings in computed column expressions or
partial index predicates.
RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Feb 19, 2021
Add a builtin that can be used to parse timestamp strings. This is
like a cast, but it does not accept relative timestamps so it can be
immutable.

Only immutable expressions are allowed in computed column expressions
or partial index predicates; unlike casts, the new function can be
used in such expressions.

Fixes cockroachdb#60578.

Release notes (sql change): A new parse_timestamp function can be used
to parse absolute timestamp strings in computed column expressions or
partial index predicates.
@RaduBerinde
Copy link
Member

@nandanrao I am adding a parse_timestamp builtin which parses an absolute timestamp string into a TIMESTAMP. It should be available in the next 20.2.x release.

craig bot pushed a commit that referenced this issue Feb 20, 2021
59566:  kvserver: introduce a Raft-based transport for closedts r=andreimatei a=andreimatei

This patch introduces a replacement for the existing closed timestamp
mechanism / transport. The new mechanism is gated by a cluster version.

Raft commands now carry increasing closed timestamps generated by the
propBuf by using the recent request Tracker for synchronizing with
in-flight requests (i.e. not closing timestamps below them).
Raft commands get a closed ts field, and the range state gets the field
as well.

The propBuf pays attention to the range's closed timestamp policy for
deciding whether to close lagging or leading timestamps.

Release note: None

60753: kv: add TestStoreRangeSplitAndMergeWithGlobalReads r=nvanbenschoten a=nvanbenschoten

Made possible by #60567.

This commit adds a new test called TestStoreRangeSplitAndMergeWithGlobalReads
that tests that a range configured to serve global reads can be split and
merged. In essence, this tests whether the split and merge transactions can
handle having their timestamp bumped by the closed timestamp on the ranges
they're operating on.

The test revealed that range merges did have issues in these cases, because
SubsumeRequests assumed that the intent on the RHS's local descriptor was below
the node's HLC clock. This is no longer always true, so we now perform the
inconsistent scan at hlc.MaxTimestamp, just like QueryIntent requests do.

60772: sql: add parse_timestamp builtin r=RaduBerinde a=RaduBerinde

Add a builtin that can be used to parse timestamp strings. This is
like a cast, but it does not accept relative timestamps so it can be
immutable.

Only immutable expressions are allowed in computed column expressions
or partial index predicates; unlike casts, the new function can be
used in such expressions.

Fixes #60578.

Release notes (sql change): A new parse_timestamp function can be used
to parse absolute timestamp strings in computed column expressions or
partial index predicates.

60796: geo: replace GEOS with new WKT parser for EWKT parsing r=otan a=andyyang890

Previously, the `parseEWKT` function used GEOS to parse WKT strings.
This was inadequate because GEOS has issues with parsing Z and M
dimensions. To address this, a new WKT parser was implemented with
goyacc and this patch integrates it into CockroachDB.

Refs: #53091

Release note: None

60818: opt: add partition info to the opt catalog r=rytaft a=rytaft

Prior to this commit, the opt catalog did not include zone information
or prefixes specific to each partition of an index. This commit adds this
information since it will be necessary to support locality optimized
search in a future commit.

Informs #55185

Release note: None

Co-authored-by: Andrei Matei <andrei@cockroachlabs.com>
Co-authored-by: Nathan VanBenschoten <nvanbenschoten@gmail.com>
Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
Co-authored-by: Andy Yang <ayang@cockroachlabs.com>
Co-authored-by: Rebecca Taft <becca@cockroachlabs.com>
@craig craig bot closed this as completed in e711b72 Feb 20, 2021
@nandanrao
Copy link
Author

Ha, wow, amazing!!!

Thanks a lot, @RaduBerinde, that's a big headache removed for me, personally. I really appreciate that.

And it's a super clean implementation for something that seems useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) 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.

2 participants