Skip to content

Commit

Permalink
fix snowflake convert Tz sql generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AFine-gs committed Dec 12, 2024
1 parent 39e5c0c commit 0a13570
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2021 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::relational::functions::pureToSqlQuery::metamodel::*;
import meta::relational::metamodel::join::*;
import meta::pure::alloy::connections::*;
Expand Down Expand Up @@ -448,14 +462,14 @@ 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);
//https://docs.snowflake.com/en/sql-reference/functions/convert_timezone
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));
format('TO_CHAR(CONVERT_TIMEZONE(%s,%s),%s)',[$params->at(0),$params->at(1),$format]);
format('TO_CHAR(CONVERT_TIMEZONE(%s,%s),%s)',[$params->at(1),$params->at(0),$format]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,5 @@ function <<test.Test>> meta::relational::tests::mapping::function::snowflake::te
let s = toSQLString(|SqlFunctionDemo.all()->project([s | $s.convertTimeZone,s|$s.dateTime->convertTimeZone('EST','yyyy-MM-ddTHH:mm:ss')], ['tzm','tzQuery']),
testMapping,
meta::relational::runtime::DatabaseType.Snowflake, meta::relational::extension::relationalExtensions());
assertEquals('select TO_CHAR(CONVERT_TIMEZONE("root".dateTime,\'EST\'),\'YYYY-MM-DD HH24:MI:SS\') as "tzm", TO_CHAR(CONVERT_TIMEZONE("root".dateTime,\'EST\'),\'YYYY-MM-DDTHH24:MI:SS\') as "tzQuery" from dataTable as "root"',$s);
assertEquals('select TO_CHAR(CONVERT_TIMEZONE(\'EST\',"root".dateTime),\'YYYY-MM-DD HH24:MI:SS\') as "tzm", TO_CHAR(CONVERT_TIMEZONE(\'EST\',"root".dateTime),\'YYYY-MM-DDTHH24:MI:SS\') as "tzQuery" from dataTable as "root"',$s);
}

0 comments on commit 0a13570

Please sign in to comment.