Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store original name for Babelfish indexes #3429

Open
wants to merge 5 commits into
base: BABEL_5_X_DEV
Choose a base branch
from

Conversation

rishabhtanwar29
Copy link
Contributor

@rishabhtanwar29 rishabhtanwar29 commented Jan 24, 2025

Description

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]

Test Scenarios Covered

  • Use case based -

  • Boundary conditions -

  • Arbitrary inputs -

  • Negative test cases -

  • Minor version upgrade tests -

  • Major version upgrade tests -

  • Performance tests -

  • Tooling impact -

  • Client tests -

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@coveralls
Copy link
Collaborator

coveralls commented Jan 24, 2025

Pull Request Test Coverage Report for Build 13014792579

Details

  • 57 of 58 (98.28%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 74.995%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contrib/babelfishpg_tsql/src/pltsql_utils.c 20 21 95.24%
Totals Coverage Status
Change from base Build 13004242762: 0.01%
Covered Lines: 47097
Relevant Lines: 62800

💛 - Coveralls

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babelfish_sp_rename_word_parse is making sure these are not NULL for sure, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

IndexStmt *stmt = (IndexStmt *) parsetree;

if (sql_dialect == SQL_DIALECT_TSQL &&
strcmp(queryString, "(CREATE FULLTEXT INDEX STATEMENT )") != 0) /* Skip fulltext indexes since they don't even have an original name */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create macro like:

/* BBF SUBCOMMANDS QUERY STRING */
#define CREATE_LOGICAL_DATABASE "(CREATE LOGICAL DATABASE )"
#define CREATE_GUEST_SCHEMAS_DURING_UPGRADE "(CREATE GUEST SCHEMAS DURING UPGRADE )"
#define CREATE_FIXED_DB_ROLES "(CREATE FIXED DATABASE ROLES )"
#define ALTER_DEFAULT_PRIVILEGES "(ALTER DEFAULT PRIVILEGES )"
#define INTERNAL_GRANT_STATEMENT "(GRANT STATEMENT )"
#define INTERNAL_REVOKE_ALL_ON_ROUTINE "(REVOKE ALL ON ROUTINE )"
#define INTERNAL_ALTER_ROLE "(ALTER ROLE ADD )"

Comment on lines +5749 to +5754
CREATE OR REPLACE PROCEDURE sys.sp_renamedb(
IN "@objname" sys.SYSNAME,
IN "@newname" sys.SYSNAME
)
AS 'babelfishpg_tsql', 'sp_renamedb_internal'
LANGUAGE C;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added by mistake.

else if (strcmp(objtype, "IX") == 0)
{
objtype_code = OBJECT_INDEX;
process_util_querystr = "(ALTER INDEX )";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macro might be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already having such hardcoded command names for all other kind of objects in this function. Moreover, macro is mostly useful when it is being used at multiple places, which is not true for this case so I think we can keep it hardcoded for now.

Comment on lines +837 to +846
, 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impact on performance? IK this is not avoidable, just curious

@@ -4,6 +4,15 @@ GO
ALTER TABLE babel_4817_t3 ADD col4 INT
GO

sp_rename 'babel_4817_t1.babel_4817_t1_idx_1', 'babel_4817_t1_idx_2', 'INDEX'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test indexname > 64 len

@@ -1009,7 +1069,7 @@ varchar#!#int#!#int
363863941f079adaa9aa733200e57c9f_partition_3_value_idx#!#4#!#3
363863941f079adaa9aa733200e57c9f_partition_3#!#4#!#0
363863941f079adaa9aa733200e57c9f_partition_3_id_key#!#4#!#2
partition_vu_prepare_normal_inded6315af14d88f45711ba24e46851b8f#!#1#!#2
partition_vu_prepare_normal_ind5f5fcd81a18b69e8a21aef69ce61f7b6#!#1#!#2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. How is partition_id getting changed with renaming index? Looks like an existing bug with sys.partitions. Please check with @sumitj824
  2. Also looking at this output, Eventually we should fix OBJECT_NAME to give the original name instead of hashed name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 2nd, object_name/object_id functions only work for schema scoped functions but index is a table scoped object in T-SQL so these functions are not actually meant to handle indexes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants