Skip to content

Commit

Permalink
exec,coldata: support timestamp type
Browse files Browse the repository at this point in the history
This commit adds support for the TIMESTAMP type to the coldata and exec
packages.

Release note: None
  • Loading branch information
jordanlewis committed Aug 30, 2019
1 parent 9baf0f1 commit 55b1f97
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/col/coldata/vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package coldata

import (
"fmt"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
Expand Down Expand Up @@ -93,6 +94,8 @@ type Vec interface {
// TODO(jordan): should this be [][]byte?
// Decimal returns an apd.Decimal slice.
Decimal() []apd.Decimal
// Timestamp returns a time.Time slice.
Timestamp() []time.Time

// Col returns the raw, typeless backing storage for this Vec.
Col() interface{}
Expand Down Expand Up @@ -172,6 +175,8 @@ func NewMemColumn(t coltypes.T, n int) Vec {
return &memColumn{t: t, col: make([]float64, n), nulls: nulls}
case coltypes.Decimal:
return &memColumn{t: t, col: make([]apd.Decimal, n), nulls: nulls}
case coltypes.Timestamp:
return &memColumn{t: t, col: make([]time.Time, n), nulls: nulls}
default:
panic(fmt.Sprintf("unhandled type %s", t))
}
Expand Down Expand Up @@ -221,6 +226,10 @@ func (m *memColumn) Decimal() []apd.Decimal {
return m.col.([]apd.Decimal)
}

func (m *memColumn) Timestamp() []time.Time {
return m.col.([]time.Time)
}

func (m *memColumn) Col() interface{} {
return m.col
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/col/coldata/vec_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package coldata

import (
"fmt"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
Expand All @@ -45,6 +46,9 @@ type _GOTYPESLICE interface{}
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// */}}

func (m *memColumn) Append(args AppendArgs) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/col/colserde/arrowbatchconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func randomBatch() ([]coltypes.T, coldata.Batch) {
availableTyps := make([]coltypes.T, 0, len(coltypes.AllTypes))
for _, typ := range coltypes.AllTypes {
// TODO(asubiotto): We do not support decimal conversion yet.
if typ == coltypes.Decimal {
if typ == coltypes.Decimal || typ == coltypes.Timestamp {
continue
}
availableTyps = append(availableTyps, typ)
Expand Down Expand Up @@ -117,7 +117,7 @@ func assertEqualBatches(t *testing.T, expected, actual coldata.Batch) {
func TestArrowBatchConverterRejectsUnsupportedTypes(t *testing.T) {
defer leaktest.AfterTest(t)()

typs := []coltypes.T{coltypes.Decimal}
typs := []coltypes.T{coltypes.Decimal, coltypes.Timestamp}
_, err := NewArrowBatchConverter(typs)
require.Error(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/col/colserde/record_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestRecordBatchSerializerSerializeDeserializeRandom(t *testing.T) {

// We do not support decimals yet.
for _, t := range coltypes.AllTypes {
if t == coltypes.Decimal {
if t == coltypes.Decimal || t == coltypes.Timestamp {
continue
}
supportedTypes = append(supportedTypes, t)
Expand Down
7 changes: 4 additions & 3 deletions pkg/col/coltypes/t_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/col/coltypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package coltypes

import (
"fmt"
"time"

"github.com/cockroachdb/apd"
)
Expand Down Expand Up @@ -40,6 +41,8 @@ const (
Float32
// Float64 is a column of type float64
Float64
// Timestamp is a column of type time.Time
Timestamp

// Unhandled is a temporary value that represents an unhandled type.
// TODO(jordan): this should be replaced by a panic once all types are
Expand Down Expand Up @@ -78,6 +81,7 @@ func init() {
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], NumberTypes...)
CompatibleTypes[Float32] = append(CompatibleTypes[Float32], NumberTypes...)
CompatibleTypes[Float64] = append(CompatibleTypes[Float64], NumberTypes...)
CompatibleTypes[Timestamp] = append(CompatibleTypes[Timestamp], Timestamp)
}

// FromGoType returns the type for a Go value, if applicable. Shouldn't be used at
Expand All @@ -104,6 +108,8 @@ func FromGoType(v interface{}) T {
return Bytes
case apd.Decimal:
return Decimal
case time.Time:
return Timestamp
default:
panic(fmt.Sprintf("type %T not supported yet", t))
}
Expand All @@ -130,6 +136,8 @@ func (t T) GoTypeName() string {
return "float32"
case Float64:
return "float64"
case Timestamp:
return "time.Time"
default:
panic(fmt.Sprintf("unhandled type %d", t))
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/colencoding/value_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package colencoding

import (
"time"

"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
Expand Down Expand Up @@ -84,6 +86,10 @@ func decodeUntaggedDatumToCol(vec coldata.Vec, idx uint16, t *types.T, buf []byt
// We map these to 64-bit INT now. See #34161.
vec.Int64()[idx] = i
}
case types.TimestampFamily:
var t time.Time
buf, t, err = encoding.DecodeUntaggedTimeValue(buf)
vec.Timestamp()[idx] = t
default:
return buf, errors.AssertionFailedf(
"couldn't decode type: %s", log.Safe(t))
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/exec/any_not_null_agg_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package exec

import (
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
Expand All @@ -45,6 +47,9 @@ func newAnyNotNullAgg(t coltypes.T) (aggregateFunc, error) {
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// _GOTYPESLICE is the template Go type slice variable for this operator. It
// will be replaced by the Go slice representation for each type in coltypes.T, for
// example []int64 for coltypes.Int64.
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec/const_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package exec

import (
"context"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand All @@ -36,6 +37,9 @@ import (
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// _TYPES_T is the template type variable for coltypes.T. It will be replaced by
// coltypes.Foo for each type Foo in the coltypes.T type.
const _TYPES_T = coltypes.Unhandled
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec/distinct_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"context"
"math"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand Down Expand Up @@ -104,6 +105,9 @@ var _ bytes.Buffer
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "tree" package.
var _ tree.Datum

Expand Down
35 changes: 35 additions & 0 deletions pkg/sql/exec/execgen/cmd/execgen/overloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ type floatIntCustomizer struct{}
// side and a float right-hand side.
type intFloatCustomizer struct{}

// timestampCustomizer is necessary since time.Time doesn't have infix operators.
type timestampCustomizer struct{}

func (boolCustomizer) getCmpOpCompareFunc() compareFunc {
return func(target, l, r string) string {
args := map[string]string{"Target": target, "Left": l, "Right": r}
Expand Down Expand Up @@ -807,11 +810,43 @@ func (c intFloatCustomizer) getCmpOpCompareFunc() compareFunc {
return getFloatCmpOpCompareFunc(false /* checkLeftNan */, true /* checkRightNan */)
}

func (c timestampCustomizer) getCmpOpCompareFunc() compareFunc {
return func(target, l, r string) string {
args := map[string]string{"Target": target, "Left": l, "Right": r}
buf := strings.Builder{}
// Inline the code from tree.compareTimestamps.
t := template.Must(template.New("").Parse(`
if {{.Left}}.Before({{.Right}}) {
{{.Target}} = -1
} else if {{.Right}}.Before({{.Left}}) {
{{.Target}} = 1
} else {
{{.Target}} = 0
}`))

if err := t.Execute(&buf, args); err != nil {
execerror.VectorizedInternalPanic(err)
}
return buf.String()
}
}

func (timestampCustomizer) getHashAssignFunc() assignFunc {
return func(op overload, target, v, _ string) string {
return fmt.Sprintf(`
s, ns := %[2]s.Second(), %[2]s.Nanosecond()
%[1]s = memhash64(noescape(unsafe.Pointer(&s)), %[1]s)
%[1]s = memhash64(noescape(unsafe.Pointer(&ns)), %[1]s)
`, target, v)
}
}

func registerTypeCustomizers() {
typeCustomizers = make(map[coltypePair]typeCustomizer)
registerTypeCustomizer(coltypePair{coltypes.Bool, coltypes.Bool}, boolCustomizer{})
registerTypeCustomizer(coltypePair{coltypes.Bytes, coltypes.Bytes}, bytesCustomizer{})
registerTypeCustomizer(coltypePair{coltypes.Decimal, coltypes.Decimal}, decimalCustomizer{})
registerTypeCustomizer(coltypePair{coltypes.Timestamp, coltypes.Timestamp}, timestampCustomizer{})
for _, leftFloatType := range coltypes.FloatTypes {
for _, rightFloatType := range coltypes.FloatTypes {
registerTypeCustomizer(coltypePair{leftFloatType, rightFloatType}, floatCustomizer{width: 64})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math"
"github.com/cockroachdb/apd"
"time"
"github.com/cockroachdb/cockroach/pkg/sql/exec/execerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/exec/execgen/cmd/execgen/projection_ops_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const projTemplate = `
package exec
import (
"bytes"
"bytes"
"context"
"math"
"time"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/exec/execgen/cmd/execgen/selection_ops_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const selTemplate = `
package exec
import (
"bytes"
"bytes"
"context"
"math"
"time"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/exec/mem_estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func EstimateBatchSizeBytes(vecTypes []coltypes.T, batchLength int) int {
// Similar to byte arrays, we can't tell how much space is used
// to hold the arbitrary precision decimal objects.
acc += 50
case coltypes.Timestamp:
// time.Time has 2 int64s and a pointer.
acc += sizeOfInt64 * 3
default:
execerror.VectorizedInternalPanic(fmt.Sprintf("unhandled type %s", t))
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec/mergejoinbase_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"fmt"
"math"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand All @@ -44,6 +45,9 @@ var _ tree.Datum
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "math" package.
var _ = math.MaxInt64

Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec/mergejoiner_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"
"math"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand All @@ -46,6 +47,9 @@ var _ tree.Datum
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "math" package.
var _ = math.MaxInt64

Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec/min_max_agg_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package exec
import (
"bytes"
"math"
"time"

"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand All @@ -43,6 +44,9 @@ var _ bytes.Buffer
// Dummy import to pull in "apd" package.
var _ apd.Decimal

// Dummy import to pull in "time" package.
var _ time.Time

// Dummy import to pull in "tree" package.
var _ tree.Datum

Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/exec/random_testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/exec/execerror"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)

// maxVarLen specifies a length limit for variable length types (e.g. byte slices).
Expand Down Expand Up @@ -94,6 +95,11 @@ func RandomVec(rng *rand.Rand, typ coltypes.T, vec coldata.Vec, n int, nullProba
for i := 0; i < n; i++ {
floats[i] = rng.Float64()
}
case coltypes.Timestamp:
timestamps := vec.Timestamp()
for i := 0; i < n; i++ {
timestamps[i] = timeutil.Unix(rng.Int63n(1000000), rng.Int63n(1000000))
}
default:
execerror.VectorizedInternalPanic(fmt.Sprintf("unhandled type %s", typ))
}
Expand Down
Loading

0 comments on commit 55b1f97

Please sign in to comment.