diff --git a/c-deps/rocksdb b/c-deps/rocksdb index ddcfb8041e92..8bd5ed57cc17 160000 --- a/c-deps/rocksdb +++ b/c-deps/rocksdb @@ -1 +1 @@ -Subproject commit ddcfb8041e92a7478a800a05ecd1f8e297dacd4d +Subproject commit 8bd5ed57cc179643869295a8ec45ccdef69b9f07 diff --git a/pkg/sql/distsqlrun/column_exec_setup.go b/pkg/sql/distsqlrun/column_exec_setup.go index 164d87789e33..04ce80812863 100644 --- a/pkg/sql/distsqlrun/column_exec_setup.go +++ b/pkg/sql/distsqlrun/column_exec_setup.go @@ -142,7 +142,9 @@ func newColOperator( aggSpec.Aggregations[0].FilterColIdx == nil && aggSpec.Aggregations[0].Func == distsqlpb.AggregatorSpec_COUNT_ROWS && !aggSpec.Aggregations[0].Distinct { - return exec.NewCountOp(inputs[0]), []types.T{types.Int64}, nil + op, err = exec.NewCountOp(inputs[0]), nil + columnTypes = []semtypes.T{*semtypes.Int} + break } var groupCols, orderedCols util.FastIntSet diff --git a/pkg/sql/exec/execgen/cmd/execgen/overloads.go b/pkg/sql/exec/execgen/cmd/execgen/overloads.go index 05395371fb4e..b801cc251093 100644 --- a/pkg/sql/exec/execgen/cmd/execgen/overloads.go +++ b/pkg/sql/exec/execgen/cmd/execgen/overloads.go @@ -336,6 +336,13 @@ func (c floatCustomizer) getHashAssignFunc() assignFunc { } } +func (c floatCustomizer) getCmpOpCompareFunc() compareFunc { + // Float comparisons need special handling for NaN. + return func(l, r string) string { + return fmt.Sprintf("compareFloats(float64(%s), float64(%s))", l, r) + } +} + func (c intCustomizer) getHashAssignFunc() assignFunc { return func(op overload, target, v, _ string) string { return fmt.Sprintf("%[1]s = memhash%[3]d(noescape(unsafe.Pointer(&%[2]s)), %[1]s)", target, v, c.width) diff --git a/pkg/sql/exec/execgen/cmd/execgen/projection_ops_gen.go b/pkg/sql/exec/execgen/cmd/execgen/projection_ops_gen.go index 1b927af4ac9f..0afd3a90f46f 100644 --- a/pkg/sql/exec/execgen/cmd/execgen/projection_ops_gen.go +++ b/pkg/sql/exec/execgen/cmd/execgen/projection_ops_gen.go @@ -61,9 +61,9 @@ func (p {{template "opRConstName" .}}) Next(ctx context.Context) coldata.Batch { batch.AppendCol(types.{{.RetTyp}}) } vec := batch.ColVec(p.colIdx) - col := vec.{{.LTyp}}()[:coldata.BatchSize] + col := vec.{{.LTyp}}() projVec := batch.ColVec(p.outputIdx) - projCol := projVec.{{.RetTyp}}()[:coldata.BatchSize] + projCol := projVec.{{.RetTyp}}() if sel := batch.Selection(); sel != nil { for _, i := range sel { {{(.Assign "projCol[i]" "col[i]" "p.constArg")}} @@ -105,9 +105,9 @@ func (p {{template "opLConstName" .}}) Next(ctx context.Context) coldata.Batch { batch.AppendCol(types.{{.RetTyp}}) } vec := batch.ColVec(p.colIdx) - col := vec.{{.RTyp}}()[:coldata.BatchSize] + col := vec.{{.RTyp}}() projVec := batch.ColVec(p.outputIdx) - projCol := projVec.{{.RetTyp}}()[:coldata.BatchSize] + projCol := projVec.{{.RetTyp}}() if sel := batch.Selection(); sel != nil { for _, i := range sel { {{(.Assign "projCol[i]" "p.constArg" "col[i]")}} @@ -149,11 +149,11 @@ func (p {{template "opName" .}}) Next(ctx context.Context) coldata.Batch { batch.AppendCol(types.{{.RetTyp}}) } projVec := batch.ColVec(p.outputIdx) - projCol := projVec.{{.RetTyp}}()[:coldata.BatchSize] + projCol := projVec.{{.RetTyp}}() vec1 := batch.ColVec(p.col1Idx) vec2 := batch.ColVec(p.col2Idx) - col1 := vec1.{{.LTyp}}()[:coldata.BatchSize] - col2 := vec2.{{.RTyp}}()[:coldata.BatchSize] + col1 := vec1.{{.LTyp}}() + col2 := vec2.{{.RTyp}}() if sel := batch.Selection(); sel != nil { for _, i := range sel { {{(.Assign "projCol[i]" "col1[i]" "col2[i]")}} diff --git a/pkg/sql/exec/execgen/cmd/execgen/selection_ops_gen.go b/pkg/sql/exec/execgen/cmd/execgen/selection_ops_gen.go index a49e2601d330..82b7073c0b9f 100644 --- a/pkg/sql/exec/execgen/cmd/execgen/selection_ops_gen.go +++ b/pkg/sql/exec/execgen/cmd/execgen/selection_ops_gen.go @@ -105,7 +105,7 @@ func (p *{{template "opConstName" .}}) Next(ctx context.Context) coldata.Batch { } vec := batch.ColVec(p.colIdx) - col := vec.{{.LTyp}}()[:coldata.BatchSize] + col := vec.{{.LTyp}}() var idx uint16 n := batch.Length() if vec.MaybeHasNulls() { @@ -143,8 +143,8 @@ func (p *{{template "opName" .}}) Next(ctx context.Context) coldata.Batch { vec1 := batch.ColVec(p.col1Idx) vec2 := batch.ColVec(p.col2Idx) - col1 := vec1.{{.LTyp}}()[:coldata.BatchSize] - col2 := vec2.{{.RTyp}}()[:coldata.BatchSize] + col1 := vec1.{{.LTyp}}() + col2 := vec2.{{.RTyp}}() n := batch.Length() var idx uint16 diff --git a/pkg/sql/exec/float.go b/pkg/sql/exec/float.go new file mode 100644 index 000000000000..a30cdca8333f --- /dev/null +++ b/pkg/sql/exec/float.go @@ -0,0 +1,34 @@ +// Copyright 2019 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package exec + +import "math" + +// compareFloats compares two float values. This function is necessary for NaN +// handling. In SQL, NaN is treated as less than all other float values. In Go, +// any comparison with NaN returns false. +func compareFloats(a, b float64) int { + if a < b { + return -1 + } + if a > b { + return 1 + } + // Compare bits so that NaN == NaN. + if math.Float64bits(a) == math.Float64bits(b) { + return 0 + } + // Either a or b is NaN. + if math.IsNaN(a) { + return -1 + } + return 1 +} diff --git a/pkg/sql/exec/select_in_tmpl.go b/pkg/sql/exec/select_in_tmpl.go index 69cb9ccde39f..2c72bed37575 100644 --- a/pkg/sql/exec/select_in_tmpl.go +++ b/pkg/sql/exec/select_in_tmpl.go @@ -177,7 +177,7 @@ func (si *selectInOp_TYPE) Next(ctx context.Context) coldata.Batch { } vec := batch.ColVec(si.colIdx) - col := vec._TemplateType()[:coldata.BatchSize] + col := vec._TemplateType() var idx uint16 n := batch.Length() @@ -247,10 +247,10 @@ func (pi *projectInOp_TYPE) Next(ctx context.Context) coldata.Batch { } vec := batch.ColVec(pi.colIdx) - col := vec._TemplateType()[:coldata.BatchSize] + col := vec._TemplateType() projVec := batch.ColVec(pi.outputIdx) - projCol := projVec.Bool()[:coldata.BatchSize] + projCol := projVec.Bool() projNulls := projVec.Nulls() n := batch.Length() diff --git a/pkg/sql/exec/sort_test.go b/pkg/sql/exec/sort_test.go index 335e18618f24..22772b4bf481 100644 --- a/pkg/sql/exec/sort_test.go +++ b/pkg/sql/exec/sort_test.go @@ -13,6 +13,7 @@ package exec import ( "context" "fmt" + "math" "math/rand" "sort" "testing" @@ -76,8 +77,8 @@ func TestSort(t *testing.T) { ordCols: []distsqlpb.Ordering_Column{{ColIdx: 0}}, }, { - tuples: tuples{{3.2}, {2.0}, {2.4}}, - expected: tuples{{2.0}, {2.4}, {3.2}}, + tuples: tuples{{3.2}, {2.0}, {2.4}, {math.NaN()}, {math.Inf(-1)}, {math.Inf(1)}}, + expected: tuples{{math.NaN()}, {math.Inf(-1)}, {2.0}, {2.4}, {3.2}, {math.Inf(1)}}, typ: []types.T{types.Float64}, ordCols: []distsqlpb.Ordering_Column{{ColIdx: 0}}, }, diff --git a/pkg/sql/exec/utils_test.go b/pkg/sql/exec/utils_test.go index d89e78787e37..3055319e89aa 100644 --- a/pkg/sql/exec/utils_test.go +++ b/pkg/sql/exec/utils_test.go @@ -13,6 +13,7 @@ package exec import ( "context" "fmt" + "math" "math/rand" "reflect" "sort" @@ -516,6 +517,14 @@ func tupleEquals(expected tuple, actual tuple) bool { return false } } else { + // Special case for NaN, since it does not equal itself. + if f1, ok := expected[i].(float64); ok { + if f2, ok := actual[i].(float64); ok { + if math.IsNaN(f1) && math.IsNaN(f2) { + continue + } + } + } if !reflect.DeepEqual(reflect.ValueOf(actual[i]).Convert(reflect.TypeOf(expected[i])).Interface(), expected[i]) { return false } diff --git a/pkg/sql/logictest/testdata/logic_test/vectorize b/pkg/sql/logictest/testdata/logic_test/vectorize index 40f536de147a..b3ffd8cab63b 100644 --- a/pkg/sql/logictest/testdata/logic_test/vectorize +++ b/pkg/sql/logictest/testdata/logic_test/vectorize @@ -444,3 +444,14 @@ query II SELECT * FROM t38753 ORDER BY y; ---- 0 NULL + +# Regression test for #38752. +query IIBB +SELECT count(*), count(*) + 1, count(*) > 4, count(*) + 1 > 4 FROM b +---- +4 5 false true + +query I +SELECT * FROM (SELECT count(*) AS x FROM b) WHERE x > 0; +---- +4