Skip to content

Commit

Permalink
fix schema processing for snowflake (finos#3115)
Browse files Browse the repository at this point in the history
* fix schema processing for snowflake

* Update dbExtension.pure
  • Loading branch information
Yasirmod17 authored Sep 22, 2024
1 parent 386afa1 commit 74e37d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
joinStringsProcessor = processJoinStringsOperationForSnowflake_JoinStrings_1__SqlGenerationContext_1__String_1_,
selectSQLQueryProcessor = processSelectSQLQueryForSnowflake_SelectSQLQuery_1__SqlGenerationContext_1__Boolean_1__String_1_,
selectSQLQueryWithCTEsProcessor = processSelectSQLQueryWithCTEsDefault_SelectSQLQueryWithCommonTableExpressions_1__SqlGenerationContext_1__Boolean_1__String_1_,
schemaIdentifierToString = meta::relational::functions::sqlQueryToString::snowflake::schemaIdentifierToString_String_1__DbConfig_1__String_1_,
columnNameToIdentifier = columnNameToIdentifierDefault_String_1__DbConfig_1__String_1_,
identifierProcessor = processIdentifierWithDoubleQuotes_String_1__DbConfig_1__String_1_,
dynaFuncDispatch = $dynaFuncDispatch,
Expand All @@ -50,6 +51,11 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
);
}

function meta::relational::functions::sqlQueryToString::snowflake::schemaIdentifierToString(identifier:String[1], dbConfig: DbConfig[1]):String[1]
{
$identifier->split('.')->map(s|$s->processIdentifierWithQuoteChar('"', $dbConfig))->joinStrings('.');
}

function <<access.private>> meta::relational::functions::sqlQueryToString::snowflake::getLiteralProcessorsForSnowflake():Map<Type,LiteralProcessor>[1]
{
newMap([
Expand Down Expand Up @@ -413,7 +419,7 @@ function meta::relational::functions::sqlQueryToString::snowflake::preAndFinally

function <<access.private>> meta::relational::functions::sqlQueryToString::snowflake::transformConvertTimeZone(params:String[3],context:SqlGenerationContext[1]):String[1]
{
let unWrappedfmt = $params->at(2)->substring(1, $params->at(2)->length()-1); $unWrappedfmt->println();
let unWrappedfmt = $params->at(2)->substring(1, $params->at(2)->length()-1);
assert($unWrappedfmt->validateDateTimeFormat(),'Found an invalid date format');
let formatpairs = meta::relational::functions::sqlQueryToString::default::defaultJavaToSQLTimeParts();
let format = $formatpairs->fold( {sub, date| $date->toOne()->replace($sub.first,$sub.second)},$params->at(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ function <<test.Test>> meta::relational::tests::sqlToString::snowflake::testToSQ
assertEquals('select "root"."NAME" as "name" from "productSchema"."synonymTable" as "root" where (\'CUSIP\' <> \'ISIN\')', $result->replace('\n', ''));
}

function <<test.Test>> meta::relational::tests::sqlToString::snowflake::testToSQLStringWithQuoteIdentifiersFlagAndSchemaMapping():Boolean[1]
{
let runtime = ^meta::core::runtime::Runtime(connectionStores = ^meta::core::runtime::ConnectionStore(element = meta::relational::tests::db,
connection=^meta::external::store::relational::runtime::TestDatabaseConnection(
type = DatabaseType.Snowflake,
quoteIdentifiers = true,
sqlQueryPostProcessors = meta::pure::alloy::connections::postProcessTablesFn([], ^meta::pure::alloy::connections::SchemaNameMapper(from='productSchema', to='test.productSchema'))
->map(pp|{query:meta::relational::metamodel::relation::SelectSQLQuery[1] | $query->meta::relational::postProcessor::postprocess($pp)})
)));
let result = toSQLStringPretty(|Synonym.all()->filter(s | $s.type != 'ISIN')->project([s | $s.name],['name']),
simpleRelationalMappingWithEnumConstant, $runtime, meta::relational::extension::relationalExtensions());

assertEquals('select "root"."NAME" as "name" from "test"."productSchema"."synonymTable" as "root" where (\'CUSIP\' <> \'ISIN\')', $result->replace('\n', ''));
}

function <<test.Test>> meta::relational::tests::sqlToString::snowflake::testToSQLStringWithQuoteIdentifiersFlagInColumnName():Boolean[1]
{
let runtime = ^meta::core::runtime::Runtime(connectionStores = ^meta::core::runtime::ConnectionStore(element = meta::relational::tests::db, connection=^meta::external::store::relational::runtime::TestDatabaseConnection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Class meta::relational::functions::sqlQueryToString::DbConfig
if($this.dbExtension.schemaNameToIdentifier->isEmpty(), |$s, |$this.dbExtension.schemaNameToIdentifier->toOne()->eval($s, $this));
}: String[1];

schemaIdentifierToString(s:String[1])
{
if($this.dbExtension.schemaIdentifierToString->isEmpty(), |$this.identifierProcessor($s), |$this.dbExtension.schemaIdentifierToString->toOne()->eval($s, $this));
}: String[1];


tableNameToIdentifier(s:String[1])
{
if($this.dbExtension.tableNameToIdentifier->isEmpty(), |$s, |$this.dbExtension.tableNameToIdentifier->toOne()->eval($s, $this));
Expand Down Expand Up @@ -226,6 +232,7 @@ Class meta::relational::functions::sqlQueryToString::DbExtension
selectSQLQueryWithCTEsProcessor: meta::pure::metamodel::function::Function<{SelectSQLQueryWithCommonTableExpressions[1], SqlGenerationContext[1], Boolean[1] -> String[1]}>[0..1];
upsertSQLQueryProcessor: meta::pure::metamodel::function::Function<{UpsertSQLQuery[1], SqlGenerationContext[1] -> String[1]}>[0..1];
schemaNameToIdentifier: meta::pure::metamodel::function::Function<{String[1], DbConfig[1] -> String[1]}>[0..1];
schemaIdentifierToString: meta::pure::metamodel::function::Function<{String[1], DbConfig[1] -> String[1]}>[0..1];
tableNameToIdentifier: meta::pure::metamodel::function::Function<{String[1], DbConfig[1] -> String[1]}>[0..1];
columnNameToIdentifier: meta::pure::metamodel::function::Function<{String[1], DbConfig[1] -> String[1]}>[0..1];
identifierProcessor: meta::pure::metamodel::function::Function<{String[1], DbConfig[1] -> String[1]}>[1];
Expand Down Expand Up @@ -443,7 +450,7 @@ function meta::relational::functions::sqlQueryToString::tableToString(table:Tabl
{
let schemaName = $dbConfig.schemaNameToIdentifier($table.schema.name);
let tableName = $dbConfig.tableNameToIdentifier($table.name);
if($schemaName == 'default', | $dbConfig.identifierProcessor($tableName), | $dbConfig.identifierProcessor($schemaName) + '.' + $dbConfig.identifierProcessor($tableName));
if($schemaName == 'default', | $dbConfig.identifierProcessor($tableName), | $dbConfig.schemaIdentifierToString($schemaName) + '.' + $dbConfig.identifierProcessor($tableName));
}

function meta::relational::functions::sqlQueryToString::indent(d:Format[1]):Format[1]
Expand Down

0 comments on commit 74e37d0

Please sign in to comment.