Skip to content

Commit

Permalink
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into jira-babel…
Browse files Browse the repository at this point in the history
…-4008-2
  • Loading branch information
basasairohan authored Oct 20, 2023
2 parents 42d99be + 0fd93b3 commit e81b014
Show file tree
Hide file tree
Showing 7 changed files with 1,253 additions and 43 deletions.
10 changes: 5 additions & 5 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ CREATE OR REPLACE VIEW sys.sp_columns_100_view AS
, sys.translate_pg_type_to_tsql(a.atttypid) AS tsql_type_name
, sys.spt_datatype_info_table AS t5
WHERE (t4."DATA_TYPE" = CAST(t5.TYPE_NAME AS sys.nvarchar(128)) OR (t4."DATA_TYPE" = 'bytea' AND t5.TYPE_NAME = 'image'))
AND ext.dbid = cast(sys.db_id() as oid);
AND ext.dbid = sys.db_id();

GRANT SELECT on sys.sp_columns_100_view TO PUBLIC;

Expand Down Expand Up @@ -1128,7 +1128,7 @@ FROM pg_catalog.pg_class t1
WHERE t5.contype = 'p'
AND CAST(t4."ORDINAL_POSITION" AS smallint) = ANY (t5.conkey)
AND CAST(t4."ORDINAL_POSITION" AS smallint) = t5.conkey[seq]
AND ext.dbid = cast(sys.db_id() as oid);
AND ext.dbid = sys.db_id();

GRANT SELECT on sys.sp_pkeys_view TO PUBLIC;

Expand Down Expand Up @@ -1199,7 +1199,7 @@ CAST(t1.relpages AS int) AS PAGES,
CAST(NULL AS sys.varchar(128)) AS FILTER_CONDITION
FROM pg_catalog.pg_class t1
JOIN sys.schemas s1 ON s1.schema_id = t1.relnamespace
JOIN information_schema_tsql.columns t3 ON (t1.relname = t3."TABLE_NAME" COLLATE sys.database_default AND s1.name = t3."TABLE_SCHEMA")
JOIN information_schema_tsql.columns t3 ON (lower(t1.relname) = lower(t3."TABLE_NAME") COLLATE C AND s1.name = t3."TABLE_SCHEMA")
, generate_series(0,31) seq -- SQL server has max 32 columns per index
UNION
SELECT
Expand Down Expand Up @@ -1230,7 +1230,7 @@ CAST(NULL AS sys.varchar(128)) AS FILTER_CONDITION
FROM pg_catalog.pg_class t1
JOIN sys.schemas s1 ON s1.schema_id = t1.relnamespace
JOIN pg_catalog.pg_roles t3 ON t1.relowner = t3.oid
JOIN information_schema_tsql.columns t4 ON (t1.relname = t4."TABLE_NAME" COLLATE sys.database_default AND s1.name = t4."TABLE_SCHEMA")
JOIN information_schema_tsql.columns t4 ON (lower(t1.relname) = lower(t4."TABLE_NAME") COLLATE C AND s1.name = t4."TABLE_SCHEMA")
JOIN (pg_catalog.pg_index t5 JOIN
pg_catalog.pg_class t6 ON t5.indexrelid = t6.oid) ON t1.oid = t5.indrelid
JOIN pg_catalog.pg_namespace nsp ON (t1.relnamespace = nsp.oid)
Expand Down Expand Up @@ -3180,7 +3180,7 @@ BEGIN
IF @currtype = 'TR' OR @currtype = 'TA'
BEGIN
DECLARE @physical_schema_name sys.nvarchar(776) = '';
SELECT @physical_schema_name = nspname FROM sys.babelfish_namespace_ext WHERE dbid = cast(sys.db_id() as oid) AND orig_name = @schemaname;
SELECT @physical_schema_name = nspname FROM sys.babelfish_namespace_ext WHERE dbid = sys.db_id() AND orig_name = @schemaname;
SELECT @curr_relname = relname FROM pg_catalog.pg_trigger tr LEFT JOIN pg_catalog.pg_class c ON tr.tgrelid = c.oid LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE tr.tgname = @subname AND n.nspname = @physical_schema_name;
END
Expand Down
18 changes: 9 additions & 9 deletions contrib/babelfishpg_tsql/sql/information_schema_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.columns AS
AND (pg_has_role(c.relowner, 'USAGE')
OR has_column_privilege(c.oid, a.attnum,
'SELECT, INSERT, UPDATE, REFERENCES'))
AND ext.dbid = cast(sys.db_id() as oid);
AND ext.dbid =sys.db_id();

GRANT SELECT ON information_schema_tsql.columns TO PUBLIC;

Expand Down Expand Up @@ -395,7 +395,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.domains AS
WHERE (pg_has_role(t.typowner, 'USAGE')
OR has_type_privilege(t.oid, 'USAGE'))
AND (t.typtype = 'd' OR is_tbl_type)
AND ext.dbid = cast(sys.db_id() as oid);
AND ext.dbid = sys.db_id();

GRANT SELECT ON information_schema_tsql.domains TO PUBLIC;

Expand Down Expand Up @@ -425,7 +425,7 @@ CREATE VIEW information_schema_tsql.tables AS
AND (pg_has_role(c.relowner, 'USAGE')
OR has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES') )
AND ext.dbid = cast(sys.db_id() as oid)
AND ext.dbid = sys.db_id()
AND (NOT c.relname = 'sysdatabases');

GRANT SELECT ON information_schema_tsql.tables TO PUBLIC;
Expand Down Expand Up @@ -463,7 +463,7 @@ CREATE VIEW information_schema_tsql.table_constraints AS
AND (pg_has_role(r.relowner, 'USAGE')
OR has_table_privilege(r.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
OR has_any_column_privilege(r.oid, 'SELECT, INSERT, UPDATE, REFERENCES') )
AND extc.dbid = cast(sys.db_id() as oid);
AND extc.dbid = sys.db_id();

GRANT SELECT ON information_schema_tsql.table_constraints TO PUBLIC;

Expand Down Expand Up @@ -498,7 +498,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.views AS
AND (pg_has_role(c.relowner, 'USAGE')
OR has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES') )
AND ext.dbid = cast(sys.db_id() as oid);
AND ext.dbid = sys.db_id();

GRANT SELECT ON information_schema_tsql.views TO PUBLIC;

Expand All @@ -524,7 +524,7 @@ CREATE VIEW information_schema_tsql.check_constraints AS
AND (pg_has_role(r.relowner, 'USAGE')
OR has_table_privilege(r.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
OR has_any_column_privilege(r.oid, 'SELECT, INSERT, UPDATE, REFERENCES'))
AND extc.dbid = cast(sys.db_id() as oid);
AND extc.dbid = sys.db_id();

GRANT SELECT ON information_schema_tsql.check_constraints TO PUBLIC;

Expand Down Expand Up @@ -712,7 +712,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.routines AS
AND has_function_privilege(p.oid, 'EXECUTE')
AND (pg_has_role(t.typowner, 'USAGE')
OR has_type_privilege(t.oid, 'USAGE'))
AND ext.dbid = cast(sys.db_id() as oid)
AND ext.dbid = sys.db_id()
AND p.prolang = l.oid
AND p.prorettype = t.oid
AND p.pronamespace = nc.oid
Expand Down Expand Up @@ -744,7 +744,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.SEQUENCES AS
pg_sequence s join pg_class r on s.seqrelid = r.oid join pg_type t on s.seqtypid=t.oid,
sys.translate_pg_type_to_tsql(s.seqtypid) AS tsql_type_name
WHERE nc.oid = r.relnamespace
AND extc.dbid = cast(sys.db_id() as oid)
AND extc.dbid = sys.db_id()
AND r.relkind = 'S'
AND (NOT pg_is_other_temp_schema(nc.oid))
AND (pg_has_role(r.relowner, 'USAGE')
Expand Down Expand Up @@ -799,7 +799,7 @@ CREATE OR REPLACE VIEW information_schema_tsql.schemata AS
CAST(null AS sys.sysname) AS "DEFAULT_CHARACTER_SET_NAME"
FROM ((pg_catalog.pg_namespace np LEFT JOIN sys.pg_namespace_ext nc on np.nspname = nc.nspname)
LEFT JOIN pg_catalog.pg_roles r on r.oid = nc.nspowner) LEFT JOIN sys.babelfish_namespace_ext ext on nc.nspname = ext.nspname
WHERE (ext.dbid = cast(sys.db_id() as oid) OR np.nspname in ('sys', 'information_schema_tsql')) AND
WHERE (ext.dbid = sys.db_id() OR np.nspname in ('sys', 'information_schema_tsql')) AND
(pg_has_role(np.nspowner, 'USAGE') OR has_schema_privilege(np.oid, 'CREATE, USAGE'))
ORDER BY nc.nspname, np.nspname;

Expand Down
6 changes: 3 additions & 3 deletions contrib/babelfishpg_tsql/sql/ownership.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ select
CAST(ext.orig_name as sys.SYSNAME) as name
, base.oid as schema_id
, base.nspowner as principal_id
from pg_catalog.pg_namespace base INNER JOIN sys.babelfish_namespace_ext ext on base.nspname = ext.nspname
where base.nspname not in ('information_schema', 'pg_catalog', 'pg_toast', 'sys', 'public')
and ext.dbid = cast(sys.db_id() as oid);
from pg_catalog.pg_namespace base
inner join sys.babelfish_namespace_ext ext on base.nspname = ext.nspname
where ext.dbid = sys.db_id();
GRANT SELECT ON sys.schemas TO PUBLIC;
CREATE SEQUENCE sys.babelfish_db_seq MAXVALUE 32767 CYCLE;

Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ BEGIN
RETURN NULL;
ELSIF (SELECT COUNT(nspname) FROM sys.babelfish_namespace_ext ext
WHERE ext.orig_name = bbf_schema_name
AND CAST(ext.dbid AS oid) = CAST(sys.db_id() AS oid)) != 1 THEN
AND ext.dbid = sys.db_id()) != 1 THEN
RETURN 0;
END IF;
END IF;
Expand Down
39 changes: 15 additions & 24 deletions contrib/babelfishpg_tsql/sql/sys_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
create or replace view sys.table_types_internal as
SELECT pt.typrelid
FROM pg_catalog.pg_type pt
INNER join sys.schemas sch on pt.typnamespace = sch.schema_id
INNER JOIN pg_catalog.pg_depend dep ON pt.typrelid = dep.objid
INNER JOIN pg_catalog.pg_class pc ON pc.oid = dep.objid
WHERE pt.typtype = 'c' AND dep.deptype = 'i' AND pc.relkind = 'r';
Expand Down Expand Up @@ -62,11 +61,11 @@ select
, CAST(null as integer) as history_table_id
, CAST(0 as sys.bit) as is_remote_data_archive_enabled
, CAST(0 as sys.bit) as is_external
from pg_class t
where t.relnamespace in (select schema_id from sys.schemas)
and t.relpersistence in ('p', 'u', 't')
from pg_class t
inner join sys.schemas sch on sch.schema_id = t.relnamespace
left join sys.table_types_internal tt on t.oid = tt.typrelid
where tt.typrelid is null
and t.relkind = 'r'
and t.oid not in (select typrelid from sys.table_types_internal)
and has_schema_privilege(t.relnamespace, 'USAGE')
and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER');
GRANT SELECT ON sys.tables TO PUBLIC;
Expand Down Expand Up @@ -399,12 +398,13 @@ from pg_attribute a
inner join pg_class c on c.oid = a.attrelid
inner join pg_type t on t.oid = a.atttypid
inner join pg_namespace s on s.oid = c.relnamespace
left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id())
left join pg_attrdef d on c.oid = d.adrelid and a.attnum = d.adnum
left join pg_collation coll on coll.oid = a.attcollation
, sys.translate_pg_type_to_tsql(a.atttypid) AS tsql_type_name
, sys.translate_pg_type_to_tsql(t.typbasetype) AS tsql_base_type_name
where not a.attisdropped
and (s.oid in (select schema_id from sys.schemas) or s.nspname = 'sys')
and (s.nspname = 'sys' or ext.nspname is not null)
-- r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized view, c = composite type, f = foreign table, p = partitioned table
and c.relkind in ('r', 'v', 'm', 'f', 'p')
and has_schema_privilege(s.oid, 'USAGE')
Expand Down Expand Up @@ -1054,25 +1054,16 @@ left join pg_catalog.pg_locks blocking_locks
GRANT SELECT ON sys.sysprocesses TO PUBLIC;

create or replace view sys.types As
with RECURSIVE type_code_list as
(
select distinct pg_typname as pg_type_name, tsql_typname as tsql_type_name
from sys.babelfish_typecode_list()
),
tt_internal as MATERIALIZED
(
Select * from sys.table_types_internal
)
-- For System types
select
ti.tsql_type_name as name
tsql_type_name as name
, t.oid as system_type_id
, t.oid as user_type_id
, s.oid as schema_id
, cast(NULL as INT) as principal_id
, sys.tsql_type_max_length_helper(ti.tsql_type_name, t.typlen, t.typtypmod, true) as max_length
, cast(sys.tsql_type_precision_helper(ti.tsql_type_name, t.typtypmod) as int) as precision
, cast(sys.tsql_type_scale_helper(ti.tsql_type_name, t.typtypmod, false) as int) as scale
, sys.tsql_type_max_length_helper(tsql_type_name, t.typlen, t.typtypmod, true) as max_length
, cast(sys.tsql_type_precision_helper(tsql_type_name, t.typtypmod) as int) as precision
, cast(sys.tsql_type_scale_helper(tsql_type_name, t.typtypmod, false) as int) as scale
, CASE c.collname
WHEN 'default' THEN default_collation_name
ELSE c.collname
Expand All @@ -1085,11 +1076,11 @@ select
, 0 as is_table_type
from pg_type t
inner join pg_namespace s on s.oid = t.typnamespace
inner join type_code_list ti on t.typname = ti.pg_type_name
left join pg_collation c on c.oid = t.typcollation
, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name
,cast(current_setting('babelfishpg_tsql.server_collation_name') as name) as default_collation_name
where
ti.tsql_type_name IS NOT NULL
tsql_type_name IS NOT NULL
and pg_type_is_visible(t.oid)
and (s.nspname = 'pg_catalog' OR s.nspname = 'sys')
union all
Expand Down Expand Up @@ -1118,14 +1109,14 @@ select cast(t.typname as text) as name
, case when tt.typrelid is not null then 1 else 0 end as is_table_type
from pg_type t
join sys.schemas sch on t.typnamespace = sch.schema_id
left join type_code_list ti on t.typname = ti.pg_type_name
left join pg_collation c on c.oid = t.typcollation
left join tt_internal tt on t.typrelid = tt.typrelid
left join sys.table_types_internal tt on t.typrelid = tt.typrelid
, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name
, sys.translate_pg_type_to_tsql(t.typbasetype) AS tsql_base_type_name
, cast(current_setting('babelfishpg_tsql.server_collation_name') as name) as default_collation_name
-- we want to show details of user defined datatypes created under babelfish database
where
ti.tsql_type_name IS NULL
tsql_type_name IS NULL
and
(
-- show all user defined datatypes created under babelfish database except table types
Expand Down
Loading

0 comments on commit e81b014

Please sign in to comment.