Skip to content

Commit

Permalink
Update views sys.triggers, sys.objects, sys.all_objects to show the c…
Browse files Browse the repository at this point in the history
…orrect object_id of trigger (#2832)

* Update views sys.triggers, sys.objects, sys.all_objects to show the correct object_id of trigger (#2830)

Show object_id of pg_trigger instead of pg_proc in the views sys.triggers, sys.events, sys.objects, sys.all_objects etc for triggers. It removes inconsistency of object_id between OBJECT_ID() and SYS views.

If the object type is specified as 'p' and it's actually a trigger, then object_id() should return NULL.

Issues Resolved: Task: BABEL-5108, BABEL-3927

Signed-off-by: Shalini Lohia [email protected]

* Update upgrade script

---------

Signed-off-by: Shalini Lohia [email protected]
Co-authored-by: Shalini Lohia <[email protected]>
  • Loading branch information
shalinilohia50 and lohia-shalini authored Aug 5, 2024
1 parent 37a7838 commit 21a50c0
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 21 deletions.
11 changes: 11 additions & 0 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,17 @@ object_id(PG_FUNCTION_ARGS)
!strcmp(object_type, "pc") || !strcmp(object_type, "tf") || !strcmp(object_type, "rf") ||
!strcmp(object_type, "x"))
{
/*
* If the object type is not specified as 'tr' and it's actually a trigger,
* then object_id() should return NULL.
*/
if (OidIsValid(tsql_get_trigger_oid(object_name, schema_oid, user_id)))
{
pfree(object_name);
pfree(object_type);
PG_RETURN_NULL();
}

/* search in pg_proc by name and schema oid */
result = tsql_get_proc_oid(object_name, schema_oid, user_id);
}
Expand Down
39 changes: 31 additions & 8 deletions contrib/babelfishpg_tsql/sql/sys_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,9 @@ union all
-- details of system defined procedures
select
p.proname::sys.sysname as name
, p.oid as object_id
, case
when t.typname = 'trigger' then tr.oid else p.oid
end as object_id
, null::integer as principal_id
, s.oid as schema_id
, cast (case when tr.tgrelid is not null
Expand Down Expand Up @@ -1532,7 +1534,9 @@ union all
-- details of user defined procedures
select
p.proname::sys.sysname as name
, p.oid as object_id
, case
when t.typname = 'trigger' then tr.oid else p.oid
end as object_id
, null::integer as principal_id
, s.oid as schema_id
, cast (case when tr.tgrelid is not null
Expand Down Expand Up @@ -1751,7 +1755,7 @@ CREATE OR REPLACE VIEW sys.triggers
AS
SELECT
CAST(p.proname as sys.sysname) as name,
CAST(p.oid as int) as object_id,
CAST(tr.oid as int) as object_id,
CAST(1 as sys.tinyint) as parent_class,
CAST('OBJECT_OR_COLUMN' as sys.nvarchar(60)) AS parent_class_desc,
CAST(tr.tgrelid as int) AS parent_id,
Expand Down Expand Up @@ -1861,7 +1865,7 @@ select
CAST(tr.name as sys.sysname) as name
, CAST(tr.object_id as int) as object_id
, CAST(NULL as int) as principal_id
, CAST(p.pronamespace as int) as schema_id
, CAST(p.relnamespace as int) as schema_id
, CAST(tr.parent_id as int) as parent_object_id
, CAST(tr.type as char(2)) as type
, CAST(tr.type_desc as sys.nvarchar(60)) as type_desc
Expand All @@ -1871,7 +1875,7 @@ select
, CAST(0 as sys.bit) as is_published
, CAST(0 as sys.bit) as is_schema_published
from sys.triggers tr
inner join pg_proc p on p.oid = tr.object_id
inner join pg_class p on p.oid = tr.parent_id
union all
select
CAST(def.name as sys.sysname) as name
Expand Down Expand Up @@ -1988,7 +1992,7 @@ CREATE OR REPLACE VIEW sys.all_sql_modules_internal AS
SELECT
ao.object_id AS object_id
, CAST(
CASE WHEN ao.type in ('P', 'FN', 'IN', 'TF', 'RF', 'IF', 'TR') THEN COALESCE(f.definition, '')
CASE WHEN ao.type in ('P', 'FN', 'IN', 'TF', 'RF', 'IF') THEN COALESCE(f.definition, '')
WHEN ao.type = 'V' THEN COALESCE(bvd.definition, '')
ELSE NULL
END
Expand Down Expand Up @@ -2021,7 +2025,26 @@ LEFT OUTER JOIN sys.babelfish_view_def bvd
LEFT JOIN pg_proc p ON ao.object_id = CAST(p.oid AS INT)
LEFT JOIN sys.babelfish_function_ext f ON ao.name = f.funcname COLLATE "C" AND ao.schema_id::regnamespace::name = f.nspname
AND sys.babelfish_get_pltsql_function_signature(ao.object_id) = f.funcsignature COLLATE "C"
WHERE ao.type in ('P', 'RF', 'V', 'TR', 'FN', 'IF', 'TF', 'R');
WHERE ao.type in ('P', 'RF', 'V', 'FN', 'IF', 'TF', 'R')
UNION ALL
SELECT
ao.object_id AS object_id
, CAST(COALESCE(f.definition, '') AS sys.nvarchar) AS definition
, CAST(1 as sys.bit) AS uses_ansi_nulls
, CAST(1 as sys.bit) AS uses_quoted_identifier
, CAST(0 as sys.bit) AS is_schema_bound
, CAST(0 as sys.bit) AS uses_database_collation
, CAST(0 as sys.bit) AS is_recompiled
, CAST(0 AS sys.bit) as null_on_null_input
, null::integer as execute_as_principal_id
, CAST(0 as sys.bit) as uses_native_compilation
, CAST(ao.is_ms_shipped as INT) as is_ms_shipped
FROM sys.all_objects ao
LEFT OUTER JOIN sys.pg_namespace_ext nmext on ao.schema_id = nmext.oid
LEFT JOIN pg_trigger tr ON ao.object_id = CAST(tr.oid AS INT)
LEFT JOIN sys.babelfish_function_ext f ON ao.name = f.funcname COLLATE "C" AND ao.schema_id::regnamespace::name = f.nspname
AND sys.babelfish_get_pltsql_function_signature(tr.tgfoid) = f.funcsignature COLLATE "C"
WHERE ao.type = 'TR';
GRANT SELECT ON sys.all_sql_modules_internal TO PUBLIC;

CREATE OR REPLACE VIEW sys.all_sql_modules AS
Expand Down Expand Up @@ -3265,7 +3288,7 @@ GRANT SELECT ON sys.numbered_procedures TO PUBLIC;
CREATE OR REPLACE VIEW sys.events
AS
SELECT
CAST(pt.tgfoid as int) AS object_id
CAST(pt.oid as int) AS object_id
, CAST(
CASE
WHEN tr.event_manipulation='INSERT' THEN 1
Expand Down
Loading

0 comments on commit 21a50c0

Please sign in to comment.