Skip to content

Commit

Permalink
exec: add random projection tests for all types
Browse files Browse the repository at this point in the history
This test randomly runs binary comparisons against all types with random
data, verifying that the result of Datum.Compare matches the result of
the exec projection.

This found the bug with date infinity matching tracked in cockroachdb#40354, and
immediately found a bug with the timestamp implementation in the
previous commit, so I'm fairly sure it's a useful random test to have
around.

Release note: None
  • Loading branch information
jordanlewis committed Sep 5, 2019
1 parent b9b019e commit 8d64a30
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/distsqlrun/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (m *materializer) next() (sqlbase.EncDatumRow, *distsqlpb.ProducerMetadata)
continue
}
}
m.row[colIdx].Datum = exec.PhysicalTypeColElemToDatum(col, rowIdx, m.da, typs[colIdx])
m.row[colIdx].Datum = exec.PhysicalTypeColElemToDatum(col, rowIdx, m.da, &typs[colIdx])
}
return m.ProcessRowHelper(m.row), nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/builtin_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (b *defaultBuiltinFuncOperator) Next(ctx context.Context) coldata.Batch {
hasNulls = true
b.row[j] = tree.DNull
} else {
b.row[j] = PhysicalTypeColElemToDatum(col, rowIdx, b.da, b.columnTypes[b.argumentCols[j]])
b.row[j] = PhysicalTypeColElemToDatum(col, rowIdx, b.da, &b.columnTypes[b.argumentCols[j]])
}
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/sql/exec/projection_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import (

"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/exec/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/assert"
)

func TestProjPlusInt64Int64ConstOp(t *testing.T) {
Expand Down Expand Up @@ -142,6 +147,74 @@ func TestGetProjectionConstMixedTypeOperator(t *testing.T) {
}
}

func TestRandomComparisons(t *testing.T) {
rng, _ := randutil.NewPseudoRand()
evalCtx := tree.NewTestingEvalContext(cluster.MakeTestingClusterSettings())
ctx := evalCtx.Ctx()
expected := make([]bool, 2048)
var da sqlbase.DatumAlloc
lDatums := make([]tree.Datum, 2048)
rDatums := make([]tree.Datum, 2048)
for _, ct := range types.Scalar {
if ct.Family() == types.DateFamily {
// TODO(jordan): #40354 tracks failure to compare infinite dates.
continue
}
typ := typeconv.FromColumnType(ct)
if typ == coltypes.Unhandled {
continue
}
typs := []coltypes.T{typ, typ, coltypes.Bool}
b := coldata.NewMemBatchWithSize(typs, 2048)
lVec := b.ColVec(0)
rVec := b.ColVec(1)
ret := b.ColVec(2)
RandomVec(rng, typ, lVec, 2048, 0)
RandomVec(rng, typ, rVec, 2048, 0)
for i := range lDatums {
lDatums[i] = PhysicalTypeColElemToDatum(lVec, uint16(i), da, ct)
rDatums[i] = PhysicalTypeColElemToDatum(rVec, uint16(i), da, ct)
}
for _, cmpOp := range []tree.ComparisonOperator{tree.EQ, tree.NE, tree.LT, tree.LE, tree.GT, tree.GE} {
for i := range lDatums {
cmp := lDatums[i].Compare(evalCtx, rDatums[i])
var b bool
switch cmpOp {
case tree.EQ:
b = cmp == 0
case tree.NE:
b = cmp != 0
case tree.LT:
b = cmp < 0
case tree.LE:
b = cmp <= 0
case tree.GT:
b = cmp > 0
case tree.GE:
b = cmp >= 0
}
expected[i] = b
}
input := newChunkingBatchSource(typs, []coldata.Vec{lVec, rVec, ret}, 2048)
op, err := GetProjectionOperator(ct, ct, cmpOp, input, 0, 1, 2)
if err != nil {
t.Fatal(err)
}
op.Init()
var idx uint16
for batch := op.Next(ctx); batch.Length() > 0; batch = op.Next(ctx) {
for i := uint16(0); i < batch.Length(); i++ {
absIdx := idx + i
assert.Equal(t, expected[absIdx], batch.ColVec(2).Bool()[i],
"expected %s %s %s (%s[%d]) to be %t found %t", lDatums[absIdx], cmpOp, rDatums[absIdx], ct, absIdx,
expected[absIdx], ret.Bool()[i])
}
idx += batch.Length()
}
}
}
}

func TestGetProjectionOperator(t *testing.T) {
ct := types.Int2
binOp := tree.Mult
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/exec/vec_elem_to_datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package exec

import (
"fmt"
"time"
"unsafe"

"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand All @@ -25,7 +26,7 @@ import (

// PhysicalTypeColElemToDatum converts an element in a colvec to a datum of semtype ct.
func PhysicalTypeColElemToDatum(
col coldata.Vec, rowIdx uint16, da sqlbase.DatumAlloc, ct types.T,
col coldata.Vec, rowIdx uint16, da sqlbase.DatumAlloc, ct *types.T,
) tree.Datum {
switch ct.Family() {
case types.BoolFamily:
Expand Down Expand Up @@ -60,6 +61,8 @@ func PhysicalTypeColElemToDatum(
return da.NewDBytes(tree.DBytes(col.Bytes().Get(int(rowIdx))))
case types.OidFamily:
return da.NewDOid(tree.MakeDOid(tree.DInt(col.Int64()[rowIdx])))
case types.TimestampFamily:
return tree.MakeDTimestamp(col.Timestamp()[rowIdx], time.Microsecond)
default:
execerror.VectorizedInternalPanic(fmt.Sprintf("Unsupported column type %s", ct.String()))
// This code is unreachable, but the compiler cannot infer that.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/row/cfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ func (rf *CFetcher) getDatumAt(colIdx int, rowIdx uint16, typ types.T) tree.Datu
if rf.machine.colvecs[colIdx].Nulls().NullAt(rowIdx) {
return tree.DNull
}
return exec.PhysicalTypeColElemToDatum(rf.machine.colvecs[colIdx], rowIdx, rf.table.da, typ)
return exec.PhysicalTypeColElemToDatum(rf.machine.colvecs[colIdx], rowIdx, rf.table.da, &typ)
}

// processValue processes the state machine's current value component, setting
Expand Down

0 comments on commit 8d64a30

Please sign in to comment.