Skip to content

Commit 8c6b04c

Browse files
committed
schemachanger: add function back-references for unvalidated check constraints
When adding a check constraint with NOT VALID that references a function, the declarative schema changer was not creating the necessary bidirectional references between the constraint and the function. This caused validation to fail with "depends-on function has no corresponding depended-on-by back reference". The fix adds the missing dependency in the opgen rules. Interestingly, the opgen rules for validated constraints do have the proper function dep handling. Fixes #155400 Release note (bug fix): Added proper dependency handling when adding a constraint with NOT VALID that references a UDF.
1 parent 738c039 commit 8c6b04c

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

pkg/sql/logictest/testdata/logic_test/udf_in_constraints

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ SELECT get_fn_depended_on_by($fn_id)
115115
statement ok
116116
ALTER TABLE t1 ADD CONSTRAINT cka CHECK (f1(a) > 1);
117117

118+
# Regression test for issue #155400 - add new constraint with NOT VALID
119+
statement ok
120+
ALTER TABLE t1 ADD CONSTRAINT ckb CHECK (f1(b) > 2) NOT VALID;
121+
118122
query T
119123
SELECT get_checks($tbl_id);
120124
----
@@ -134,6 +138,15 @@ SELECT get_checks($tbl_id);
134138
"constraintId": 3,
135139
"expr": "[FUNCTION 100106](a) \u003e 1:::INT8",
136140
"name": "cka"
141+
},
142+
{
143+
"columnIds": [
144+
2
145+
],
146+
"constraintId": 4,
147+
"expr": "[FUNCTION 100106](b) \u003e 2:::INT8",
148+
"name": "ckb",
149+
"validity": "Unvalidated"
137150
}
138151
]
139152

@@ -144,7 +157,8 @@ SELECT get_fn_depended_on_by($fn_id)
144157
{
145158
"constraintIds": [
146159
2,
147-
3
160+
3,
161+
4
148162
],
149163
"id": 111
150164
}
@@ -165,7 +179,8 @@ SELECT get_fn_depended_on_by($fn_id)
165179
{
166180
"constraintIds": [
167181
2,
168-
3
182+
3,
183+
4
169184
],
170185
"id": 111
171186
},
@@ -189,7 +204,8 @@ SELECT get_fn_depended_on_by($fn_id)
189204
{
190205
"constraintIds": [
191206
2,
192-
3
207+
3,
208+
4
193209
],
194210
"id": 111
195211
},
@@ -211,7 +227,8 @@ SELECT get_fn_depended_on_by($fn_id)
211227
{
212228
"constraintIds": [
213229
2,
214-
3
230+
3,
231+
4
215232
],
216233
"id": 111
217234
}

pkg/sql/schemachanger/scplan/internal/opgen/opgen_check_constraint_unvalidated.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func init() {
4343
BackReferencedTableID: this.TableID,
4444
}
4545
}),
46+
emit(func(this *scpb.CheckConstraintUnvalidated) *scop.AddTableConstraintBackReferencesInFunctions {
47+
if len(this.UsesFunctionIDs) == 0 {
48+
return nil
49+
}
50+
return &scop.AddTableConstraintBackReferencesInFunctions{
51+
FunctionIDs: this.UsesFunctionIDs,
52+
BackReferencedTableID: this.TableID,
53+
BackReferencedConstraintID: this.ConstraintID,
54+
}
55+
}),
4656
),
4757
),
4858
toAbsent(
@@ -72,6 +82,16 @@ func init() {
7282
BackReferencedTableID: this.TableID,
7383
}
7484
}),
85+
emit(func(this *scpb.CheckConstraintUnvalidated) *scop.RemoveTableConstraintBackReferencesFromFunctions {
86+
if len(this.UsesFunctionIDs) == 0 {
87+
return nil
88+
}
89+
return &scop.RemoveTableConstraintBackReferencesFromFunctions{
90+
FunctionIDs: this.UsesFunctionIDs,
91+
BackReferencedTableID: this.TableID,
92+
BackReferencedConstraintID: this.ConstraintID,
93+
}
94+
}),
7595
),
7696
),
7797
)

0 commit comments

Comments
 (0)