diff --git a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/DataTypeDerivedSqlCallConverter.java b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/DataTypeDerivedSqlCallConverter.java index 928e51c9d..f9c8c4f15 100644 --- a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/DataTypeDerivedSqlCallConverter.java +++ b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/DataTypeDerivedSqlCallConverter.java @@ -15,7 +15,7 @@ import com.linkedin.coral.common.utils.TypeDerivationUtil; import com.linkedin.coral.hive.hive2rel.HiveToRelConverter; import com.linkedin.coral.trino.rel2trino.transformers.FromUtcTimestampOperatorTransformer; -import com.linkedin.coral.trino.rel2trino.transformers.NamedStructOperandTransformer; +import com.linkedin.coral.trino.rel2trino.transformers.NamedStructToCastTransformer; /** @@ -33,7 +33,7 @@ public DataTypeDerivedSqlCallConverter(HiveMetastoreClient mscClient, SqlNode to SqlValidator sqlValidator = new HiveToRelConverter(mscClient).getSqlValidator(); TypeDerivationUtil typeDerivationUtil = new TypeDerivationUtil(sqlValidator, topSqlNode); operatorTransformerList = SqlCallTransformers.of(new FromUtcTimestampOperatorTransformer(typeDerivationUtil), - new NamedStructOperandTransformer(typeDerivationUtil)); + new NamedStructToCastTransformer(typeDerivationUtil)); } @Override diff --git a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructOperandTransformer.java b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructToCastTransformer.java similarity index 82% rename from coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructOperandTransformer.java rename to coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructToCastTransformer.java index 89ceeb14e..85dcc6b95 100644 --- a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructOperandTransformer.java +++ b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/NamedStructToCastTransformer.java @@ -25,11 +25,13 @@ /** - * Converts Coral's named_struct function to CAST AS ROW(types) function. + * This transformer converts Coral IR function `named_struct` to a Trino compatible representation. + * For example, the SqlCall: `named_struct('abc', 123, 'def', 'xyz')` will be transformed to + * `CAST(ROW(123, 'xyz') AS ROW(`abc` INTEGER, `def` CHAR(3) CHARACTER SET `ISO-8859-1`))` */ -public class NamedStructOperandTransformer extends SqlCallTransformer { +public class NamedStructToCastTransformer extends SqlCallTransformer { - public NamedStructOperandTransformer(TypeDerivationUtil typeDerivationUtil) { + public NamedStructToCastTransformer(TypeDerivationUtil typeDerivationUtil) { super(typeDerivationUtil); } diff --git a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java index 6c90a42a0..af23538fb 100644 --- a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java +++ b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java @@ -326,6 +326,19 @@ public void testIfWithNullAsSecondParameter() { assertEquals(expandedSql, targetSql); } + @Test + public void testNamedStructWithConcat() { + RelNode relNode = TestUtils.getHiveToRelConverter().convertSql( + "SELECT if(FALSE, NULL, named_struct('a', '')) from test.tableB where concat(current_date(), '|', tableB.a) = 'invalid'"); + String targetSql = "SELECT \"if\"(FALSE, NULL, CAST(ROW('') AS ROW(\"a\" CHAR(0))))\n" + + "FROM \"test\".\"tableb\" AS \"tableb\"\n" + + "WHERE \"concat\"(CAST(CURRENT_DATE AS VARCHAR(65535)), '|', CAST(\"tableb\".\"a\" AS VARCHAR(65535))) = 'invalid'"; + + RelToTrinoConverter relToTrinoConverter = TestUtils.getRelToTrinoConverter(); + String expandedSql = relToTrinoConverter.convert(relNode); + assertEquals(expandedSql, targetSql); + } + @Test public void testIfWithNullAsThirdParameter() { RelNode relNode = TestUtils.getHiveToRelConverter().convertSql("SELECT if(FALSE, named_struct('a', ''), NULL)");