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

Fix OBJECT_ID() to exclude indexes #3495

Open
wants to merge 8 commits into
base: BABEL_5_X_DEV
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2361,13 +2361,39 @@ object_id(PG_FUNCTION_ARGS)
}
else if (enr == NULL)
{
result = get_relname_relid((const char *) object_name, LookupNamespaceNoError("pg_temp"));
Oid temp_nsp_oid = LookupNamespaceNoError("pg_temp");
if (OidIsValid(temp_nsp_oid))
{
result = get_relname_relid((const char *) object_name, temp_nsp_oid);
}
}
}
else
{
/* search in pg_class by name and schema oid */
Oid relid = get_relname_relid((const char *) object_name, schema_oid);
HeapTuple class_tuple;
Oid relid = InvalidOid;

/* Perform a single lookup in pg_class by name and schema oid */
class_tuple = SearchSysCache2(RELNAMENSP, CStringGetDatum(object_name), ObjectIdGetDatum(schema_oid));

if (HeapTupleIsValid(class_tuple))
{
Form_pg_class classform = (Form_pg_class) GETSTRUCT(class_tuple);

/* Check if the relation is not an index */
if (classform->relkind != RELKIND_INDEX)
{
relid = classform->oid;

/* Perform ACL check */
if (pg_class_aclcheck(relid, user_id, ACL_SELECT) == ACLCHECK_OK)
{
result = relid;
}
}

ReleaseSysCache(class_tuple);
}

if (OidIsValid(relid) && pg_class_aclcheck(relid, user_id, ACL_SELECT) == ACLCHECK_OK)
{
Expand Down
26 changes: 26 additions & 0 deletions test/JDBC/expected/object_id_index_conflict-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Index cleanup
DROP INDEX IF EXISTS dbo.object_id_conflict_t.object_id_idx_name;
GO

DROP INDEX IF EXISTS dbo.object_id_conflict_parent_t.object_id_idx_parent_id;
GO

-- Constraint cleanup
ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS pk_id;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS fk_constraint;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS chk_age;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS unq_name;
GO

-- Table cleanup
DROP TABLE IF EXISTS object_id_conflict_parent_t;
GO

DROP TABLE IF EXISTS object_id_conflict_t;
GO
35 changes: 35 additions & 0 deletions test/JDBC/expected/object_id_index_conflict-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE TABLE object_id_conflict_t (
id INT,
name VARCHAR(100),
age INT,
parent_id INT
);
GO

CREATE TABLE object_id_conflict_parent_t (
id INT PRIMARY KEY
);
GO

-- Case 1: Primary Key constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT pk_id PRIMARY KEY (id);
GO

-- Case 2: Foreign key constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT fk_constraint FOREIGN KEY (parent_id) REFERENCES object_id_conflict_parent_t(id);
GO

-- Case 3: Check constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT chk_age CHECK (age >= 0);
GO

-- Case 4: Unique constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT unq_name UNIQUE (name);
GO

-- Case 5: Different kinds of index
CREATE INDEX object_id_idx_name ON object_id_conflict_t (name);
GO

CREATE INDEX object_id_idx_parent_id ON object_id_conflict_t (parent_id);
GO
69 changes: 69 additions & 0 deletions test/JDBC/expected/object_id_index_conflict-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
SELECT
CASE
WHEN OBJECT_ID('pk_id') = OBJECT_ID('pk_id', 'pk')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO
~~START~~
varchar
Match
~~END~~


SELECT
CASE
WHEN OBJECT_ID('fk_constraint') = OBJECT_ID('fk_constraint', 'f')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO
~~START~~
varchar
Match
~~END~~


SELECT
CASE
WHEN OBJECT_ID('chk_age') = OBJECT_ID('chk_age', 'c')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO
~~START~~
varchar
Match
~~END~~


SELECT
CASE
WHEN OBJECT_ID('unq_name') = OBJECT_ID('unq_name', 'uq')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO
~~START~~
varchar
Match
~~END~~


-- Initially when we used to query using full index name, we used to get the corresponding OID from pg_class which is incorrect
-- this should now return NULL
SELECT OBJECT_ID('object_id_idx_name');
GO
~~START~~
int
<NULL>
~~END~~


SELECT OBJECT_ID('object_id_idx_parent_id');
GO
~~START~~
int
<NULL>
~~END~~

26 changes: 26 additions & 0 deletions test/JDBC/input/object_id_index_conflict-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Index cleanup
DROP INDEX IF EXISTS dbo.object_id_conflict_t.object_id_idx_name;
GO

DROP INDEX IF EXISTS dbo.object_id_conflict_parent_t.object_id_idx_parent_id;
GO

-- Constraint cleanup
ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS pk_id;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS fk_constraint;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS chk_age;
GO

ALTER TABLE object_id_conflict_t DROP CONSTRAINT IF EXISTS unq_name;
GO

-- Table cleanup
DROP TABLE IF EXISTS object_id_conflict_parent_t;
GO

DROP TABLE IF EXISTS object_id_conflict_t;
GO
35 changes: 35 additions & 0 deletions test/JDBC/input/object_id_index_conflict-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE TABLE object_id_conflict_t (
id INT,
name VARCHAR(100),
age INT,
parent_id INT
);
GO

CREATE TABLE object_id_conflict_parent_t (
id INT PRIMARY KEY
);
GO

-- Case 1: Primary Key constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT pk_id PRIMARY KEY (id);
GO

-- Case 2: Foreign key constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT fk_constraint FOREIGN KEY (parent_id) REFERENCES object_id_conflict_parent_t(id);
GO

-- Case 3: Check constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT chk_age CHECK (age >= 0);
GO

-- Case 4: Unique constraint
ALTER TABLE object_id_conflict_t ADD CONSTRAINT unq_name UNIQUE (name);
GO

-- Case 5: Different kinds of index
CREATE INDEX object_id_idx_name ON object_id_conflict_t (name);
GO

CREATE INDEX object_id_idx_parent_id ON object_id_conflict_t (parent_id);
GO
39 changes: 39 additions & 0 deletions test/JDBC/input/object_id_index_conflict-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
SELECT
CASE
WHEN OBJECT_ID('pk_id') = OBJECT_ID('pk_id', 'pk')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO

SELECT
CASE
WHEN OBJECT_ID('fk_constraint') = OBJECT_ID('fk_constraint', 'f')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO

SELECT
CASE
WHEN OBJECT_ID('chk_age') = OBJECT_ID('chk_age', 'c')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO

SELECT
CASE
WHEN OBJECT_ID('unq_name') = OBJECT_ID('unq_name', 'uq')
THEN 'Match'
ELSE 'No Match'
END AS Result;
GO

-- Initially when we used to query using full index name, we used to get the corresponding OID from pg_class which is incorrect
-- this should now return NULL
SELECT OBJECT_ID('object_id_idx_name');
GO

SELECT OBJECT_ID('object_id_idx_parent_id');
GO
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ newid
objectpropertyex
openjson
orderby
object_id_index_conflict
routines_definition
rowcount
schema_resolution_func
Expand Down
Loading