Skip to content

Commit

Permalink
Store original name for Babelfish indexes (#3429)
Browse files Browse the repository at this point in the history
In Babelfish, index names are generated by first calculating hash of given original index name
and table name concatenated together and then concatenating index name, table name and
this hash together once again to form a 64 character long identifier.
But this name is not intuitive for users as it looks different from what user specified while
creating the index.
To address this issue, this commit adds ability to store original index name as a reloption for a
given index and show that stored original name in the catalog views, like sys.indexes.
This CR extends this solution to also store original index names for partitioned indexes.
Additionally, support renaming of indexes using `sp_rename` so that existing indexes can be
renamed so that their original names get stored in the catalog.
 
Task: BABEL-1517
Signed-off-by: Rishabh Tanwar <[email protected]>
  • Loading branch information
rishabhtanwar29 authored Feb 12, 2025
1 parent 5f23598 commit 639deb6
Show file tree
Hide file tree
Showing 54 changed files with 1,870 additions and 526 deletions.
33 changes: 27 additions & 6 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2985,11 +2985,18 @@ BEGIN
DECLARE @row_count INT;
SELECT @row_count = COUNT(*) FROM #sp_rename_temptable2;

IF @objtype = 'COLUMN'
IF @objtype = 'COLUMN' OR @objtype = 'INDEX'
BEGIN
IF @row_count = 1
BEGIN
THROW 33557097, N'Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.', 1;
IF @objtype = 'COLUMN'
BEGIN
THROW 33557097, N'Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.', 1;
END;
ELSE
BEGIN
THROW 33557097, N'Either the parameter @objname is ambiguous or the claimed @objtype (INDEX) is wrong.', 1;
END
END
ELSE IF @row_count > 4
BEGIN
Expand Down Expand Up @@ -3071,10 +3078,6 @@ BEGIN
BEGIN
THROW 33557097, N'Please provide @objtype that is supported in Babelfish', 1;
END
ELSE IF @objtype = 'INDEX'
BEGIN
THROW 33557097, N'Feature not supported: renaming object type Index', 1;
END
ELSE IF @objtype = 'STATISTICS'
BEGIN
THROW 33557097, N'Feature not supported: renaming object type Statistics', 1;
Expand Down Expand Up @@ -3104,6 +3107,24 @@ BEGIN
END
SET @currtype = 'CO';
END
ELSE IF @objtype = 'INDEX'
BEGIN
DECLARE @relid INT = 0;
DECLARE @index_count INT;
SELECT @relid = object_id FROM sys.objects o1 INNER JOIN sys.schemas s1 ON o1.schema_id = s1.schema_id
WHERE s1.name = @schemaname AND o1.name = @curr_relname;
IF @relid = 0
BEGIN
THROW 33557097, N'There is no object with the given @objname.', 1;
END
SELECT @index_count = COUNT(*) FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid
WHERE i.indrelid = @relid AND c.relname = sys.babelfish_construct_unique_index_name(@subname, @curr_relname);
IF @index_count < 0
BEGIN
THROW 33557097, N'There is no object with the given @objname.', 1;
END
SET @currtype = 'IX';
END
ELSE IF @objtype = 'USERDATATYPE'
BEGIN
DECLARE @alias_count INT;
Expand Down
4 changes: 4 additions & 0 deletions contrib/babelfishpg_tsql/sql/sys_function_helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11431,3 +11431,7 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION sys.babelfish_split_identifier(IN identifier VARCHAR, OUT value VARCHAR)
RETURNS SETOF VARCHAR AS 'babelfishpg_tsql', 'split_identifier_internal'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.babelfish_construct_unique_index_name(index_name TEXT, table_name TEXT)
RETURNS TEXT AS 'babelfishpg_tsql', 'bbf_construct_unique_index_name'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
11 changes: 10 additions & 1 deletion contrib/babelfishpg_tsql/sql/sys_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,16 @@ with index_id_map as MATERIALIZED(
)
select
cast(X.indrelid as int) as object_id
, cast(I.relname as sys.sysname) as name
, cast(
coalesce(
(select pg_catalog.string_agg(
case
when option like 'bbf_original_rel_name=%' then substring(option, 23 /* prefix length */)
else null
end, ',')
from unnest(I.reloptions) as option),
I.relname)
AS sys.sysname) AS name
, cast(case when X.indisclustered then 1 else 2 end as sys.tinyint) as type
, cast(case when X.indisclustered then 'CLUSTERED' else 'NONCLUSTERED' end as sys.nvarchar(60)) as type_desc
, cast(X.indisunique as sys.bit) as is_unique
Expand Down
Loading

0 comments on commit 639deb6

Please sign in to comment.