Skip to content

Commit 22e8be2

Browse files
author
anju15bharti
committed
fix more failures
1 parent 4418f9e commit 22e8be2

75 files changed

Lines changed: 140 additions & 387 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
#include "parser/parse_type.h"
3333
#include "parser/parsetree.h"
3434
#include "utils/fmgroids.h"
35-
#include "utils/builtins.h"
36-
#include "rewrite/rewriteHandler.h"
37-
#include "utils/lsyscache.h"
3835
#include "utils/lsyscache.h"
3936
#include "utils/syscache.h"
4037
#include "utils/memdebug.h"
@@ -1237,6 +1234,7 @@ PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *target
12371234
*/
12381235
SetParamMetadataCommonInfo(col, finfo);
12391236
initStringInfo(&(col->colName));
1237+
appendStringInfoString(&col->colName, NameStr(att->attname));
12401238

12411239
/* Do we have a non-resjunk tlist item? */
12421240
while (tlist_item &&
@@ -1246,91 +1244,13 @@ PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *target
12461244
{
12471245
tle = (TargetEntry *) lfirst(tlist_item);
12481246

1249-
/*
1250-
* Use tle->resname if available and longer than attname,
1251-
* since attname is limited to NAMEDATALEN-1 (63 chars).
1252-
*/
1253-
if (tle->resname && strlen(tle->resname) > strlen(NameStr(att->attname))
1254-
&& pg_verifymbstr(tle->resname, strlen(tle->resname), true))
1255-
appendStringInfoString(&col->colName, tle->resname);
1256-
else if (tle->resname && strlen(tle->resname) > strlen(NameStr(att->attname)))
1257-
{
1258-
elog(WARNING, "invalid UTF8 in tle->resname for column %d, falling back to attname", attno + 1);
1259-
appendStringInfoString(&col->colName, NameStr(att->attname));
1260-
}
1261-
else
1262-
{
1263-
appendStringInfoString(&col->colName, NameStr(att->attname));
1264-
1265-
/* For SELECT *, try attoptions for full original name */
1266-
if (col->colName.len >= NAMEDATALEN - 1 && tle->resorigtbl != InvalidOid && tle->resorigcol > 0)
1267-
{
1268-
HeapTuple atttup = SearchSysCache2(ATTNUM,
1269-
ObjectIdGetDatum(tle->resorigtbl),
1270-
Int16GetDatum(tle->resorigcol));
1271-
if (HeapTupleIsValid(atttup))
1272-
{
1273-
bool isnull;
1274-
Datum opts = SysCacheGetAttr(ATTNUM, atttup, Anum_pg_attribute_attoptions, &isnull);
1275-
if (!isnull)
1276-
{
1277-
ArrayType *arr = DatumGetArrayTypeP(opts);
1278-
Datum *elems;
1279-
int n;
1280-
deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT, &elems, NULL, &n);
1281-
for (int i = 0; i < n; i++)
1282-
{
1283-
char *s = text_to_cstring(DatumGetTextP(elems[i]));
1284-
if (strncmp(s, "bbf_original_name=", 18) == 0)
1285-
{
1286-
resetStringInfo(&col->colName);
1287-
appendStringInfoString(&col->colName, s + 18);
1288-
break;
1289-
}
1290-
}
1291-
}
1292-
ReleaseSysCache(atttup);
1293-
}
1294-
}
1295-
1296-
/* Fallback: for views, lookup pg_rewrite for full column name */
1297-
if (col->colName.len >= NAMEDATALEN - 1 && tle->resorigtbl != InvalidOid)
1298-
{
1299-
char relkind = get_rel_relkind(tle->resorigtbl);
1300-
if (relkind == RELKIND_VIEW)
1301-
{
1302-
Relation vrel = relation_open(tle->resorigtbl, AccessShareLock);
1303-
Query *vquery = get_view_query(vrel);
1304-
ListCell *vlc;
1305-
int vcolno = 0;
1306-
1307-
foreach(vlc, vquery->targetList)
1308-
{
1309-
TargetEntry *vtle = (TargetEntry *) lfirst(vlc);
1310-
if (vtle->resjunk)
1311-
continue;
1312-
vcolno++;
1313-
if (vcolno == tle->resorigcol && vtle->resname &&
1314-
strlen(vtle->resname) > (Size) col->colName.len)
1315-
{
1316-
resetStringInfo(&col->colName);
1317-
appendStringInfoString(&col->colName, vtle->resname);
1318-
break;
1319-
}
1320-
}
1321-
relation_close(vrel, AccessShareLock);
1322-
}
1323-
}
1324-
}
1325-
13261247
col->relOid = tle->resorigtbl;
13271248
col->attrNum = tle->resorigcol;
13281249

13291250
tlist_item = lnext(targetlist, tlist_item);
13301251
}
13311252
else
13321253
{
1333-
appendStringInfoString(&col->colName, NameStr(att->attname));
13341254
/* No info available, so send zeroes */
13351255
col->relOid = 0;
13361256
col->attrNum = 0;

contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--6.1.0--6.2.0.sql

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,21 +1330,25 @@ language plpgsql STABLE;
13301330
-- Note: full view recreation needed - copy from sys_views.sql
13311331
-- Drop deprecated views
13321332

1333-
-- Drop per-database wrapper views that depend on deprecated sysdatabases
1333+
-- Drop wrapper views before dropping deprecated
13341334
DO $$
1335-
DECLARE
1336-
db_name TEXT;
1337-
schema_name TEXT;
1335+
DECLARE r RECORD;
13381336
BEGIN
1339-
FOR db_name IN SELECT name FROM sys.babelfish_sysdatabases
1340-
LOOP
1341-
schema_name := db_name || '_dbo';
1342-
IF EXISTS (SELECT 1 FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid WHERE c.relname = 'sysdatabases' AND n.nspname = schema_name) THEN
1343-
EXECUTE format('DROP VIEW IF EXISTS %I.sysdatabases', schema_name);
1344-
END IF;
1337+
FOR r IN SELECT name FROM sys.babelfish_sysdatabases LOOP
1338+
EXECUTE format('DROP VIEW IF EXISTS %I.sysdatabases', r.name || '_dbo');
13451339
END LOOP;
13461340
END $$;
13471341
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysdatabases_deprecated_in_6_2_0');
1342+
-- Recreate wrapper views
1343+
DO $$
1344+
DECLARE r RECORD;
1345+
BEGIN
1346+
FOR r IN SELECT name FROM sys.babelfish_sysdatabases LOOP
1347+
EXECUTE format('CREATE VIEW %I.sysdatabases AS SELECT * FROM sys.sysdatabases', r.name || '_dbo');
1348+
EXECUTE format('ALTER TABLE %I.sysdatabases OWNER TO %I', r.name || '_dbo', r.name || '_dbo');
1349+
END LOOP;
1350+
END $$;
1351+
13481352
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'pg_namespace_ext_deprecated_in_6_2_0');
13491353
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sp_tables_view_deprecated_in_6_2_0');
13501354
CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sp_special_columns_view_deprecated_in_6_2_0');
@@ -1462,6 +1466,44 @@ CREATE OR REPLACE VIEW information_schema_tsql.columns_internal AS
14621466
AND ext.dbid = sys.db_id();
14631467

14641468
GRANT SELECT ON information_schema_tsql.columns_internal TO PUBLIC;
1469+
CREATE OR REPLACE VIEW information_schema_tsql.tables AS
1470+
WITH tt_internal AS MATERIALIZED (
1471+
SELECT * FROM sys.table_types_internal
1472+
)
1473+
SELECT CAST(nc.dbname AS sys.nvarchar(128)) AS "TABLE_CATALOG",
1474+
CAST(ext.orig_name AS sys.nvarchar(128)) AS "TABLE_SCHEMA",
1475+
CAST(
1476+
COALESCE(
1477+
(SELECT PG_CATALOG.string_agg(
1478+
CASE
1479+
WHEN option LIKE 'bbf_original_rel_name=%' THEN substring(option, 23)
1480+
ELSE NULL
1481+
END, ',')
1482+
FROM unnest(c.reloptions) AS option),
1483+
c.relname)
1484+
AS sys._ci_sysname) AS "TABLE_NAME",
1485+
1486+
CAST(
1487+
CASE WHEN c.relkind IN ('r', 'p') THEN 'BASE TABLE'
1488+
WHEN c.relkind = 'v' THEN 'VIEW'
1489+
ELSE null END
1490+
AS sys.varchar(10)) COLLATE sys.database_default AS "TABLE_TYPE"
1491+
1492+
FROM sys.pg_namespace_ext nc JOIN pg_class c ON (nc.oid = c.relnamespace)
1493+
LEFT OUTER JOIN sys.babelfish_namespace_ext ext on nc.nspname = ext.nspname
1494+
LEFT JOIN tt_internal tt ON c.oid = tt.typrelid
1495+
1496+
WHERE c.relkind IN ('r', 'v', 'p')
1497+
AND c.relispartition = false
1498+
AND (NOT pg_is_other_temp_schema(nc.oid))
1499+
AND tt.typrelid IS NULL
1500+
AND (pg_has_role(c.relowner, 'USAGE')
1501+
OR has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')
1502+
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES') )
1503+
AND ext.dbid = sys.db_id()
1504+
AND (NOT c.relname = 'sysdatabases');
1505+
1506+
GRANT SELECT ON information_schema_tsql.tables TO PUBLIC;
14651507

14661508

14671509
-- 5o. Recreate constraint and object views to resolve original names from babelfish_identifier_mapping
@@ -3226,39 +3268,6 @@ WHERE a.atthasdef = 't' and a.attgenerated = ''
32263268
AND has_column_privilege(a.attrelid, a.attname, 'SELECT,INSERT,UPDATE,REFERENCES');
32273269
GRANT SELECT ON sys.default_constraints TO PUBLIC;
32283270

3229-
create or replace view sys.shipped_objects_not_in_sys AS
3230-
-- This portion of view retrieves information on objects that reside in a schema in one specfic database.
3231-
-- For example, 'master_dbo' schema can only exist in the 'master' database.
3232-
-- Internally stored schema name (nspname) must be provided.
3233-
select t.name,t.type, ns.oid as schemaid from
3234-
(
3235-
values
3236-
('xp_qv','master_dbo','P'),
3237-
('xp_instance_regread','master_dbo','P'),
3238-
('sp_addlinkedserver', 'master_dbo', 'P'),
3239-
('sp_addlinkedsrvlogin', 'master_dbo', 'P'),
3240-
('sp_dropserver', 'master_dbo', 'P'),
3241-
('sp_droplinkedsrvlogin', 'master_dbo', 'P'),
3242-
('sp_testlinkedserver', 'master_dbo', 'P'),
3243-
('sp_enum_oledb_providers','master_dbo','P'),
3244-
('fn_syspolicy_is_automation_enabled', 'msdb_dbo', 'FN'),
3245-
('syspolicy_configuration', 'msdb_dbo', 'V'),
3246-
('syspolicy_system_health_state', 'msdb_dbo', 'V')
3247-
) t(name,schema_name, type)
3248-
inner join pg_catalog.pg_namespace ns on t.schema_name = ns.nspname
3249-
3250-
union all
3251-
3252-
-- This portion of view retrieves information on objects that reside in a schema in any number of databases.
3253-
-- For example, 'dbo' schema can exist in the 'master', 'tempdb', 'msdb', and any user created database.
3254-
select t.name,t.type, ns.oid as schemaid from
3255-
(
3256-
values
3257-
('sysdatabases','dbo','V')
3258-
) t (name, schema_name, type)
3259-
inner join sys.babelfish_namespace_ext b on t.schema_name = b.orig_name
3260-
inner join pg_catalog.pg_namespace ns on b.nspname = ns.nspname;
3261-
GRANT SELECT ON sys.shipped_objects_not_in_sys TO PUBLIC;
32623271

32633272
create or replace view sys.procedures as
32643273
select

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,6 +4312,10 @@ is_antlr_parse_cache_enabled_for_routine(HeapTuple proctup, HeapTuple bbftup)
43124312
if (!HeapTupleIsValid(bbftup))
43134313
return pltsql_enable_antlr_parse_cache;
43144314

4315+
/* Guard against old tuples that don't have this column yet (e.g. after pg_upgrade) */
4316+
if (HeapTupleHeaderGetNatts(bbftup->t_data) < Anum_bbf_function_ext_antlr_parse_cache_enabled)
4317+
return pltsql_enable_antlr_parse_cache;
4318+
43154319
cache_flag = SysCacheGetAttr(PROCNAMENSPSIGNATURE, bbftup,
43164320
Anum_bbf_function_ext_antlr_parse_cache_enabled,
43174321
&isnull);

contrib/babelfishpg_tsql/src/pl_handler.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8726,34 +8726,18 @@ void pltsql_bbfSelectIntoUtility(ParseState *pstate, PlannedStmt *pstmt, const c
87268726
*/
87278727
Query *query = castNode(Query, ((CreateTableAsStmt *)parsetree)->query);
87288728
ListCell *tlc;
8729-
List *saved_resnames = NIL;
87308729

8731-
/* Save original resnames and truncate for table creation */
87328730
foreach(tlc, query->targetList)
87338731
{
87348732
TargetEntry *tle = (TargetEntry *) lfirst(tlc);
8735-
saved_resnames = lappend(saved_resnames, tle->resname);
87368733
if (tle->resname && strlen(tle->resname) >= NAMEDATALEN)
8737-
tle->resname = downcase_truncate_identifier(tle->resname, strlen(tle->resname), false);
8738-
}
8739-
8740-
PG_TRY();
8741-
{
8742-
*address = ExecCreateTableAs(pstate, (CreateTableAsStmt *)parsetree, params, queryEnv, qc);
8743-
}
8744-
PG_CATCH();
8745-
{
8746-
/* Restore original resnames on failure */
8747-
ListCell *slc = list_head(saved_resnames);
8748-
foreach(tlc, query->targetList)
87498734
{
8750-
TargetEntry *tle = (TargetEntry *) lfirst(tlc);
8751-
tle->resname = (char *) lfirst(slc);
8752-
slc = lnext(saved_resnames, slc);
8735+
tle->resname = pstrdup(tle->resname);
8736+
truncate_identifier(tle->resname, strlen(tle->resname), false);
87538737
}
8754-
PG_RE_THROW();
87558738
}
8756-
PG_END_TRY();
8739+
8740+
*address = ExecCreateTableAs(pstate, (CreateTableAsStmt *)parsetree, params, queryEnv, qc);
87578741
}
87588742
else
87598743
{

0 commit comments

Comments
 (0)