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: remove nanoseconds #8864

Merged
merged 1 commit into from Aug 29, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions sql/lease.go
Expand Up @@ -135,6 +135,7 @@ func (s LeaseStore) Acquire(
) (*LeaseState, error) {
lease := &LeaseState{}
expiration := time.Unix(0, s.clock.Now().WallTime).Add(jitteredLeaseDuration())
expiration = expiration.Round(time.Microsecond)
if !minExpirationTime.IsZero() && expiration.Before(minExpirationTime.Time) {
expiration = minExpirationTime.Time
}
Expand Down
46 changes: 0 additions & 46 deletions sql/parser/builtins.go
Expand Up @@ -657,14 +657,6 @@ var Builtins = map[string][]Builtin{
case "microsecond", "microseconds":
return NewDInt(DInt(fromTime.Nanosecond() / int(time.Microsecond))), nil

case "nanosecond", "nanoseconds":
// This is a CockroachDB extension.
return NewDInt(DInt(fromTime.Nanosecond())), nil

case "epoch_nanosecond", "epoch_nanoseconds":
// This is a CockroachDB extension.
return NewDInt(DInt(fromTime.UnixNano())), nil

case "epoch":
return NewDInt(DInt(fromTime.Unix())), nil

Expand All @@ -675,44 +667,6 @@ var Builtins = map[string][]Builtin{
},
},

// Nanosecond functions.
// These functions are the only ways to create and read nanoseconds in
// timestamps. All other functions round to microseconds.

"parse_timestamp_ns": {
Builtin{
Types: ArgTypes{TypeString},
ReturnType: TypeTimestamp,
category: categoryDateAndTime,
fn: func(ctx *EvalContext, args DTuple) (Datum, error) {
s := string(*args[0].(*DString))
return ParseDTimestamp(s, ctx.GetLocation(), time.Nanosecond)
},
},
},

"format_timestamp_ns": {
Builtin{
Types: ArgTypes{TypeTimestamp},
ReturnType: TypeString,
fn: func(ctx *EvalContext, args DTuple) (Datum, error) {
t := args[0].(*DTimestamp)
return NewDString(t.Time.UTC().Format(timestampFormatNS)), nil
},
},
},

"current_timestamp_ns": {
Builtin{
Types: ArgTypes{},
ReturnType: TypeTimestamp,
impure: true,
fn: func(ctx *EvalContext, args DTuple) (Datum, error) {
return ctx.GetTxnTimestamp(time.Nanosecond), nil
},
},
},

// Math functions

"abs": {
Expand Down
1 change: 0 additions & 1 deletion sql/parser/datum.go
Expand Up @@ -775,7 +775,6 @@ const (
timestampRFC3339NanoWithoutZoneFormat = "2006-01-02T15:04:05"

timestampNodeFormat = timestampFormat + ".999999-07:00"
timestampFormatNS = timestampFormat + ".999999999"
)

var timeFormats = []string{
Expand Down
20 changes: 0 additions & 20 deletions sql/testdata/builtin_function
Expand Up @@ -816,16 +816,6 @@ SELECT extract(microsecond FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306158

query I
SELECT extract(nanoseconds FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306158000

query I
SELECT extract(nanosecond FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306158000

query I
SELECT extract(second FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
Expand Down Expand Up @@ -891,16 +881,6 @@ SELECT extract(epoch FROM '1970-01-02 00:00:01.000001'::timestamp)
----
86401

query I
SELECT extract(epoch_nanosecond FROM '1970-01-02 00:00:01.000001'::timestamp)
----
86401000001000

query I
SELECT extract(epoch_nanoseconds FROM '1970-01-02 00:00:01.000001'::timestamp)
----
86401000001000

query BI
SELECT experimental_unique_bytes() < experimental_unique_bytes(), length(experimental_unique_bytes())
----
Expand Down
82 changes: 1 addition & 81 deletions sql/testdata/datetime
Expand Up @@ -164,7 +164,7 @@ COMMIT TRANSACTION
# Check that the current_timestamp, now and transaction_timestamp are the same.
# Test that the transaction_timestamp can differ from the statement_timestamp.
# Check that the transaction_timestamp changes with each transaction.
# We use, SELECT * FROM kv, to insert delays of more than a nanosecond.
# We use, SELECT * FROM kv, to insert delays of more than a microsecond.
statement ok
BEGIN;
INSERT INTO kv (k,v) VALUES ('b', transaction_timestamp());
Expand Down Expand Up @@ -371,86 +371,6 @@ SELECT extract(microsecond from '2001-04-10 12:04:59.34565423')
----
345654

# nanoseconds are truncated to microseconds
query I
SELECT extract(nanosecond from '2001-04-10 12:04:59.34565423')
----
345654000

# verify that nanosecond uniqueness is not preserved
statement error pq: duplicate key value
INSERT INTO t (a) VALUES ('2001-04-10 12:04:59.000000001'), ('2001-04-10 12:04:59.000000002')

# nanosecond uniqueness is preserved with parse_timestamp_ns
statement ok
INSERT INTO t (a) VALUES (parse_timestamp_ns('2001-04-10 12:04:59.000000001')), (parse_timestamp_ns('2001-04-10 12:04:59.000000002'))

# parse_timestamp_ns retains nanoseconds
query I
SELECT extract(nanosecond from parse_timestamp_ns('2001-04-10 12:04:59.345654423'))
----
345654423

# current_timestamp_ns has nanoseconds
# Since it will produce 0 ns 1 out of 1000 executions,
# INSERT many and verify that at least one is non-zero.

statement ok
INSERT INTO t (a) VALUES (current_timestamp_ns())

statement ok
INSERT INTO t (a) VALUES (current_timestamp_ns())

statement ok
INSERT INTO t (a) VALUES (current_timestamp_ns())

statement ok
INSERT INTO t (a) VALUES (current_timestamp_ns())

statement ok
INSERT INTO t (a) VALUES (current_timestamp_ns())

query B
SELECT max(extract(nanosecond from a) % 1000) != 0 FROM t
----
true

query TT
SELECT format_timestamp_ns('2001-04-10 12:04:59.345654423'), format_timestamp_ns(parse_timestamp_ns('2001-04-10 12:04:59.345654423'))
----
2001-04-10 12:04:59.345654 2001-04-10 12:04:59.345654423

# now() should not return any nanoseconds
query I
SELECT extract(nanosecond from now()) % 1000
----
0

# statement_timestamp() should not return any nanoseconds
query I
SELECT extract(nanosecond from statement_timestamp()) % 1000
----
0

# clock_timestamp() should not return any nanoseconds
query I
SELECT extract(nanosecond from clock_timestamp()) % 1000
----
0

# statement_timestamp() should not return any nanoseconds
query I
SELECT extract(nanosecond from statement_timestamp()) % 1000
----
0

# system.lease should still have nanoseconds
# Similar to current_timestamp_ns, make sure at least 1 is non-zero.
query B
SELECT max(extract(nanosecond from expiration) % 1000) != 0 from system.lease
----
true

query error extract: unsupported timespan: nansecond
SELECT extract(nansecond from '2001-04-10 12:04:59.34565423')

Expand Down