Skip to content

Commit

Permalink
Fix snowflake string functions (#2463)
Browse files Browse the repository at this point in the history
* Update snowflakeExtension.pure

* Update testSnowflakeWithFunction.pure

* Update testSnowflakeTDSWindowColumn.pure

* Update snowflakeExtension.pure

* Update snowflakeExtension.pure
  • Loading branch information
PrateekGarg-gs authored Feb 2, 2024
1 parent 4c2b3d3 commit 66a1f25
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
dynaFnToSql('convertDate', $allStates, ^ToSql(format='%s', transform={p:String[*] | $p->convertToDateSnowflake()})),
dynaFnToSql('convertDateTime', $allStates, ^ToSql(format='%s' , transform={p:String[*] | $p->convertToDateTimeSnowflake()})),
dynaFnToSql('convertVarchar128', $allStates, ^ToSql(format='to_char(%s)')),
dynaFnToSql('contains', $allStates, ^ToSql(format='contains(%s,%s)', transform={p:String[2]|$p})),
dynaFnToSql('dateDiff', $allStates, ^ToSql(format='datediff(%s,%s,%s)', transform={p:String[*]|[$p->at(2)->replace('\'', '')->processDateDiffDurationUnitForSnowflake(),$p->at(0),$p->at(1)]})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='Date(%s)')),
dynaFnToSql('dayOfMonth', $allStates, ^ToSql(format='DAYOFMONTH(%s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='to_char(%s, \'DYDY\')')),
dynaFnToSql('dayOfWeekNumber', $allStates, ^ToSql(format='DAYOFWEEKISO(%s)')),
dynaFnToSql('dayOfYear', $allStates, ^ToSql(format='DAYOFYEAR(%s)')),
dynaFnToSql('endsWith', $allStates, ^ToSql(format='endswith(%s,%s)', transform={p:String[2]|$p})),
dynaFnToSql('decodeBase64', $allStates, ^ToSql(format='BASE64_DECODE_STRING(%s)')),
dynaFnToSql('encodeBase64', $allStates, ^ToSql(format='BASE64_ENCODE(%s)')),
dynaFnToSql('extractFromSemiStructured', $allStates, ^ToSql(format='%s', transform={p:String[3]|$p->processExtractFromSemiStructuredParamsForSnowflake()})),
Expand Down Expand Up @@ -159,6 +161,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
dynaFnToSql('quarter', $allStates, ^ToSql(format='quarter(%s)')),
dynaFnToSql('quarterNumber', $allStates, ^ToSql(format='quarter(%s)')),
dynaFnToSql('round', $allStates, ^ToSql(format='round((%s)::numeric, %s)', transform=transformRound_String_MANY__String_MANY_)),
dynaFnToSql('startsWith', $allStates, ^ToSql(format='startswith(%s,%s)', transform={p:String[2]|$p})),
dynaFnToSql('second', $allStates, ^ToSql(format='second(%s)')),
dynaFnToSql('sha256', $allStates, ^ToSql(format='sha2(%s, 256)')),
dynaFnToSql('substring', $allStates, ^ToSql(format='substring%s', transform={p:String[*]|$p->joinStrings('(', ', ', ')')})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ function <<test.Test>> meta::relational::tests::tds::snowflake::testOLAPGroupByS
->filter(r|$r.getInteger('rowNumber') > 10)
};
let result = toSQLString($func, simpleRelationalMappingIncWithStoreFilter, DatabaseType.Snowflake, meta::relational::extension::relationalExtensions());
assertEquals('select "firstName" as "firstName", "lastName" as "lastName", "ageSum" as "ageSum", "rowNumber" as "rowNumber" from (select "root".FIRSTNAME as "firstName", "root".LASTNAME as "lastName", sum("root".AGE) as "ageSum", row_number() OVER (Order By "root".FIRSTNAME ASC) as "rowNumber" from (select "root".ID as ID, "root".FIRSTNAME as FIRSTNAME, "root".LASTNAME as LASTNAME, "root".AGE as AGE from personTable as "root" where "root".AGE > 110) as "root" where "root".AGE < 200 and "root".LASTNAME like \'David%\' group by "firstName","lastName") as "subselect" where "rowNumber" > 10', $result);
assertEquals('select "firstName" as "firstName", "lastName" as "lastName", "ageSum" as "ageSum", "rowNumber" as "rowNumber" from (select "root".FIRSTNAME as "firstName", "root".LASTNAME as "lastName", sum("root".AGE) as "ageSum", row_number() OVER (Order By "root".FIRSTNAME ASC) as "rowNumber" from (select "root".ID as ID, "root".FIRSTNAME as FIRSTNAME, "root".LASTNAME as LASTNAME, "root".AGE as AGE from personTable as "root" where "root".AGE > 110) as "root" where "root".AGE < 200 and startswith("root".LASTNAME,\'David\') group by "firstName","lastName") as "subselect" where "rowNumber" > 10', $result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ function <<test.Test>> meta::relational::tests::query::snowflake::testFilterUsin
assertEquals('select "root".ID as "pk_0", "root".FIRSTNAME as "firstName", "root".AGE as "age", "root".LASTNAME as "lastName" from personTable as "root" left outer join firmTable as "firmTable_d#2_dy0_d#3_d_m1" on ("firmTable_d#2_dy0_d#3_d_m1".ID = "root".FIRMID) where "firmTable_d#2_dy0_d#3_d_m1".LEGALNAME regexp \'^[A-Za-z0-9]*$\'',$s);
}

function <<test.Test>> meta::relational::tests::query::snowflake::testFilterUsingContains():Boolean[1]
{
let sql =toSQLString(|Person.all()->project(p|$p.firstName,'firstName')->filter(p | $p.getString('firstName')->contains('d')),
simpleRelationalMapping, meta::relational::runtime::DatabaseType.Snowflake, meta::relational::extension::relationalExtensions());
assertEquals('select "root".FIRSTNAME as "firstName" from personTable as "root" where contains("root".FIRSTNAME,\'d\')', $sql);
}

function <<test.Test>> meta::relational::tests::query::snowflake::testFilterUsingContainsNested():Boolean[1]
{
let sql =toSQLString(|Person.all()->project(p|$p.firstName,'firstName')->filter(p | $p.getString('firstName')->toLower()->contains(toLower('d'))),
simpleRelationalMapping, meta::relational::runtime::DatabaseType.Snowflake, meta::relational::extension::relationalExtensions());
assertEquals('select "root".FIRSTNAME as "firstName" from personTable as "root" where contains(lower("root".FIRSTNAME),lower(\'d\'))', $sql);
}

function <<test.Test>> meta::relational::tests::query::snowflake::testFilterBoolean():Boolean[1]
{
let sql =toSQLString(|Person.all()->project(p|$p.firstName,'firstName')->filter(p | if(
Expand All @@ -47,5 +61,7 @@ function <<test.Test>> meta::relational::tests::query::snowflake::testFilterBool
|false
)),
simpleRelationalMapping, meta::relational::runtime::DatabaseType.Snowflake, meta::relational::extension::relationalExtensions());
assertEquals('select "root".FIRSTNAME as "firstName" from personTable as "root" where case when "root".FIRSTNAME like \'d%\' then true else false end', $sql);
}
assertEquals('select "root".FIRSTNAME as "firstName" from personTable as "root" where case when startswith("root".FIRSTNAME,\'d\') then true else false end', $sql);
}


0 comments on commit 66a1f25

Please sign in to comment.