From fb2b1449164424bb467000e4181f2d09f9f06fdd Mon Sep 17 00:00:00 2001 From: AFine-gs <69924417+AFine-gs@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:26:00 -0500 Subject: [PATCH] fix snowflake convert Tz sql generation (#3301) --- .../sqlQueryToString/snowflakeExtension.pure | 20 ++++++++++++++++--- .../testSnowflakeSqlFunctionsInMapping.pure | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/sqlQueryToString/snowflakeExtension.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/sqlQueryToString/snowflakeExtension.pure index 5debf3dbda3..f2916be0a9d 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/sqlQueryToString/snowflakeExtension.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/sqlQueryToString/snowflakeExtension.pure @@ -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::*; @@ -448,14 +462,14 @@ function meta::relational::functions::sqlQueryToString::snowflake::preAndFinally } - function <> 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]); } diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/tests/testSnowflakeSqlFunctionsInMapping.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/tests/testSnowflakeSqlFunctionsInMapping.pure index 74a28a479ef..f3ebec7af35 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/tests/testSnowflakeSqlFunctionsInMapping.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-dbExtension/legend-engine-xt-relationalStore-snowflake/legend-engine-xt-relationalStore-snowflake-pure/src/main/resources/core_relational_snowflake/relational/tests/testSnowflakeSqlFunctionsInMapping.pure @@ -202,5 +202,5 @@ function <> 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); }