Skip to content

Commit f31787e

Browse files
authored
Merge pull request #143638 from cockroachdb/blathers/backport-release-24.1-143632
release-24.1: sql: only re-use resolved routine type when flag is set
2 parents b261e7a + 5a9ab40 commit f31787e

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

pkg/sql/logictest/testdata/logic_test/udf

+25
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ LIMIT
974974
subtest end
975975

976976
# Regression test for #104009.
977+
subtest regression_104009
978+
977979
statement ok
978980
CREATE TABLE ab104009(a INT PRIMARY KEY, b INT)
979981

@@ -997,3 +999,26 @@ PREPARE p AS SELECT f($1::REGCLASS::INT)
997999

9981000
statement ok
9991001
EXECUTE p(10)
1002+
1003+
# Regression test for #142615.
1004+
subtest regression_142615
1005+
1006+
statement ok
1007+
create function app_to_db_id(app_id INT8) RETURNS INT8 LANGUAGE SQL AS $$ SELECT app_id * 2; $$;
1008+
1009+
statement ok
1010+
create sequence seq1;
1011+
1012+
statement ok
1013+
create table test (id int8 not null default app_to_db_id(nextval('seq1'::REGCLASS)));
1014+
1015+
query TTITT rowsort
1016+
select * from pg_catalog.pg_attrdef;
1017+
----
1018+
1508958170 132 2 unique_rowid() unique_rowid()
1019+
1202826234 151 1 gen_random_uuid() gen_random_uuid()
1020+
1202826232 151 3 now() now()
1021+
2121222035 174 1 public.app_to_db_id(nextval('public.seq1'::REGCLASS)) public.app_to_db_id(nextval('public.seq1'::REGCLASS))
1022+
2121222032 174 2 unique_rowid() unique_rowid()
1023+
1024+
subtest end

pkg/sql/opt/optbuilder/values.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ func (b *Builder) buildValuesClause(
7777
elemPos += numCols
7878
if texpr.ResolvedType().IsWildcardType() {
7979
// Type-check the expression once again in order to update expressions
80-
// that wrap a routine to reflect the modified type. Make sure to use
81-
// the previously resolved type as the desired type, since the AST may
82-
// have been modified to remove type annotations. This can happen when
83-
// the routine's return type is unknown until its body is built.
84-
texpr, err = tree.TypeCheck(b.ctx, texpr, b.semaCtx, texpr.ResolvedType())
85-
if err != nil {
86-
panic(err)
87-
}
80+
// that wrap a routine to reflect the modified type.
81+
func() {
82+
defer b.semaCtx.Properties.Restore(b.semaCtx.Properties)
83+
b.semaCtx.Properties.RoutineUseResolvedType = true
84+
texpr, err = tree.TypeCheck(b.ctx, texpr, b.semaCtx, desired)
85+
if err != nil {
86+
panic(err)
87+
}
88+
}()
8889
}
8990
if typ := texpr.ResolvedType(); typ.Family() != types.UnknownFamily {
9091
if colTypes[colIdx].Family() == types.UnknownFamily {

pkg/sql/sem/tree/type_check.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ type SemaProperties struct {
9696
// IgnoreUnpreferredOverloads is set to true when "unpreferred" overloads
9797
// should not be used during type-checking and overload resolution.
9898
IgnoreUnpreferredOverloads bool
99+
100+
// RoutineUseResolvedType is set to true when type-checking for a routine
101+
// should reuse the already resolved type, if any. This is used to handle
102+
// "re-type-checking" that occurs for a RECORD-returning routine, for which
103+
// the return type is not known until the routine body is built.
104+
RoutineUseResolvedType bool
99105
}
100106

101107
type semaRequirements struct {
@@ -1154,9 +1160,10 @@ func (expr *FuncExpr) typeCheckWithFuncAncestor(semaCtx *SemaContext, fn func()
11541160
func (expr *FuncExpr) TypeCheck(
11551161
ctx context.Context, semaCtx *SemaContext, desired *types.T,
11561162
) (TypedExpr, error) {
1157-
if expr.fn != nil && expr.fn.Type != BuiltinRoutine && expr.typ != nil {
1158-
// Don't overwrite the resolved properties for a user-defined routine if the
1159-
// routine has already been resolved.
1163+
if semaCtx != nil && semaCtx.Properties.RoutineUseResolvedType &&
1164+
expr.typ != nil && !expr.typ.IsWildcardType() {
1165+
// Don't overwrite the resolved properties for a routine if the routine has
1166+
// already been resolved (and we are in a context that needs this behavior).
11601167
return expr, nil
11611168
}
11621169

0 commit comments

Comments
 (0)