Skip to content

Commit 5d5f6d8

Browse files
authored
Include unique constraints metadata in sys.objects view (#3658) (#3660)
This commit rectifies the type check for fetching metadata of both primary key and unique constraints from sys.key_constraints catalog inorder to correctly populate sys.objects view. Task: BABEL-5712 Signed-off-by: Roshan Kanwar <rskanwar@amazon.com>
1 parent 4d9c93c commit 5d5f6d8

12 files changed

+248
-9
lines changed

contrib/babelfishpg_tsql/sql/sys_views.sql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,15 +1815,19 @@ select
18151815
, CAST(p.principal_id as int) as principal_id
18161816
, CAST(p.schema_id as int) as schema_id
18171817
, CAST(p.parent_object_id as int) as parent_object_id
1818-
, CAST('PK' as char(2)) as type
1819-
, CAST('PRIMARY_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc
1818+
, CAST(p.type as char(2)) as type
1819+
, CAST(
1820+
CASE p.type
1821+
WHEN 'PK' THEN 'PRIMARY_KEY_CONSTRAINT'
1822+
WHEN 'UQ' THEN 'UNIQUE_CONSTRAINT'
1823+
END
1824+
as sys.nvarchar(60)) as type_desc
18201825
, CAST(p.create_date as sys.datetime) as create_date
18211826
, CAST(p.modify_date as sys.datetime) as modify_date
18221827
, CAST(p.is_ms_shipped as sys.bit) as is_ms_shipped
18231828
, CAST(p.is_published as sys.bit) as is_published
18241829
, CAST(p.is_schema_published as sys.bit) as is_schema_published
18251830
from sys.key_constraints p
1826-
where p.type = 'PK'
18271831
union all
18281832
select
18291833
CAST(pr.name as sys.sysname) as name

contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.5.0--4.6.0.sql

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,179 @@ CREATE OR REPLACE FUNCTION sys.json_query(json_string text, path text default '$
10451045
RETURNS sys.NVARCHAR_JSON
10461046
AS 'babelfishpg_tsql', 'tsql_json_query' LANGUAGE C IMMUTABLE PARALLEL SAFE;
10471047

1048+
create or replace view sys.objects as
1049+
select
1050+
CAST(t.name as sys.sysname) as name
1051+
, CAST(t.object_id as int) as object_id
1052+
, CAST(t.principal_id as int) as principal_id
1053+
, CAST(t.schema_id as int) as schema_id
1054+
, CAST(t.parent_object_id as int) as parent_object_id
1055+
, CAST('U' as char(2)) as type
1056+
, CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc
1057+
, CAST(t.create_date as sys.datetime) as create_date
1058+
, CAST(t.modify_date as sys.datetime) as modify_date
1059+
, CAST(t.is_ms_shipped as sys.bit) as is_ms_shipped
1060+
, CAST(t.is_published as sys.bit) as is_published
1061+
, CAST(t.is_schema_published as sys.bit) as is_schema_published
1062+
from sys.tables t
1063+
union all
1064+
select
1065+
CAST(v.name as sys.sysname) as name
1066+
, CAST(v.object_id as int) as object_id
1067+
, CAST(v.principal_id as int) as principal_id
1068+
, CAST(v.schema_id as int) as schema_id
1069+
, CAST(v.parent_object_id as int) as parent_object_id
1070+
, CAST('V' as char(2)) as type
1071+
, CAST('VIEW' as sys.nvarchar(60)) as type_desc
1072+
, CAST(v.create_date as sys.datetime) as create_date
1073+
, CAST(v.modify_date as sys.datetime) as modify_date
1074+
, CAST(v.is_ms_shipped as sys.bit) as is_ms_shipped
1075+
, CAST(v.is_published as sys.bit) as is_published
1076+
, CAST(v.is_schema_published as sys.bit) as is_schema_published
1077+
from sys.views v
1078+
union all
1079+
select
1080+
CAST(f.name as sys.sysname) as name
1081+
, CAST(f.object_id as int) as object_id
1082+
, CAST(f.principal_id as int) as principal_id
1083+
, CAST(f.schema_id as int) as schema_id
1084+
, CAST(f.parent_object_id as int) as parent_object_id
1085+
, CAST('F' as char(2)) as type
1086+
, CAST('FOREIGN_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc
1087+
, CAST(f.create_date as sys.datetime) as create_date
1088+
, CAST(f.modify_date as sys.datetime) as modify_date
1089+
, CAST(f.is_ms_shipped as sys.bit) as is_ms_shipped
1090+
, CAST(f.is_published as sys.bit) as is_published
1091+
, CAST(f.is_schema_published as sys.bit) as is_schema_published
1092+
from sys.foreign_keys f
1093+
union all
1094+
select
1095+
CAST(p.name as sys.sysname) as name
1096+
, CAST(p.object_id as int) as object_id
1097+
, CAST(p.principal_id as int) as principal_id
1098+
, CAST(p.schema_id as int) as schema_id
1099+
, CAST(p.parent_object_id as int) as parent_object_id
1100+
, CAST(p.type as char(2)) as type
1101+
, CAST(
1102+
CASE p.type
1103+
WHEN 'PK' THEN 'PRIMARY_KEY_CONSTRAINT'
1104+
WHEN 'UQ' THEN 'UNIQUE_CONSTRAINT'
1105+
END
1106+
as sys.nvarchar(60)) as type_desc
1107+
, CAST(p.create_date as sys.datetime) as create_date
1108+
, CAST(p.modify_date as sys.datetime) as modify_date
1109+
, CAST(p.is_ms_shipped as sys.bit) as is_ms_shipped
1110+
, CAST(p.is_published as sys.bit) as is_published
1111+
, CAST(p.is_schema_published as sys.bit) as is_schema_published
1112+
from sys.key_constraints p
1113+
union all
1114+
select
1115+
CAST(pr.name as sys.sysname) as name
1116+
, CAST(pr.object_id as int) as object_id
1117+
, CAST(pr.principal_id as int) as principal_id
1118+
, CAST(pr.schema_id as int) as schema_id
1119+
, CAST(pr.parent_object_id as int) as parent_object_id
1120+
, CAST(pr.type as char(2)) as type
1121+
, CAST(pr.type_desc as sys.nvarchar(60)) as type_desc
1122+
, CAST(pr.create_date as sys.datetime) as create_date
1123+
, CAST(pr.modify_date as sys.datetime) as modify_date
1124+
, CAST(pr.is_ms_shipped as sys.bit) as is_ms_shipped
1125+
, CAST(pr.is_published as sys.bit) as is_published
1126+
, CAST(pr.is_schema_published as sys.bit) as is_schema_published
1127+
from sys.procedures pr
1128+
union all
1129+
select
1130+
CAST(tr.name as sys.sysname) as name
1131+
, CAST(tr.object_id as int) as object_id
1132+
, CAST(NULL as int) as principal_id
1133+
, CAST(p.relnamespace as int) as schema_id
1134+
, CAST(tr.parent_id as int) as parent_object_id
1135+
, CAST(tr.type as char(2)) as type
1136+
, CAST(tr.type_desc as sys.nvarchar(60)) as type_desc
1137+
, CAST(tr.create_date as sys.datetime) as create_date
1138+
, CAST(tr.modify_date as sys.datetime) as modify_date
1139+
, CAST(tr.is_ms_shipped as sys.bit) as is_ms_shipped
1140+
, CAST(0 as sys.bit) as is_published
1141+
, CAST(0 as sys.bit) as is_schema_published
1142+
from sys.triggers tr
1143+
inner join pg_class p on p.oid = tr.parent_id
1144+
union all
1145+
select
1146+
CAST(def.name as sys.sysname) as name
1147+
, CAST(def.object_id as int) as object_id
1148+
, CAST(def.principal_id as int) as principal_id
1149+
, CAST(def.schema_id as int) as schema_id
1150+
, CAST(def.parent_object_id as int) as parent_object_id
1151+
, CAST(def.type as char(2)) as type
1152+
, CAST(def.type_desc as sys.nvarchar(60)) as type_desc
1153+
, CAST(def.create_date as sys.datetime) as create_date
1154+
, CAST(def.modified_date as sys.datetime) as modify_date
1155+
, CAST(def.is_ms_shipped as sys.bit) as is_ms_shipped
1156+
, CAST(def.is_published as sys.bit) as is_published
1157+
, CAST(def.is_schema_published as sys.bit) as is_schema_published
1158+
from sys.default_constraints def
1159+
union all
1160+
select
1161+
CAST(chk.name as sys.sysname) as name
1162+
, CAST(chk.object_id as int) as object_id
1163+
, CAST(chk.principal_id as int) as principal_id
1164+
, CAST(chk.schema_id as int) as schema_id
1165+
, CAST(chk.parent_object_id as int) as parent_object_id
1166+
, CAST(chk.type as char(2)) as type
1167+
, CAST(chk.type_desc as sys.nvarchar(60)) as type_desc
1168+
, CAST(chk.create_date as sys.datetime) as create_date
1169+
, CAST(chk.modify_date as sys.datetime) as modify_date
1170+
, CAST(chk.is_ms_shipped as sys.bit) as is_ms_shipped
1171+
, CAST(chk.is_published as sys.bit) as is_published
1172+
, CAST(chk.is_schema_published as sys.bit) as is_schema_published
1173+
from sys.check_constraints chk
1174+
union all
1175+
select
1176+
CAST(p.relname as sys.sysname) as name
1177+
, CAST(p.oid as int) as object_id
1178+
, CAST(null as int) as principal_id
1179+
, CAST(s.schema_id as int) as schema_id
1180+
, CAST(0 as int) as parent_object_id
1181+
, CAST('SO' as char(2)) as type
1182+
, CAST('SEQUENCE_OBJECT' as sys.nvarchar(60)) as type_desc
1183+
, CAST(null as sys.datetime) as create_date
1184+
, CAST(null as sys.datetime) as modify_date
1185+
, CAST(0 as sys.bit) as is_ms_shipped
1186+
, CAST(0 as sys.bit) as is_published
1187+
, CAST(0 as sys.bit) as is_schema_published
1188+
from pg_class p
1189+
inner join sys.schemas s on s.schema_id = p.relnamespace
1190+
and p.relkind = 'S'
1191+
union all
1192+
select
1193+
CAST(('TT_' || tt.name collate "C" || '_' || tt.type_table_object_id) as sys.sysname) as name
1194+
, CAST(tt.type_table_object_id as int) as object_id
1195+
, CAST(tt.principal_id as int) as principal_id
1196+
, CAST(tt.schema_id as int) as schema_id
1197+
, CAST(0 as int) as parent_object_id
1198+
, CAST('TT' as char(2)) as type
1199+
, CAST('TABLE_TYPE' as sys.nvarchar(60)) as type_desc
1200+
, CAST((select PG_CATALOG.string_agg(
1201+
case
1202+
when option like 'bbf_rel_create_date=%%' then substring(option, 21)
1203+
else NULL
1204+
end, ',')
1205+
from unnest(c.reloptions) as option)
1206+
as sys.datetime) as create_date
1207+
, CAST((select PG_CATALOG.string_agg(
1208+
case
1209+
when option like 'bbf_rel_create_date=%%' then substring(option, 21)
1210+
else NULL
1211+
end, ',')
1212+
from unnest(c.reloptions) as option)
1213+
as sys.datetime) as modify_date
1214+
, CAST(1 as sys.bit) as is_ms_shipped
1215+
, CAST(0 as sys.bit) as is_published
1216+
, CAST(0 as sys.bit) as is_schema_published
1217+
from sys.table_types tt
1218+
inner join pg_class c on tt.type_table_object_id = c.oid;
1219+
GRANT SELECT ON sys.objects TO PUBLIC;
1220+
10481221
DO $$
10491222
DECLARE
10501223
old_function_exists boolean;

test/JDBC/expected/ISC-Table_Constraints-vu-verify.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ master#!#sch1#!#tbl_pk_pkey#!#master#!#sch1#!#tbl_pk#!#PRIMARY KEY#!#NO#!#NO
7878

7979

8080
-- verify from sys.objects
81-
-- Note: sys.objects not showing unique constraints currently
8281
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints where table_name in ('tbl_pk','tbl_fk') order by constraint_name, constraint_schema;
8382
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') and object_name(parent_object_id) in ('tbl_pk','tbl_fk') order by name, schname;
8483
go
@@ -102,6 +101,8 @@ tbl_fk_a_fkey#!#dbo#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
102101
tbl_fk_a_fkey#!#sch1#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
103102
tbl_fk_pkey#!#dbo#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
104103
tbl_fk_pkey#!#sch1#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
104+
tbl_pk_c_key#!#dbo#!#tbl_pk#!#UNIQUE_CONSTRAINT
105+
tbl_pk_c_key#!#sch1#!#tbl_pk#!#UNIQUE_CONSTRAINT
105106
tbl_pk_d_check#!#dbo#!#tbl_pk#!#CHECK_CONSTRAINT
106107
tbl_pk_d_check#!#sch1#!#tbl_pk#!#CHECK_CONSTRAINT
107108
tbl_pk_pkey#!#dbo#!#tbl_pk#!#PRIMARY_KEY_CONSTRAINT

test/JDBC/expected/ISC-Table_Constraints.out

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ master#!#sch1#!#tbl_pk_pkey#!#master#!#sch1#!#tbl_pk#!#PRIMARY KEY#!#NO#!#NO
9494

9595

9696
-- verify from sys.objects
97-
-- Note: sys.objects not showing unique constraints currently
9897
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints order by constraint_name, constraint_schema;
9998
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') order by name, schname;
10099
go
@@ -118,6 +117,8 @@ tbl_fk_a_fkey#!#dbo#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
118117
tbl_fk_a_fkey#!#sch1#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
119118
tbl_fk_pkey#!#dbo#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
120119
tbl_fk_pkey#!#sch1#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
120+
tbl_pk_c_key#!#dbo#!#tbl_pk#!#UNIQUE_CONSTRAINT
121+
tbl_pk_c_key#!#sch1#!#tbl_pk#!#UNIQUE_CONSTRAINT
121122
tbl_pk_d_check#!#dbo#!#tbl_pk#!#CHECK_CONSTRAINT
122123
tbl_pk_d_check#!#sch1#!#tbl_pk#!#CHECK_CONSTRAINT
123124
tbl_pk_pkey#!#dbo#!#tbl_pk#!#PRIMARY_KEY_CONSTRAINT
@@ -349,7 +350,6 @@ master#!#sch1#!#tbl_pk_pkey#!#master#!#sch1#!#tbl_pk#!#PRIMARY KEY#!#NO#!#NO
349350

350351

351352
-- verify from sys.objects
352-
-- Note: sys.objects not showing unique constraints currently
353353
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints order by constraint_name, constraint_schema;
354354
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') order by name, schname;
355355
go
@@ -373,6 +373,8 @@ tbl_fk_a_fkey#!#dbo#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
373373
tbl_fk_a_fkey#!#sch1#!#tbl_fk#!#FOREIGN_KEY_CONSTRAINT
374374
tbl_fk_pkey#!#dbo#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
375375
tbl_fk_pkey#!#sch1#!#tbl_fk#!#PRIMARY_KEY_CONSTRAINT
376+
tbl_pk_c_key#!#dbo#!#tbl_pk#!#UNIQUE_CONSTRAINT
377+
tbl_pk_c_key#!#sch1#!#tbl_pk#!#UNIQUE_CONSTRAINT
376378
tbl_pk_d_check#!#dbo#!#tbl_pk#!#CHECK_CONSTRAINT
377379
tbl_pk_d_check#!#sch1#!#tbl_pk#!#CHECK_CONSTRAINT
378380
tbl_pk_pkey#!#dbo#!#tbl_pk#!#PRIMARY_KEY_CONSTRAINT

test/JDBC/expected/object_id_index_conflict-vu-cleanup.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ GO
1818
ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS unq_name;
1919
GO
2020

21+
ALTER TABLE object_id_conflict_t2 DROP CONSTRAINT IF EXISTS unq_num;
22+
GO
23+
2124
-- Table cleanup
25+
DROP TABLE IF EXISTS object_id_conflict_t2;
26+
GO
27+
2228
DROP TABLE IF EXISTS object_id_conflict_parent_t;
2329
GO
2430

test/JDBC/expected/object_id_index_conflict-vu-prepare.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ GO
3333

3434
CREATE INDEX object_id_idx_parent_id ON object_id_conflict_t (parent_id);
3535
GO
36+
37+
-- Test for unique constraints metadata for CREATE/ALTER TABLE
38+
CREATE TABLE object_id_conflict_t2 (id INT, num INT CONSTRAINT unq_num UNIQUE);
39+
GO

test/JDBC/expected/object_id_index_conflict-vu-verify.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,29 @@ int
6767
<NULL>
6868
~~END~~
6969

70+
71+
72+
-- Unique constraints metadata test
73+
-- CREATE TABLE constraint
74+
SELECT o.name AS constraint_name,
75+
o.type,
76+
o.type_desc
77+
FROM sys.objects o WHERE o.parent_object_id = OBJECT_ID('object_id_conflict_t2') AND o.type = 'UQ';
78+
GO
79+
~~START~~
80+
varchar#!#char#!#nvarchar
81+
unq_num#!#UQ#!#UNIQUE_CONSTRAINT
82+
~~END~~
83+
84+
85+
-- ALTER TABLE constraint
86+
SELECT o.name AS constraint_name,
87+
o.type,
88+
o.type_desc
89+
FROM sys.objects o WHERE o.parent_object_id = OBJECT_ID('object_id_conflict_t') AND o.type = 'UQ';
90+
GO
91+
~~START~~
92+
varchar#!#char#!#nvarchar
93+
unq_name#!#UQ#!#UNIQUE_CONSTRAINT
94+
~~END~~
95+

test/JDBC/input/ISC-Table_Constraints-vu-verify.mix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ select * from information_schema.table_constraints where table_name in ('tbl_pk'
2222
go
2323

2424
-- verify from sys.objects
25-
-- Note: sys.objects not showing unique constraints currently
2625
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints where table_name in ('tbl_pk','tbl_fk') order by constraint_name, constraint_schema;
2726
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') and object_name(parent_object_id) in ('tbl_pk','tbl_fk') order by name, schname;
2827
go

test/JDBC/input/ISC-Table_Constraints.mix

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ select * from information_schema.table_constraints where table_name in ('tbl_pk'
4747
go
4848

4949
-- verify from sys.objects
50-
-- Note: sys.objects not showing unique constraints currently
5150
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints order by constraint_name, constraint_schema;
5251

5352
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') order by name, schname;
@@ -210,7 +209,6 @@ select * from information_schema.table_constraints where table_name in ('tbl_pk'
210209
go
211210

212211
-- verify from sys.objects
213-
-- Note: sys.objects not showing unique constraints currently
214212
select constraint_name, constraint_schema, table_name, constraint_type from information_schema.table_constraints order by constraint_name, constraint_schema;
215213

216214
select name, schema_name(schema_id) as schname, object_name(parent_object_id),type_desc from sys.objects where type in ('C','F','PK','UQ') order by name, schname;

test/JDBC/input/object_id_index_conflict-vu-cleanup.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ GO
1818
ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS unq_name;
1919
GO
2020

21+
ALTER TABLE object_id_conflict_t2 DROP CONSTRAINT IF EXISTS unq_num;
22+
GO
23+
2124
-- Table cleanup
25+
DROP TABLE IF EXISTS object_id_conflict_t2;
26+
GO
27+
2228
DROP TABLE IF EXISTS object_id_conflict_parent_t;
2329
GO
2430

0 commit comments

Comments
 (0)