Skip to content

Commit

Permalink
opt: add volatility info for Unknown to Tuple cast
Browse files Browse the repository at this point in the history
Casts from Unknown to any type are allowed as a special case, but we had no cast
defined from Unknown to Tuple (leading to an assertion error). This change adds
this missing information. I checked that now all type families define a cast
from Unknown.

Fixes #50258.

Release note (bug fix): fix recently introduced "no volatility for cast
unknown::tuple" error.
  • Loading branch information
RaduBerinde committed Jun 19, 2020
1 parent cbac99f commit 2b7cab1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/sql/opt/memo/testdata/logprops/scalar
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,23 @@ project
├── fd: (3)-->(1,2)
├── prune: (1-3)
└── interesting orderings: (+3)

# Regression test for #50258: cast from unknown to tuple.
expr
(Values
[
(Tuple [ (Cast (Null "unknown") "tuple{string}") ] "tuple{string}" )
]
[ (Cols [ (NewColumn "a" "tuple{string}") ]) ]
)
----
values
├── columns: a:1(tuple{string})
├── cardinality: [1 - 1]
├── immutable
├── key: ()
├── fd: ()-->(1)
├── prune: (1)
└── tuple [type=tuple{string}]
└── cast: [type=tuple{string}]
└── null [type=unknown]
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/casts.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ var validCasts = []castInfo{
{from: types.StringFamily, to: types.EnumFamily, volatility: VolatilityImmutable},
{from: types.EnumFamily, to: types.EnumFamily, volatility: VolatilityImmutable},
{from: types.BytesFamily, to: types.EnumFamily, volatility: VolatilityImmutable},

// Casts to TupleFamily.
{from: types.UnknownFamily, to: types.TupleFamily, volatility: VolatilityImmutable},
}

type castsMapKey struct {
Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/sem/tree/casts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ func TestCastsVolatilityMatchesPostgres(t *testing.T) {
}
}
}

// TestCastsFromUnknown verifies that there is a cast from Unknown defined for
// all type families.
func TestCastsFromUnknown(t *testing.T) {
defer leaktest.AfterTest(t)()

for v := range types.Family_name {
switch fam := types.Family(v); fam {
case types.UnknownFamily, types.AnyFamily:
// These type families are exceptions.

default:
cast := lookupCast(types.UnknownFamily, fam)
if cast == nil {
t.Errorf("cast from Unknown to %s does not exist", fam)
}
}
}
}

0 comments on commit 2b7cab1

Please sign in to comment.