Skip to content

Commit aa90415

Browse files
craig[bot]jeffswenson
andcommitted
Merge #156878
156878: eval: add missing identity cast implementations r=jeffswenson a=jeffswenson This adds missing identity casts for the ltree and void types. Identity casts are usually elided by the optimizer, but LDR random schema tests were flaking on schemas that involve these types. Release note: none Epic: CRDB-48647 Co-authored-by: Jeff Swenson <[email protected]>
2 parents 5e4549d + 40760ca commit aa90415

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

pkg/sql/sem/eval/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ go_test(
116116
name = "eval_test",
117117
srcs = [
118118
"cast_map_test.go",
119+
"cast_rand_test.go",
119120
"cast_test.go",
120121
"compare_test.go",
121122
"eval_internal_test.go",

pkg/sql/sem/eval/cast.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ func performCastWithoutPrecisionTruncation(
961961
}
962962
case types.LTreeFamily:
963963
switch v := d.(type) {
964+
case *tree.DLTree:
965+
return d, nil
964966
case *tree.DString:
965967
ltree, err := tree.ParseDLTree(string(*v))
966968
if err != nil {
@@ -1051,7 +1053,7 @@ func performCastWithoutPrecisionTruncation(
10511053
}
10521054
case types.VoidFamily:
10531055
switch d.(type) {
1054-
case *tree.DString, *tree.DCollatedString:
1056+
case *tree.DString, *tree.DCollatedString, *tree.DVoid:
10551057
return tree.DVoidDatum, nil
10561058
}
10571059
}

pkg/sql/sem/eval/cast_rand_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package eval_test
7+
8+
import (
9+
"context"
10+
"testing"
11+
12+
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
13+
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
14+
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
15+
"github.com/cockroachdb/cockroach/pkg/sql/types"
16+
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
17+
"github.com/cockroachdb/cockroach/pkg/util/log"
18+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
19+
"github.com/stretchr/testify/require"
20+
)
21+
22+
func TestCastIdentityRandom(t *testing.T) {
23+
defer leaktest.AfterTest(t)()
24+
defer log.Scope(t).Close(t)
25+
26+
var usesFamily func(typ *types.T, family types.Family) bool
27+
usesFamily = func(typ *types.T, family types.Family) bool {
28+
switch typ.Family() {
29+
case family:
30+
return true
31+
case types.ArrayFamily:
32+
return usesFamily(typ.ArrayContents(), family)
33+
case types.TupleFamily:
34+
for _, tupleType := range typ.TupleContents() {
35+
if usesFamily(tupleType, family) {
36+
return true
37+
}
38+
}
39+
return false
40+
default:
41+
return false
42+
}
43+
}
44+
45+
ctx := context.Background()
46+
castCtx := eval.MakeTestingEvalContext(cluster.MakeTestingClusterSettings())
47+
rng, _ := randutil.NewTestRand()
48+
for trial := 0; trial < 100; trial++ {
49+
typ := randgen.RandType(rng)
50+
51+
// Test eval context does not support the OidFamily
52+
if usesFamily(typ, types.OidFamily) {
53+
continue
54+
}
55+
56+
value := randgen.RandDatum(rng, typ, true /* nullOk */)
57+
58+
// A value should cast to its own type and be equal to itself after the
59+
// cast.
60+
identityCastValue, err := eval.PerformCast(ctx, &castCtx, value, typ)
61+
require.NoError(t, err)
62+
63+
cmp, err := value.Compare(ctx, &castCtx, identityCastValue)
64+
require.NoError(t, err)
65+
require.Zero(t, cmp)
66+
67+
// A value should cast to its canonical type and be equal to itself after
68+
// the cast.
69+
canonicalCastValue, err := eval.PerformCast(ctx, &castCtx, value, typ.Canonical())
70+
require.NoError(t, err)
71+
72+
cmp, err = value.Compare(ctx, &castCtx, canonicalCastValue)
73+
require.NoError(t, err)
74+
require.Zero(t, cmp)
75+
}
76+
}

0 commit comments

Comments
 (0)