Skip to content

Commit

Permalink
Fix system procedure sp_pkeys which may fail to return any row if par…
Browse files Browse the repository at this point in the history
…allel mode is enforced (#2147)

It is Postgres peculiarity with Hash cond (with operator text = name and text = name) that hash functions being used to
hash text and name data type values are totally different, for example, hashtext would consider explicit collation whereas
hashname does not care about explicit collation. That is the reason why hash condition would fail every time. (hashtext and hashname may produce different hash value for same input under collation other than "C").

This means that whenever there is such hash condition for Hash join then it would always fail and will not return any row.
And optimiser can choose such hash join based on stats. Such issue is observed with helper view sys.sp_pkeys_view which is being used by system procedure sp_pkeys causing procedure to return zero rows when optimiser chooses
certain hash join. This commit fixes such issue by casting name data type to appropriate T-SQL data type.

Task: BABEL-4603
Signed-off-by: Dipesh Dhameliya [email protected]
  • Loading branch information
Deepesh125 authored Dec 14, 2023
1 parent 91ba800 commit 3f629a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ FROM pg_catalog.pg_class t1
JOIN sys.pg_namespace_ext t2 ON t1.relnamespace = t2.oid
JOIN pg_catalog.pg_roles t3 ON t1.relowner = t3.oid
LEFT OUTER JOIN sys.babelfish_namespace_ext ext on t2.nspname = ext.nspname
JOIN information_schema_tsql.columns t4 ON (t1.relname = t4."TABLE_NAME" COLLATE sys.database_default AND ext.orig_name = t4."TABLE_SCHEMA" )
JOIN information_schema_tsql.columns t4 ON (cast(t1.relname as sys.nvarchar(128)) = t4."TABLE_NAME" AND ext.orig_name = t4."TABLE_SCHEMA" )
JOIN pg_constraint t5 ON t1.oid = t5.conrelid
, generate_series(1,16) seq -- SQL server has max 16 columns per primary key
WHERE t5.contype = 'p'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ LANGUAGE plpgsql;
* final behaviour.
*/

CREATE OR REPLACE VIEW sys.sp_pkeys_view AS
SELECT
CAST(t4."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER,
CAST(t4."TABLE_SCHEMA" AS sys.sysname) AS TABLE_OWNER,
CAST(t1.relname AS sys.sysname) AS TABLE_NAME,
CAST(t4."COLUMN_NAME" AS sys.sysname) AS COLUMN_NAME,
CAST(seq AS smallint) AS KEY_SEQ,
CAST(t5.conname AS sys.sysname) AS PK_NAME
FROM pg_catalog.pg_class t1
JOIN sys.pg_namespace_ext t2 ON t1.relnamespace = t2.oid
JOIN pg_catalog.pg_roles t3 ON t1.relowner = t3.oid
LEFT OUTER JOIN sys.babelfish_namespace_ext ext on t2.nspname = ext.nspname
JOIN information_schema_tsql.columns t4 ON (cast(t1.relname as sys.nvarchar(128)) = t4."TABLE_NAME" AND ext.orig_name = t4."TABLE_SCHEMA" )
JOIN pg_constraint t5 ON t1.oid = t5.conrelid
, generate_series(1,16) seq -- SQL server has max 16 columns per primary key
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 = sys.db_id();


-- Drops the temporary procedure used by the upgrade script.
-- Please have this be one of the last statements executed in this upgrade script.
DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar);
Expand Down
4 changes: 0 additions & 4 deletions test/JDBC/parallel_query_jdbc_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ ignore#!#babel_datetime-vu-verify
ignore#!#babel_datetime
ignore#!#BABEL-2812-vu-verify

# output difference: JIRA-4542
ignore#!#sys-sp_pkeys-dep-vu-verify
ignore#!#BABEL-SP_PKEYS

# Test failures caused by babel_extra_join_test_cases_northwind failure
ignore#!#babelfish_sysdatabases-vu-prepare
ignore#!#babelfish_sysdatabases-vu-verify
Expand Down

0 comments on commit 3f629a7

Please sign in to comment.