Skip to content

Commit bbbfdbd

Browse files
authored
Merge pull request #155526 from spilchen/blathers/backport-release-25.2-155404
release-25.2: schemachanger: add function back-references for unvalidated check constraints
2 parents c75e10e + cc67fbc commit bbbfdbd

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
@@ -102,6 +102,10 @@ SELECT get_fn_depended_on_by($fn_id)
102102
statement ok
103103
ALTER TABLE t1 ADD CONSTRAINT cka CHECK (f1(a) > 1);
104104

105+
# Regression test for issue #155400 - add new constraint with NOT VALID
106+
statement ok
107+
ALTER TABLE t1 ADD CONSTRAINT ckb CHECK (f1(b) > 2) NOT VALID;
108+
105109
query T
106110
SELECT get_checks($tbl_id);
107111
----
@@ -121,6 +125,15 @@ SELECT get_checks($tbl_id);
121125
"constraintId": 3,
122126
"expr": "[FUNCTION 100106](a) \u003e 1:::INT8",
123127
"name": "cka"
128+
},
129+
{
130+
"columnIds": [
131+
2
132+
],
133+
"constraintId": 4,
134+
"expr": "[FUNCTION 100106](b) \u003e 2:::INT8",
135+
"name": "ckb",
136+
"validity": "Unvalidated"
124137
}
125138
]
126139

@@ -131,7 +144,8 @@ SELECT get_fn_depended_on_by($fn_id)
131144
{
132145
"constraintIds": [
133146
2,
134-
3
147+
3,
148+
4
135149
],
136150
"id": 111
137151
}
@@ -152,7 +166,8 @@ SELECT get_fn_depended_on_by($fn_id)
152166
{
153167
"constraintIds": [
154168
2,
155-
3
169+
3,
170+
4
156171
],
157172
"id": 111
158173
},
@@ -176,7 +191,8 @@ SELECT get_fn_depended_on_by($fn_id)
176191
{
177192
"constraintIds": [
178193
2,
179-
3
194+
3,
195+
4
180196
],
181197
"id": 111
182198
},
@@ -198,7 +214,8 @@ SELECT get_fn_depended_on_by($fn_id)
198214
{
199215
"constraintIds": [
200216
2,
201-
3
217+
3,
218+
4
202219
],
203220
"id": 111
204221
}

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)