-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
97093: builtins: automatically add builtins for each type cast r=rafiss a=otan In PG, casts from one type to another can also be use the function syntax, e.g. `date(now())` = `now()::date`. This is done at type resolution time. Unfortunately we do not support that in type resolution, and from experience long ago it was tricky to do so (happy to be proven wrong). This change instead defines a builtin for each castable type, which emulates the same behavior. We already kind of do this for `oid` and `inet`, so this isn't much worse right? Release note (sql change): Each type cast is now expressable as a function, e.g. `now()::date` can be expressed as `date(now())`. Resolves #97067 Co-authored-by: Oliver Tan <[email protected]>
- Loading branch information
Showing
20 changed files
with
762 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 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 builtins_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/cast" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/volatility" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/lib/pq/oid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestCastBuiltins sanity checks all casts for the cast map work. | ||
// Note we don't have to check for families or anything crazy like we do | ||
// for shouldMakeFromCastBuiltin. | ||
func TestCastBuiltins(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
serv, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{}) | ||
defer serv.Stopper().Stop(ctx) | ||
|
||
cast.ForEachCast(func( | ||
fromOID oid.Oid, toOID oid.Oid, castCtx cast.Context, ctxOrigin cast.ContextOrigin, v volatility.V, | ||
) { | ||
fromTyp, ok := types.OidToType[fromOID] | ||
if !ok { | ||
return | ||
} | ||
// Cannot cast as tuple. | ||
if fromTyp.Family() == types.TupleFamily { | ||
return | ||
} | ||
toTyp, ok := types.OidToType[toOID] | ||
if !ok { | ||
return | ||
} | ||
toName := tree.Name(cast.CastTypeName(toTyp)) | ||
q := fmt.Sprintf("SELECT %s(NULL::%s)", toName.String(), fromTyp.String()) | ||
t.Run(q, func(t *testing.T) { | ||
_, err := sqlDB.Exec(q) | ||
require.NoError(t, err) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.