Skip to content

Commit

Permalink
Use "C" collation in sys.sp_statistics_view
Browse files Browse the repository at this point in the history
Signed-off-by: Sumit Jaiswal <[email protected]>
  • Loading branch information
Sumit Jaiswal authored and basasairohan committed Oct 16, 2023
1 parent c56df45 commit 3493bd9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
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 (t1.relname = 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 (t1.relname = 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
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,64 @@ END;
$body$
LANGUAGE plpgsql IMMUTABLE;

CREATE OR REPLACE VIEW sys.sp_statistics_view AS
SELECT
CAST(t3."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER,
CAST(t3."TABLE_SCHEMA" AS sys.sysname) AS TABLE_OWNER,
CAST(t3."TABLE_NAME" AS sys.sysname) AS TABLE_NAME,
CAST(NULL AS smallint) AS NON_UNIQUE,
CAST(NULL AS sys.sysname) AS INDEX_QUALIFIER,
CAST(NULL AS sys.sysname) AS INDEX_NAME,
CAST(0 AS smallint) AS TYPE,
CAST(NULL AS smallint) AS SEQ_IN_INDEX,
CAST(NULL AS sys.sysname) AS COLUMN_NAME,
CAST(NULL AS sys.varchar(1)) AS COLLATION,
CAST(t1.reltuples AS int) AS CARDINALITY,
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 C AND s1.name = t3."TABLE_SCHEMA")
, generate_series(0,31) seq -- SQL server has max 32 columns per index
UNION
SELECT
CAST(t4."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER,
CAST(t4."TABLE_SCHEMA" AS sys.sysname) AS TABLE_OWNER,
CAST(t4."TABLE_NAME" AS sys.sysname) AS TABLE_NAME,
CASE
WHEN t5.indisunique = 't' THEN CAST(0 AS smallint)
ELSE CAST(1 AS smallint)
END AS NON_UNIQUE,
CAST(t1.relname AS sys.sysname) AS INDEX_QUALIFIER,
-- the index name created by CREATE INDEX is re-mapped, find it (by checking
-- the ones not in pg_constraint) and restoring it back before display
CASE
WHEN t8.oid > 0 THEN CAST(t6.relname AS sys.sysname)
ELSE CAST(SUBSTRING(t6.relname,1,LENGTH(t6.relname)-32-LENGTH(t1.relname)) AS sys.sysname)
END AS INDEX_NAME,
CASE
WHEN t5.indisclustered = 't' THEN CAST(1 AS smallint)
ELSE CAST(3 AS smallint)
END AS TYPE,
CAST(seq + 1 AS smallint) AS SEQ_IN_INDEX,
CAST(t4."COLUMN_NAME" AS sys.sysname) AS COLUMN_NAME,
CAST('A' AS sys.varchar(1)) AS COLLATION,
CAST(t7.n_distinct AS int) AS CARDINALITY,
CAST(0 AS int) AS PAGES, --not supported
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 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)
LEFT JOIN pg_catalog.pg_stats t7 ON (t1.relname = t7.tablename AND t7.schemaname = nsp.nspname)
LEFT JOIN pg_catalog.pg_constraint t8 ON t5.indexrelid = t8.conindid
, generate_series(0,31) seq -- SQL server has max 32 columns per index
WHERE CAST(t4."ORDINAL_POSITION" AS smallint) = ANY (t5.indkey)
AND CAST(t4."ORDINAL_POSITION" AS smallint) = t5.indkey[seq];
GRANT SELECT on sys.sp_statistics_view TO PUBLIC;

-- This is a temporary procedure which is called during upgrade to update guest schema
-- for the guest users in the already existing databases
Expand Down

0 comments on commit 3493bd9

Please sign in to comment.