-
Notifications
You must be signed in to change notification settings - Fork 133
Support BCP on TSQL #temp table #4632
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
base: BABEL_5_X_DEV
Are you sure you want to change the base?
Changes from 11 commits
c621fb9
5029e18
902d340
df64359
51b0660
f2b73fb
ca868a6
aba6f2d
e25e9b6
b8eabe0
ee6121b
6ae335d
d62a003
791a09d
bb7ed6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| #include "catalog/pg_namespace.h" | ||
| #include "catalog/pg_type.h" | ||
| #include "catalog/pg_attrdef.h" | ||
| #include "catalog/pg_attribute.h" | ||
| #include "catalog/pg_depend.h" | ||
| #include "commands/dbcommands.h" | ||
| #include "commands/extension.h" | ||
|
|
@@ -135,6 +136,7 @@ PG_FUNCTION_INFO_V1(servername); | |
| PG_FUNCTION_INFO_V1(servicename); | ||
| PG_FUNCTION_INFO_V1(xact_state); | ||
| PG_FUNCTION_INFO_V1(get_enr_list); | ||
| PG_FUNCTION_INFO_V1(get_enr_attributes); | ||
| PG_FUNCTION_INFO_V1(tsql_random); | ||
| PG_FUNCTION_INFO_V1(timezone_mapping); | ||
| PG_FUNCTION_INFO_V1(pltsql_timezone_mapping_pg_to_windows); | ||
|
|
@@ -6094,3 +6096,136 @@ openxml_simple(PG_FUNCTION_ARGS) | |
| #endif /* USE_LIBXML */ | ||
| } | ||
|
|
||
| /* | ||
| * get_enr_attributes - Get pg_attribute rows for temp tables (ENR and non-ENR) | ||
| * | ||
| * This function returns all pg_attribute columns for a temp table. | ||
| * It works for both ENR temp tables (reads from ENR cache) and non-ENR temp tables | ||
| * (reads from actual pg_attribute catalog). | ||
| */ | ||
| Datum | ||
| get_enr_attributes(PG_FUNCTION_ARGS) | ||
|
||
| { | ||
| char *table_name_input = PG_ARGISNULL(0) ? NULL : text_to_cstring(PG_GETARG_TEXT_PP(0)); | ||
| ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; | ||
| TupleDesc tupdesc; | ||
| Tuplestorestate *tupstore; | ||
| MemoryContext per_query_ctx; | ||
| MemoryContext oldcontext; | ||
| EphemeralNamedRelation enr = NULL; | ||
| char *table_name; | ||
| char *temp_name_ptr; | ||
| Relation pg_attribute_rel; | ||
| Oid relid = InvalidOid; | ||
| bool is_enr = false; | ||
| int i; | ||
|
|
||
| /* check to see if caller supports us returning a tuplestore */ | ||
| if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) | ||
| ereport(ERROR, | ||
| (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||
| errmsg("set-valued function called in context that cannot accept a set"))); | ||
| if (!(rsinfo->allowedModes & SFRM_Materialize)) | ||
| ereport(ERROR, | ||
| (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||
| errmsg("materialize mode required, but it is not allowed in this context"))); | ||
|
|
||
| if (!table_name_input) | ||
| PG_RETURN_NULL(); | ||
|
|
||
| /* Handle formats like ".[#TempTable]" or "#TempTable" to find the # */ | ||
| temp_name_ptr = strchr(table_name_input, '#'); | ||
| if (!temp_name_ptr) | ||
| PG_RETURN_NULL(); | ||
|
|
||
| /* Lowercase the table name and strip trailing "]" */ | ||
| table_name = pstrdup(temp_name_ptr); | ||
| for (i = 0; table_name[i]; i++) | ||
| { | ||
| if (table_name[i] == ']') | ||
| { | ||
| table_name[i] = '\0'; | ||
| break; | ||
| } | ||
| table_name[i] = tolower((unsigned char) table_name[i]); | ||
| } | ||
|
||
|
|
||
| /* Try ENR first */ | ||
| if (currentQueryEnv != NULL) | ||
| { | ||
| enr = get_ENR(currentQueryEnv, table_name, true); | ||
| if (enr != NULL && enr->md.enrtype == ENR_TSQL_TEMP) | ||
| { | ||
| relid = enr->md.reliddesc; | ||
| is_enr = true; | ||
| } | ||
| } | ||
|
|
||
| /* Fallback: search pg_temp schema for non-ENR temp tables */ | ||
| if (!is_enr) | ||
| { | ||
| Oid temp_ns = LookupNamespaceNoError("pg_temp"); | ||
| if (OidIsValid(temp_ns)) | ||
| relid = get_relname_relid(table_name, temp_ns); | ||
| } | ||
|
|
||
| if (!OidIsValid(relid)) | ||
| PG_RETURN_NULL(); | ||
|
|
||
| /* Setup return - use pg_attribute's tuple descriptor */ | ||
| per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; | ||
| oldcontext = MemoryContextSwitchTo(per_query_ctx); | ||
|
|
||
| pg_attribute_rel = table_open(AttributeRelationId, AccessShareLock); | ||
| tupdesc = CreateTupleDescCopy(RelationGetDescr(pg_attribute_rel)); | ||
|
|
||
| tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random, | ||
| false, work_mem); | ||
|
|
||
| rsinfo->returnMode = SFRM_Materialize; | ||
| rsinfo->setResult = tupstore; | ||
| rsinfo->setDesc = BlessTupleDesc(tupdesc); | ||
|
|
||
| MemoryContextSwitchTo(oldcontext); | ||
|
|
||
| if (is_enr) | ||
| { | ||
| /* ENR path: return tuples directly from ENR cache */ | ||
| ListCell *lc; | ||
|
|
||
| foreach(lc, enr->md.cattups[ENR_CATTUP_ATTRIBUTE]) | ||
| { | ||
| HeapTuple tup = (HeapTuple) lfirst(lc); | ||
|
|
||
| tuplestore_puttuple(tupstore, tup); | ||
| } | ||
|
||
| } | ||
| else | ||
| { | ||
| /* Non-ENR path: scan pg_attribute catalog */ | ||
| ScanKeyData skey[1]; | ||
| SysScanDesc scan; | ||
| HeapTuple tup; | ||
|
|
||
| ScanKeyInit(&skey[0], | ||
| Anum_pg_attribute_attrelid, | ||
| BTEqualStrategyNumber, F_OIDEQ, | ||
| ObjectIdGetDatum(relid)); | ||
|
|
||
| scan = systable_beginscan(pg_attribute_rel, AttributeRelidNumIndexId, | ||
| true, NULL, 1, skey); | ||
|
|
||
| while (HeapTupleIsValid(tup = systable_getnext(scan))) | ||
| { | ||
| tuplestore_puttuple(tupstore, tup); | ||
| } | ||
|
|
||
| systable_endscan(scan); | ||
| } | ||
|
|
||
| table_close(pg_attribute_rel, AccessShareLock); | ||
|
|
||
| tuplestore_donestoring(tupstore); | ||
|
|
||
| PG_RETURN_NULL(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| #include "utils/rel.h" | ||
| #include "utils/syscache.h" | ||
| #include "utils/fmgroids.h" | ||
| #include "utils/lsyscache.h" | ||
| #include "utils/formatting.h" | ||
| #include "pltsql_instr.h" | ||
| #include "pltsql.h" | ||
|
|
@@ -55,6 +56,7 @@ | |
| #include "catalog.h" | ||
| #include "catalog/toasting.h" | ||
| #include "extendedproperty.h" | ||
| #include "collation.h" | ||
| #include "multidb.h" | ||
| #include "session.h" | ||
| #include "rolecmds.h" | ||
|
|
@@ -5115,4 +5117,4 @@ tsql_openxml_get_colpattern(PG_FUNCTION_ARGS) | |
| } | ||
|
|
||
| PG_RETURN_TEXT_P(cstring_to_text(xpath_expr)); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert file back to original |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| -- BABEL-5264: Cleanup for sp_tablecollations_100 tests | ||
| -- Cleanup UDTs created for non-ENR tests | ||
| DROP TYPE IF EXISTS dbo.TestVarcharType1 | ||
| GO | ||
| DROP TYPE IF EXISTS dbo.TestVarcharType2 | ||
| GO | ||
| DROP TYPE IF EXISTS dbo.TestVarcharType3 | ||
| GO | ||
| DROP TYPE IF EXISTS dbo.TestVarcharType4 | ||
| GO | ||
| DROP TYPE IF EXISTS dbo.TestIntType | ||
| GO |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| -- BABEL-5264: Test setup for sp_tablecollations_100 functions | ||
| -- Create UDTs for non-ENR temp table tests | ||
| CREATE TYPE dbo.TestVarcharType1 FROM varchar(100) | ||
| GO | ||
| CREATE TYPE dbo.TestVarcharType2 FROM varchar(50) | ||
| GO | ||
| CREATE TYPE dbo.TestVarcharType3 FROM varchar(50) | ||
| GO | ||
| CREATE TYPE dbo.TestVarcharType4 FROM varchar(50) | ||
| GO | ||
| CREATE TYPE dbo.TestIntType FROM int | ||
| GO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks its about time babelfish temp table's code have a seperate file in extension : )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't agree more!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ayush-061 lets start with this one. Could you instead create a new file (maybe called -
temp-table-hooks) and write this function there instead. We'll move the rest slowly later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Fine
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the function get_tsql_temp_table_attribute or the other one is_temp_table name too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both of them