Skip to content

Commit

Permalink
Support non-ENR temp tables in OBJECT_ID and OBJECT_NAME (#2292)
Browse files Browse the repository at this point in the history
Adding the ability for the OBJECT_ID and OBJECT_NAME functions to see Non-ENR temp tables. Temp tables not in ENR will still be visible in pg catalogs, and so the OBJECT functions should be able to return information about then.

Task: BABEL-4498
  • Loading branch information
timchang514 authored Jan 25, 2024
1 parent 2af117f commit 9f67648
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
12 changes: 11 additions & 1 deletion contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,10 @@ object_id(PG_FUNCTION_ARGS)
{
result = enr->md.reliddesc;
}
else if (enr == NULL)
{
result = get_relname_relid((const char *) object_name, LookupNamespaceNoError("pg_temp"));
}
}
else if (!strcmp(object_type, "r") || !strcmp(object_type, "ec") || !strcmp(object_type, "pg") ||
!strcmp(object_type, "sn") || !strcmp(object_type, "sq") || !strcmp(object_type, "tt"))
Expand Down Expand Up @@ -2156,6 +2160,10 @@ object_id(PG_FUNCTION_ARGS)
if (enr != NULL && enr->md.enrtype == ENR_TSQL_TEMP)
{
result = enr->md.reliddesc;
}
else if (enr == NULL)
{
result = get_relname_relid((const char *) object_name, LookupNamespaceNoError("pg_temp"));
}
}
else
Expand Down Expand Up @@ -2187,6 +2195,7 @@ object_id(PG_FUNCTION_ARGS)
}
}
}

pfree(object_name);
if (object_type)
pfree(object_type);
Expand Down Expand Up @@ -2363,7 +2372,8 @@ object_name(PG_FUNCTION_ARGS)
if (!OidIsValid(schema_id) ||
is_schema_from_db(schema_id, database_id) ||
(schema_id == get_namespace_oid("sys", true)) ||
(schema_id == get_namespace_oid("information_schema_tsql", true)))
(schema_id == get_namespace_oid("information_schema_tsql", true)) ||
(isTempNamespace(schema_id)))
{
PG_RETURN_VARCHAR_P((VarChar *) result_text);
}
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/BABEL_OBJECT_ID-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ GO
DROP SCHEMA [babel_object_id_schema .with .dot_and_spaces]
GO

DROP TYPE babel_object_id_type
GO

DROP TABLE [babel_object_id_t2 .with .dot_an_spaces]
GO

Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/BABEL_OBJECT_ID-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ GO
CREATE TABLE [babel_object_id_schema .with .dot_and_spaces]."babel_object_id_t3 .with .dot_and_spaces" (a int);
GO

CREATE TYPE babel_object_id_type FROM int
GO

-- To test lookup in different database
CREATE DATABASE babel_object_id_db;
GO
Expand Down
12 changes: 12 additions & 0 deletions test/JDBC/expected/BABEL_OBJECT_ID-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ varchar
~~END~~


-- Test temp objects not in ENR
CREATE TABLE #babel_object_id_temp_t2(a babel_object_id_type);
GO

SELECT OBJECT_NAME(OBJECT_ID('#babel_object_id_temp_t2'))
GO
~~START~~
varchar
#babel_object_id_temp_t2
~~END~~


-- We can also specify object_type as parameter
SELECT OBJECT_NAME(OBJECT_ID('#babel_object_id_temp_t1', 'U'))
GO
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/input/BABEL_OBJECT_ID-vu-cleanup.mix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ GO
DROP SCHEMA [babel_object_id_schema .with .dot_and_spaces]
GO

DROP TYPE babel_object_id_type
GO

DROP TABLE [babel_object_id_t2 .with .dot_an_spaces]
GO

Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/input/BABEL_OBJECT_ID-vu-prepare.mix
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ GO
CREATE TABLE [babel_object_id_schema .with .dot_and_spaces]."babel_object_id_t3 .with .dot_and_spaces" (a int);
GO

CREATE TYPE babel_object_id_type FROM int
GO

-- To test lookup in different database
CREATE DATABASE babel_object_id_db;
GO
Expand Down
7 changes: 7 additions & 0 deletions test/JDBC/input/BABEL_OBJECT_ID-vu-verify.mix
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ GO
SELECT OBJECT_NAME(OBJECT_ID('tempdb..#babel_object_id_temp_t1'))
GO

-- Test temp objects not in ENR
CREATE TABLE #babel_object_id_temp_t2(a babel_object_id_type);
GO

SELECT OBJECT_NAME(OBJECT_ID('#babel_object_id_temp_t2'))
GO

-- We can also specify object_type as parameter
SELECT OBJECT_NAME(OBJECT_ID('#babel_object_id_temp_t1', 'U'))
GO
Expand Down

0 comments on commit 9f67648

Please sign in to comment.