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: allow relative AS OF queries with a negative interval #24768

Merged
merged 1 commit into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/sql/as_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestAsOfTime(t *testing.T) {
}

// String values that are neither timestamps nor decimals are an error.
if _, err := db.Query("SELECT a FROM d.t AS OF SYSTEM TIME 'xxx'"); !testutils.IsError(err, "value is neither timestamp nor decimal") {
if _, err := db.Query("SELECT a FROM d.t AS OF SYSTEM TIME 'xxx'"); !testutils.IsError(err, "value is neither timestamp, decimal, nor interval") {
t.Fatal(err)
}

Expand Down
18 changes: 11 additions & 7 deletions pkg/sql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2480,20 +2480,24 @@ func EvalAsOfTimestamp(
break
}
// Attempt to parse as a decimal.
if dec, _, err := apd.NewFromString(s); err != nil {
// Override the error. It would be misleading to fail with a
// DECIMAL conversion error if a user was attempting to use a
// timestamp string and the conversion above failed.
convErr = errors.Errorf("AS OF SYSTEM TIME: value is neither timestamp nor decimal")
} else {
if dec, _, err := apd.NewFromString(s); err == nil {
ts, convErr = decimalToHLC(dec)
break
}
// Attempt to parse as an interval.
if iv, err := tree.ParseDInterval(s); err == nil {
ts.WallTime = duration.Add(evalCtx.GetStmtTimestamp(), iv.Duration).UnixNano()
break
}
convErr = errors.Errorf("AS OF SYSTEM TIME: value is neither timestamp, decimal, nor interval")
case *tree.DInt:
ts.WallTime = int64(*d)
case *tree.DDecimal:
ts, convErr = decimalToHLC(&d.Decimal)
case *tree.DInterval:
ts.WallTime = duration.Add(evalCtx.GetStmtTimestamp(), d.Duration).UnixNano()
default:
convErr = errors.Errorf("AS OF SYSTEM TIME: expected timestamp, got %s (%T)", d.ResolvedType(), d)
convErr = errors.Errorf("AS OF SYSTEM TIME: expected timestamp, decimal, or interval, got %s (%T)", d.ResolvedType(), d)
}
if convErr != nil {
return ts, convErr
Expand Down
30 changes: 30 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/as_of
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# LogicTest: default

statement ok
CREATE TABLE t (i INT)

statement ok
INSERT INTO t VALUES (2)

# Verify strings can be parsed as intervals.
query I
SELECT * FROM t AS OF SYSTEM TIME '-0ns'
----
2

query I
SELECT * FROM t AS OF SYSTEM TIME '-1ns'
----
2

# Verify a forced interval type works.
query I
SELECT * FROM t AS OF SYSTEM TIME INTERVAL '-1ns'
----
2

statement error pq: relation "t" does not exist
SELECT * FROM t AS OF SYSTEM TIME '-1h'

statement error cannot specify timestamp in the future
SELECT * FROM t AS OF SYSTEM TIME '10s'