From 554665f00486bb880010bf79a3fd1f077c3022a5 Mon Sep 17 00:00:00 2001 From: gs-jp1 <80327721+gs-jp1@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:41:03 +0000 Subject: [PATCH] Legend SQL - Handle null valuespecifications correctly in cast (#2499) --- .../binding/fromPure/fromPure.pure | 9 +++++---- .../binding/fromPure/tests/testTranspile.pure | 7 +++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure index 6baecee1be0..189a6656376 100644 --- a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure +++ b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure @@ -1585,9 +1585,10 @@ function <> meta::external::query::sql::transformation::queryToP let expression = processExpression($c.expression, $expContext, $context); - $c.expression->match([ - n:NullLiteral[1] | processCastAsCast($c, $expression, $expContext, $context), - e:meta::external::query::sql::metamodel::Expression[1] | + if ($expression->evaluateAndDeactivate().multiplicity == PureZero, + | + processCastAsCast($c, $expression, $expContext, $context), + | let type = $expression.genericType.rawType->toOne(); let castName = $c.type.name->toUpper(); @@ -1598,7 +1599,7 @@ function <> meta::external::query::sql::transformation::queryToP pair($type == String || $castName == 'TEXT', | processCastAsParse($c, $expression, $expContext, $context)), pair(true, | processCastAsCast($c, $expression, $expContext, $context)) ]->getValues(true)->first()->toOne()->eval()->evaluateAndDeactivate(); - ]); + ); } function <> meta::external::query::sql::transformation::queryToPure::processDoublePrecisionCast(c:Cast[1], v:ValueSpecification[1], expContext:SqlTransformExpressionContext[1], context:SqlTransformContext[1]):ValueSpecification[1] diff --git a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure index 3b18da5beb8..24131ddf746 100644 --- a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure +++ b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure @@ -1051,7 +1051,9 @@ function <> meta::external::query::sql::transformation::queryToPure:: function <> meta::external::query::sql::transformation::queryToPure::tests::testNullCasts():Boolean[1] { test('SELECT CAST(NULL AS VARCHAR) AS "string", CAST(NULL AS TEXT) AS "text", CAST(NULL AS DATE) AS "date", ' + - 'CAST(NULL AS INTEGER) AS "integer", CAST(NULL AS BOOLEAN) AS "boolean", CAST(NULL AS DOUBLE PRECISION) AS "double", CAST(NULL AS NUMERIC) AS "numeric", CAST(NULL AS TIMESTAMP) AS "timestamp" FROM service."/service/service1"', + 'CAST(NULL AS INTEGER) AS "integer", CAST(NULL AS BOOLEAN) AS "boolean", CAST(NULL AS DOUBLE PRECISION) AS "double", ' + + 'CAST(NULL AS NUMERIC) AS "numeric", CAST(NULL AS TIMESTAMP) AS "timestamp", CAST(floor(NULL) AS TEXT) AS "exp" ' + + 'FROM service."/service/service1"', {| FlatInput.all()->project( [ x | $x.booleanIn, x | $x.integerIn, x | $x.floatIn, x | $x.decimalIn, x | $x.strictDateIn, x | $x.dateTimeIn, x | $x.stringIn ], @@ -1064,7 +1066,8 @@ function <> meta::external::query::sql::transformation::queryToPure:: col(row:TDSRow[1] | cast([], @Boolean), 'boolean'), col(row:TDSRow[1] | cast([], @Float), 'double'), col(row:TDSRow[1] | cast([], @Decimal), 'numeric'), - col(row:TDSRow[1] | cast([], @DateTime), 'timestamp') + col(row:TDSRow[1] | cast([], @DateTime), 'timestamp'), + col(row:TDSRow[1] | cast([], @String), 'exp') ]) }) }