Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-24.2: opt: type check VALUES rows after building them #137721

Open
wants to merge 1 commit into
base: release-24.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/ccl/testccl/sqlccl/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestExplainGist(t *testing.T) {

// Use the release-build panic-catching behavior instead of the
// crdb_test-build behavior. This is needed so that some known bugs like
// #117101 don't result in a test failure.
// #119045 and #133129 don't result in a test failure.
defer colexecerror.ProductionBehaviorForTests()()

ctx := context.Background()
Expand Down Expand Up @@ -195,7 +195,6 @@ func TestExplainGist(t *testing.T) {
if err != nil && strings.Contains(err.Error(), "internal error") {
// Ignore all errors except the internal ones.
for _, knownErr := range []string{
"invalid datum type given: RECORD, expected RECORD", // #117101
"expected equivalence dependants to be its closure", // #119045
"not in index", // #133129
} {
Expand Down
22 changes: 22 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf
Original file line number Diff line number Diff line change
Expand Up @@ -925,4 +925,26 @@ SELECT f('0');
statement ok
DROP FUNCTION f;

# Regression test for #117101 - update the resolved type of a VALUES row with a
# UDF after the UDF is built.
subtest regression_117101

statement ok
CREATE FUNCTION f117101() RETURNS RECORD CALLED ON NULL INPUT AS $funcbody$
SELECT ((42)::INT8, (43)::INT8)
$funcbody$ LANGUAGE SQL;

statement error pgcode 42804 pq: VALUES types tuple{int, int} and tuple{tuple{string, int}, unknown} cannot be matched
SELECT
*
FROM
(
VALUES
(
(('aloha'::TEXT,
(44)::INT8), NULL)
),
(COALESCE(f117101(), NULL))
);

subtest end
8 changes: 8 additions & 0 deletions pkg/sql/opt/optbuilder/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func (b *Builder) buildValuesClause(
// resolving the column types.
elems[elemPos] = b.buildScalar(texpr, inScope, nil, nil, nil)
elemPos += numCols
// Type-check the expression once again in order to update expressions
// that wrap a UDF to reflect the modified type. Make sure to use the
// previously resolved type as the desired type, since the AST may have
// been modified to remove type annotations.
texpr, err = tree.TypeCheck(b.ctx, texpr, b.semaCtx, texpr.ResolvedType())
if err != nil {
panic(err)
}
if typ := texpr.ResolvedType(); typ.Family() != types.UnknownFamily {
if colTypes[colIdx].Family() == types.UnknownFamily {
colTypes[colIdx] = typ
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/sem/tree/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,12 @@ func (expr *FuncExpr) typeCheckWithFuncAncestor(semaCtx *SemaContext, fn func()
func (expr *FuncExpr) TypeCheck(
ctx context.Context, semaCtx *SemaContext, desired *types.T,
) (TypedExpr, error) {
if expr.fn != nil && expr.fn.Type != BuiltinRoutine && expr.typ != nil {
// Don't overwrite the resolved properties for a user-defined routine if the
// routine has already been resolved.
return expr, nil
}

searchPath := EmptySearchPath
var resolver FunctionReferenceResolver
if semaCtx != nil {
Expand Down