Skip to content

Commit 46ff0e3

Browse files
author
Rahul Parande
committed
Passing subid of relation to handle_bbf_view_binding_on_object_drop
instead of 0 Signed-off-by: Rahul Parande <rparande@amazon.com>
1 parent fb02234 commit 46ff0e3

File tree

9 files changed

+106
-66
lines changed

9 files changed

+106
-66
lines changed

contrib/babelfishpg_tsql/runtime/functions.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,25 +4481,13 @@ objectproperty_internal(PG_FUNCTION_ARGS)
44814481
*/
44824482
if (pg_strcasecmp(property, "isschemabound") == 0)
44834483
{
4484+
bool is_weak_view = false;
4485+
44844486
if (type == OBJECT_TYPE_VIEW)
4485-
{
4486-
bool is_weak_view = false;
4487-
bool is_tsql_view = check_is_tsql_view(object_id, &is_weak_view);
4488-
4489-
if (is_tsql_view)
4490-
{
4491-
/* Views are schema bound (return 1) */
4492-
pfree(property);
4493-
PG_RETURN_INT32(is_weak_view ? 0 : 1);
4494-
}
4495-
else
4496-
{
4497-
pfree(property);
4498-
PG_RETURN_INT32(0);
4499-
}
4500-
}
4487+
check_is_tsql_view(object_id, &is_weak_view);
4488+
45014489
pfree(property);
4502-
PG_RETURN_INT32(0);
4490+
PG_RETURN_INT32(((int) !is_weak_view));
45034491
}
45044492
/*
45054493
* For ExecIsQuotedIdentOn and ExecIsAnsiNullsOn, we hardcoded it to 1

contrib/babelfishpg_tsql/src/catalog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ search_bbf_view_def(Relation bbf_view_def_rel, Oid viewOid)
12131213
int16 dbid = InvalidDbid;
12141214

12151215
if (!OidIsValid(viewOid))
1216-
return NULL;
1216+
return NULL;
12171217

12181218
schema_id = get_rel_namespace(viewOid);
12191219
schema_name = get_namespace_name(schema_id);

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ static Query *create_dummy_view_query_for_broken_view(Oid viewOid);
285285
static bool repair_broken_view_recursive(Oid viewOid, List *visitedViews);
286286
static bool is_dummy_view(Oid viewOid);
287287
static bool update_bbf_view_flags(Oid viewOid, uint64 flags_to_set, uint64 flags_to_clear, bool update_validity);
288-
// static HeapTuple get_bbf_view_def_tuple(Oid viewOid);
289288
static Oid get_view_oid_from_rule(Oid ruleOid);
290289

291290
/* Save hook values in case of unload */
@@ -3173,7 +3172,7 @@ bbf_object_access_hook(ObjectAccessType access, Oid classId, Oid objectId, int s
31733172

31743173
obj.classId = RelationRelationId;
31753174
obj.objectId = objectId;
3176-
obj.objectSubId = 0;
3175+
obj.objectSubId = subId;
31773176

31783177
/* Call view dependency handling function */
31793178
handle_bbf_view_binding_on_object_drop(&obj, NULL, NULL);
@@ -6949,7 +6948,7 @@ bbf_view_set_broken(Oid viewOid, bool mark_broken)
69496948
(flag_validity & BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW) != 0;
69506949

69516950
/* Cannot mark a strong-bound view as broken */
6952-
if (mark_broken && !is_weak_view && !pltsql_weak_view_binding)
6951+
if (mark_broken && !is_weak_view)
69536952
{
69546953
ereport(ERROR,
69556954
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
@@ -7014,8 +7013,7 @@ is_dummy_view(Oid viewOid)
70147013
foreach(lc, viewQuery->targetList)
70157014
{
70167015
TargetEntry *te = (TargetEntry *) lfirst(lc);
7017-
Const *con = (Const *) te->expr;
7018-
if (!te->resjunk && !IsA(te->expr, Const) && !con->constisnull)
7016+
if (te->resjunk || !IsA(te->expr, Const) || !((Const *) (te->expr))->constisnull)
70197017
{
70207018
is_dummy = false;
70217019
break;
@@ -7217,7 +7215,7 @@ repair_broken_views(Query *parsetree)
72177215
List *view_oids = NIL;
72187216
bool repaired = false;
72197217

7220-
if (sql_dialect != SQL_DIALECT_TSQL || !IS_TDS_CONN() || parsetree->rtable == NIL)
7218+
if (sql_dialect != SQL_DIALECT_TSQL || !IS_TDS_CONN())
72217219
return false;
72227220

72237221
/* Find all views referenced in the query, including those in subqueries and sublinks */
@@ -7333,12 +7331,15 @@ create_dummy_view_query_for_broken_view(Oid viewOid)
73337331
bool
73347332
handle_bbf_view_binding_on_object_drop(const ObjectAddress *droppedObject, Relation depRel, ViewStmt *stmt)
73357333
{
7336-
ScanKeyData key[2];
7334+
ScanKeyData key[3];
73377335
SysScanDesc scan;
73387336
HeapTuple tup;
73397337
List *processed_views = NIL;
7338+
Oid viewOid = InvalidOid;
73407339
bool is_alter_view = false;
73417340
bool is_weak_view = false;
7341+
bool processed = true;
7342+
int nkeys = 2;
73427343

73437344
/* Check if this is an ALTER VIEW operation */
73447345
is_alter_view = (stmt != NULL && stmt->createOrAlter);
@@ -7356,46 +7357,42 @@ handle_bbf_view_binding_on_object_drop(const ObjectAddress *droppedObject, Relat
73567357
Anum_pg_depend_refobjid,
73577358
BTEqualStrategyNumber, F_OIDEQ,
73587359
ObjectIdGetDatum(droppedObject->objectId));
7360+
if (droppedObject->objectSubId)
7361+
{
7362+
ScanKeyInit(&key[2],
7363+
Anum_pg_depend_refobjsubid,
7364+
BTEqualStrategyNumber, F_INT4EQ,
7365+
Int32GetDatum(droppedObject->objectSubId));
7366+
nkeys = 3;
7367+
}
73597368

7369+
// scan = systable_beginscan(depRel, DependReferenceIndexId, true,
7370+
// NULL, 2, key);
73607371
scan = systable_beginscan(depRel, DependReferenceIndexId, true,
7361-
NULL, 2, key);
7372+
NULL, nkeys, key);
73627373

73637374
/* Loop over all the direct dependents */
73647375
while ((tup = systable_getnext(scan)) != NULL)
73657376
{
73667377
Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
73677378

73687379
/* Only handle entries for pg_rewrite rules with NORMAL dependency */
7369-
if (depform->classid == RewriteRelationId &&
7370-
depform->deptype == DEPENDENCY_NORMAL)
7380+
if (depform->classid != RewriteRelationId || depform->deptype != DEPENDENCY_NORMAL)
7381+
continue;
7382+
7383+
viewOid = get_view_oid_from_rule(depform->objid);
7384+
7385+
if (OidIsValid(viewOid) && !list_member_oid(processed_views, viewOid))
73717386
{
7372-
Oid viewOid = get_view_oid_from_rule(depform->objid);
7373-
7374-
if (OidIsValid(viewOid) && !list_member_oid(processed_views, viewOid))
7387+
processed_views = lappend_oid(processed_views, viewOid);
7388+
if (!is_alter_view)
73757389
{
7376-
processed_views = lappend_oid(processed_views, viewOid);
7377-
if (!is_alter_view)
7390+
/* DROP operation: if dependent is weak view, mark broken */
7391+
is_weak_view = false;
7392+
if (check_is_tsql_view(viewOid, &is_weak_view) && is_weak_view)
73787393
{
7379-
/* DROP operation: if dependent is weak view, mark broken */
7380-
is_weak_view = false;
7381-
if (check_is_tsql_view(viewOid, &is_weak_view) && is_weak_view)
7382-
{
7383-
/* Mark view as broken */
7384-
bool updated = bbf_view_set_broken(viewOid, true);
7385-
7386-
/* If the view was successfully marked as broken, create a dummy query */
7387-
if (updated)
7388-
{
7389-
Query *dummyQuery = create_dummy_view_query_for_broken_view(viewOid);
7390-
StoreViewQuery(viewOid, dummyQuery, true);
7391-
}
7392-
CommandCounterIncrement();
7393-
}
7394-
}
7395-
else
7396-
{
7397-
/* ALTER operation: mark weak & broken */
7398-
bool updated = bbf_view_set_broken(viewOid, true);
7394+
/* Mark view as broken */
7395+
bool updated = bbf_view_set_broken(viewOid, true);
73997396

74007397
/* If the view was successfully marked as broken, create a dummy query */
74017398
if (updated)
@@ -7404,16 +7401,35 @@ handle_bbf_view_binding_on_object_drop(const ObjectAddress *droppedObject, Relat
74047401
StoreViewQuery(viewOid, dummyQuery, true);
74057402
}
74067403
CommandCounterIncrement();
7407-
update_bbf_view_flags(viewOid, BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW, 0, true);
74087404
}
74097405
}
7406+
else if (is_alter_view && pltsql_weak_view_binding)
7407+
{
7408+
/* ALTER operation: mark weak & broken */
7409+
update_bbf_view_flags(viewOid, BBF_VIEW_DEF_FLAG_IS_WEAK_VIEW, 0, true);
7410+
CommandCounterIncrement();
7411+
7412+
bool updated = bbf_view_set_broken(viewOid, true);
7413+
7414+
/* If the view was successfully marked as broken, create a dummy query */
7415+
if (updated)
7416+
{
7417+
Query *dummyQuery = create_dummy_view_query_for_broken_view(viewOid);
7418+
StoreViewQuery(viewOid, dummyQuery, true);
7419+
}
7420+
}
7421+
else
7422+
{
7423+
processed = false;
7424+
break;
7425+
}
74107426
}
74117427
}
74127428
systable_endscan(scan);
74137429
table_close(depRel, AccessShareLock);
74147430
list_free(processed_views);
74157431
CommandCounterIncrement();
7416-
return true;
7432+
return processed;
74177433
}
74187434

74197435
/*

contrib/babelfishpg_tsql/src/pl_handler.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,6 @@ pltsql_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
11401140
* This check will happen when calling parse_analyze_fixed_params() present in DefineView()
11411141
*/
11421142
check_view_binding_dependencies(query);
1143-
pltsql_current_query_is_view_definition = false;
11441143
}
11451144
}
11461145
else if (query->commandType == CMD_UTILITY)
@@ -2475,7 +2474,7 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
24752474
ViewStmt *vstmt = (ViewStmt *) parsetree;
24762475
Oid relid = RangeVarGetRelid(vstmt->view, NoLock, true);
24772476

2478-
if (vstmt->replace && check_is_tsql_view(relid, false))
2477+
if (vstmt->replace && check_is_tsql_view(relid, NULL))
24792478
{
24802479
ereport(ERROR,
24812480
(errcode(ERRCODE_INTERNAL_ERROR),

test/JDBC/expected/alter-view-vu-verify.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ ALTER VIEW alter_v24 AS SELECT a,b FROM alter_t;
399399
GO
400400
~~ERROR (Code: 33557097)~~
401401

402-
~~ERROR (Message: cannot mark schema-bound view as broken)~~
402+
~~ERROR (Message: cannot alter view because other objects depend on it)~~
403403

404404

405405
SELECT * FROM alter_v24;

test/JDBC/expected/weak-binding-vu-cleanup.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ GO
162162
DROP VIEW IF EXISTS complex_nested_view;
163163
GO
164164

165+
DROP VIEW IF EXISTS no_top_level;
166+
GO
167+
165168
USE master;
166169
GO
167170

test/JDBC/expected/weak-binding-vu-verify.out

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ CREATE TABLE dml_test_table (
14921492
GO
14931493

14941494
CREATE VIEW dml_test_view AS
1495-
SELECT id, name FROM dml_test_table;
1495+
SELECT id, name FROM dml_test_table;
14961496
GO
14971497

14981498
INSERT INTO dml_test_view (id, name) VALUES (1, 'Test1'), (2, 'Test2');
@@ -1536,9 +1536,16 @@ int#!#varchar
15361536
~~END~~
15371537

15381538

1539+
-- View with top-level query having an empty rtable
1540+
CREATE VIEW no_top_level AS
1541+
SELECT
1542+
(SELECT MAX(id) FROM dml_test_view) AS max_id,
1543+
(SELECT MIN(id) FROM dml_test_view) AS min_id;
1544+
GO
1545+
15391546

15401547
---------------------------------------------------------------
1541-
-- Views referenced inside CTE statements
1548+
-- Broken Views referenced inside CTE statements
15421549
---------------------------------------------------------------
15431550
WITH cte AS (
15441551
SELECT * FROM dml_test_view
@@ -1600,9 +1607,18 @@ int#!#varchar
16001607
~~END~~
16011608

16021609

1610+
-- View with top-level query having an empty rtable and broken view
1611+
SELECT * FROM no_top_level;
1612+
GO
1613+
~~START~~
1614+
int#!#int
1615+
2#!#1
1616+
~~END~~
1617+
1618+
16031619

16041620
---------------------------------------------------------------
1605-
-- Views referenced inside subqueries
1621+
-- Broken Views referenced inside subqueries
16061622
---------------------------------------------------------------
16071623
SELECT * FROM (
16081624
SELECT * FROM dml_test_view
@@ -1662,7 +1678,9 @@ int#!#varchar
16621678

16631679

16641680

1665-
-- Views in subqueries inside where clause
1681+
---------------------------------------------------------------
1682+
-- Broken Views in subqueries inside where clause
1683+
---------------------------------------------------------------
16661684
SELECT * FROM dml_test_view
16671685
WHERE id IN (
16681686
SELECT id FROM dml_test_view WHERE name LIKE 'Test%'

test/JDBC/input/views/weak-binding-vu-cleanup.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ GO
162162
DROP VIEW IF EXISTS complex_nested_view;
163163
GO
164164

165+
DROP VIEW IF EXISTS no_top_level;
166+
GO
167+
165168
USE master;
166169
GO
167170

test/JDBC/input/views/weak-binding-vu-verify.sql

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ CREATE TABLE dml_test_table (
10411041
GO
10421042

10431043
CREATE VIEW dml_test_view AS
1044-
SELECT id, name FROM dml_test_table;
1044+
SELECT id, name FROM dml_test_table;
10451045
GO
10461046

10471047
INSERT INTO dml_test_view (id, name) VALUES (1, 'Test1'), (2, 'Test2');
@@ -1062,8 +1062,15 @@ GO
10621062
SELECT * FROM dml_test_view;
10631063
GO
10641064

1065+
-- View with top-level query having an empty rtable
1066+
CREATE VIEW no_top_level AS
1067+
SELECT
1068+
(SELECT MAX(id) FROM dml_test_view) AS max_id,
1069+
(SELECT MIN(id) FROM dml_test_view) AS min_id;
1070+
GO
1071+
10651072
---------------------------------------------------------------
1066-
-- Views referenced inside CTE statements
1073+
-- Broken Views referenced inside CTE statements
10671074
---------------------------------------------------------------
10681075

10691076
WITH cte AS (
@@ -1103,8 +1110,12 @@ WITH cte AS (
11031110
SELECT * FROM cte WHERE id = 1;
11041111
GO
11051112

1113+
-- View with top-level query having an empty rtable and broken view
1114+
SELECT * FROM no_top_level;
1115+
GO
1116+
11061117
---------------------------------------------------------------
1107-
-- Views referenced inside subqueries
1118+
-- Broken Views referenced inside subqueries
11081119
---------------------------------------------------------------
11091120

11101121
SELECT * FROM (
@@ -1140,7 +1151,9 @@ SELECT * FROM (
11401151
) AS subquery WHERE id = 1;
11411152
GO
11421153

1143-
-- Views in subqueries inside where clause
1154+
---------------------------------------------------------------
1155+
-- Broken Views in subqueries inside where clause
1156+
---------------------------------------------------------------
11441157

11451158
SELECT * FROM dml_test_view
11461159
WHERE id IN (

0 commit comments

Comments
 (0)