Skip to content

Commit

Permalink
Fix for temp tables with quoted identifiers flag (#3278)
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-rpant1729 authored Nov 27, 2024
1 parent c177652 commit b69cd1c
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testInExecut
assertEquals('tempVarForIn_8', $varName);
}

function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFetchSnowflakeTempTableDbAndSchemaSpecified():Boolean[1]
function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFetchSnowflakeTempTableStrategyDbAndSchemaSpecified():Boolean[1]
{
let tree = #{
Person {
Expand Down Expand Up @@ -191,11 +191,40 @@ function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFet
let rootNode2 = $generatedPlan2.rootExecutionNode.executionNodes->at(0)->cast(@StoreMappingGlobalGraphFetchExecutionNode).localGraphFetchExecutionNode->cast(@RelationalRootQueryTempTableGraphFetchExecutionNode);
let processedTempTableName2 = $rootNode2.processedTempTableName;
assertEquals('temp_db1.temp_schema1.temp_table_node_0', $processedTempTableName2);
let tempTableStrategy = $rootNode2.tempTableStrategy;
assertEquals('CREATE TEMPORARY TABLE temp_db1.temp_schema1.temp_table_node_0(pk_0 INT);', $tempTableStrategy.createTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('INSERT INTO temp_db1.temp_schema1.temp_table_node_0 VALUES ${temp_table_rows_from_result_set}', $tempTableStrategy.loadTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('Drop table if exists temp_db1.temp_schema1.temp_table_node_0;', $tempTableStrategy.dropTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
}


function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFetchSnowflakeTempTableStrategyWithQuoteIdentifiers():Boolean[1]
{
let tree = #{
Person {
firstName,
lastName,
firm {
legalName
}
}
}#;
let query = {|Person.all()->graphFetch($tree)->serialize($tree)};
let conn = ^RelationalDatabaseConnection(
datasourceSpecification = ^SnowflakeDatasourceSpecification(region = 'us-region-1', warehouseName='warehouseName',databaseName='databaseName',accountName = 'accountName'),
authenticationStrategy = ^SnowflakePublicAuthenticationStrategy(privateKeyVaultReference = 'privatekey', passPhraseVaultReference= 'passphrase', publicUserName = 'public'),
type=DatabaseType.Snowflake, quoteIdentifiers = true
);
let generatedPlan = executionPlan($query, simpleRelationalMapping, ^Runtime(connectionStores=^ConnectionStore(element = 'database',connection=$conn)), meta::relational::extension::relationalExtensions());
let rootNode = $generatedPlan.rootExecutionNode.executionNodes->at(0)->cast(@StoreMappingGlobalGraphFetchExecutionNode).localGraphFetchExecutionNode->cast(@RelationalRootQueryTempTableGraphFetchExecutionNode);
let processedTempTableName = $rootNode.processedTempTableName;
assertEquals('"LEGEND_TEMP_DB"."LEGEND_TEMP_SCHEMA"."temp_table_node_0"', $processedTempTableName);
let tempTableStrategy = $rootNode.tempTableStrategy;
assertEquals('CREATE TEMPORARY TABLE "LEGEND_TEMP_DB"."LEGEND_TEMP_SCHEMA"."temp_table_node_0"("pk_0" INT);', $tempTableStrategy.createTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('INSERT INTO "LEGEND_TEMP_DB"."LEGEND_TEMP_SCHEMA"."temp_table_node_0" VALUES ${temp_table_rows_from_result_set}', $tempTableStrategy.loadTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('Drop table if exists "LEGEND_TEMP_DB"."LEGEND_TEMP_SCHEMA"."temp_table_node_0";', $tempTableStrategy.dropTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
}

function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFetchSnowflakeTempTableDbAndSchemaWithQuoteIdentifiers():Boolean[1]
function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFetchSnowflakeTempTableStrategyWithDbSchemaAndQuoteIdentifiers():Boolean[1]
{
let tree = #{
Person {
Expand All @@ -215,9 +244,14 @@ function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testGraphFet
let generatedPlan = executionPlan($query, simpleRelationalMapping, ^Runtime(connectionStores=^ConnectionStore(element = 'database',connection=$conn)), meta::relational::extension::relationalExtensions());
let rootNode = $generatedPlan.rootExecutionNode.executionNodes->at(0)->cast(@StoreMappingGlobalGraphFetchExecutionNode).localGraphFetchExecutionNode->cast(@RelationalRootQueryTempTableGraphFetchExecutionNode);
let processedTempTableName = $rootNode.processedTempTableName;
assertEquals('"temp_db1"."temp_schema1".temp_table_node_0', $processedTempTableName);
assertEquals('"temp_db1"."temp_schema1"."temp_table_node_0"', $processedTempTableName);
let tempTableStrategy = $rootNode.tempTableStrategy;
assertEquals('CREATE TEMPORARY TABLE "temp_db1"."temp_schema1"."temp_table_node_0"("pk_0" INT);', $tempTableStrategy.createTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('INSERT INTO "temp_db1"."temp_schema1"."temp_table_node_0" VALUES ${temp_table_rows_from_result_set}', $tempTableStrategy.loadTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('Drop table if exists "temp_db1"."temp_schema1"."temp_table_node_0";', $tempTableStrategy.dropTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
}
function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testCrossStoreGraphFetchSnowflakeTempTableDbAndSchemaSpecified():Boolean[1]

function <<test.Test>> meta::pure::executionPlan::tests::snowflake::testCrossStoreGraphFetchSnowflakeTempTableStrategyDbAndSchemaSpecified():Boolean[1]
{
let tree = #{
meta::relational::graphFetch::tests::crossDatabase::Firm {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ function meta::relational::functions::sqlQueryToString::snowflake::getDDLCommand
function meta::relational::functions::sqlQueryToString::snowflake::processTempTableNameSnowflake(tempTableName: String[1], dc: meta::external::store::relational::runtime::DatabaseConnection[1]): String[1]
{
let dataSourceSpec = if($dc->instanceOf(RelationalDatabaseConnection),| $dc->cast(@RelationalDatabaseConnection).datasourceSpecification->cast(@SnowflakeDatasourceSpecification),| []);
let dbConfig = meta::relational::functions::sqlQueryToString::createDbConfig($dc);

if($dataSourceSpec->isNotEmpty() &&
$dataSourceSpec.tempTableDb->isNotEmpty() &&
$dataSourceSpec.tempTableSchema->isNotEmpty(),
|
let dbConfig = meta::relational::functions::sqlQueryToString::createDbConfig($dc.type, $dc.timeZone, $dc.quoteIdentifiers);
$dbConfig.identifierProcessor($dataSourceSpec.tempTableDb->toOne()) + '.' + $dbConfig.identifierProcessor($dataSourceSpec.tempTableSchema->toOne()) + '.' + $tempTableName;,
$dbConfig.identifierProcessor($dataSourceSpec.tempTableDb->toOne()) + '.' + $dbConfig.identifierProcessor($dataSourceSpec.tempTableSchema->toOne()) + '.' + $dbConfig.identifierProcessor($tempTableName);,
|
assert($dataSourceSpec.tempTableDb->isEmpty() && $dataSourceSpec.tempTableSchema->isEmpty(), 'One of Database name and schema name for temp tables is missing. Please specify both.');
'LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.' + $tempTableName;
$dbConfig.identifierProcessor('LEGEND_TEMP_DB') +'.' + $dbConfig.identifierProcessor('LEGEND_TEMP_SCHEMA') + '.' + $dbConfig.identifierProcessor($tempTableName);
);
}

Expand All @@ -147,16 +147,15 @@ function meta::relational::functions::sqlQueryToString::snowflake::loadValuesToD
if($l.absolutePathToFile->isNotEmpty(),|
let createStage = 'CREATE OR REPLACE TEMPORARY STAGE LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.LEGEND_TEMP_STAGE';
let loadTempTableCommand = 'PUT file://'+$l.absolutePathToFile->toOne()->processOperation($dbConfig.dbType, []) + ' @LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.LEGEND_TEMP_STAGE' + $l.absolutePathToFile->toOne()->processOperation($dbConfig.dbType, []) + ' PARALLEL= 16 AUTO_COMPRESS=TRUE;';
let copyIntoCommand = 'COPY INTO ' + $l.table.name->toOne() + ' FROM @LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.LEGEND_TEMP_STAGE' + $l.absolutePathToFile->toOne()->processOperation($dbConfig.dbType, []) + ' file_format = (type= CSV field_optionally_enclosed_by= \'"\');';
let copyIntoCommand = 'COPY INTO ' + $l.table->tableToString($dbConfig)->toOne() + ' FROM @LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.LEGEND_TEMP_STAGE' + $l.absolutePathToFile->toOne()->processOperation($dbConfig.dbType, []) + ' file_format = (type= CSV field_optionally_enclosed_by= \'"\');';
let dropStage = 'DROP STAGE LEGEND_TEMP_DB.LEGEND_TEMP_SCHEMA.LEGEND_TEMP_STAGE';
[$createStage, $loadTempTableCommand, $copyIntoCommand, $dropStage];
,| $l->meta::relational::functions::sqlQueryToString::default::loadValuesToDbTableDefault($dbConfig))
}

function meta::relational::functions::sqlQueryToString::snowflake::translateDropTableStatementForSnowflake(dropTableSQL:DropTableSQL[1], dbConfig: DbConfig[1]) : String[1]
function meta::relational::functions::sqlQueryToString::snowflake::translateDropTableStatementForSnowflake(d:DropTableSQL[1], dbConfig: DbConfig[1]) : String[1]
{
let t= $dropTableSQL.table;
'Drop table if exists '+if($t.schema.name == 'default',|'',|$t.schema.name+'.')+$t.name+';';
'Drop table if exists ' + $d.table->tableToString($dbConfig) + ';';
}

function <<access.private>> meta::relational::functions::sqlQueryToString::snowflake::getDynaFunctionToSqlForSnowflake(): DynaFunctionToSql[*]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::relational::graphFetch::executionPlan::*;
import meta::relational::functions::sqlQueryToString::h2::*;
import meta::pure::alloy::connections::alloy::authentication::*;
import meta::pure::alloy::connections::alloy::specification::*;
Expand Down Expand Up @@ -2627,3 +2628,46 @@ function <<test.Test>> meta::pure::executionPlan::tests::datetime::testPlanWithL

}

function <<test.Test>> meta::pure::executionPlan::tests::testGraphFetchH2TempTableStrategy():Boolean[1]
{
let tree = #{
Person {
firstName,
lastName,
firm {
legalName
}
}
}#;
let query = {|Person.all()->graphFetch($tree)->serialize($tree)};
let generatedPlan = executionPlan($query, simpleRelationalMapping, meta::external::store::relational::tests::testRuntime(), meta::relational::extension::relationalExtensions());
let rootNode = $generatedPlan.rootExecutionNode.executionNodes->at(0)->cast(@StoreMappingGlobalGraphFetchExecutionNode).localGraphFetchExecutionNode->cast(@RelationalRootQueryTempTableGraphFetchExecutionNode);
let processedTempTableName = $rootNode.processedTempTableName;
assertEquals('temp_table_node_0', $processedTempTableName);
let tempTableStrategy = $rootNode.tempTableStrategy;
assertEquals('CREATE LOCAL TEMPORARY TABLE temp_table_node_0(pk_0 INT);', $tempTableStrategy.createTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('INSERT INTO temp_table_node_0 SELECT * FROM CSVREAD(\'${csv_file_location}\');', $tempTableStrategy.loadTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('Drop table if exists temp_table_node_0;', $tempTableStrategy.dropTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
}

function <<test.Test>> meta::pure::executionPlan::tests::testGraphFetchH2TempTableStrategyWithQuoteIdentifiers():Boolean[1]
{
let tree = #{
Person {
firstName,
lastName,
firm {
legalName
}
}
}#;
let query = {|Person.all()->graphFetch($tree)->serialize($tree)};
let generatedPlan = executionPlan($query, simpleRelationalMapping, meta::external::store::relational::tests::testRuntime(true), meta::relational::extension::relationalExtensions());
let rootNode = $generatedPlan.rootExecutionNode.executionNodes->at(0)->cast(@StoreMappingGlobalGraphFetchExecutionNode).localGraphFetchExecutionNode->cast(@RelationalRootQueryTempTableGraphFetchExecutionNode);
let processedTempTableName = $rootNode.processedTempTableName;
assertEquals('"temp_table_node_0"', $processedTempTableName);
let tempTableStrategy = $rootNode.tempTableStrategy;
assertEquals('CREATE LOCAL TEMPORARY TABLE "temp_table_node_0"("pk_0" INT);', $tempTableStrategy.createTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('INSERT INTO "temp_table_node_0" SELECT * FROM CSVREAD(\'${csv_file_location}\');', $tempTableStrategy.loadTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
assertEquals('Drop table if exists "temp_table_node_0";', $tempTableStrategy.dropTempTableNode.executionNodes->at(0)->cast(@SQLExecutionNode).sqlQuery);
}
Loading

0 comments on commit b69cd1c

Please sign in to comment.