Skip to content

Commit

Permalink
rename transformer and add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
aastha25 committed Jun 27, 2023
1 parent dc5753a commit 2366b3f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down

0 comments on commit 2366b3f

Please sign in to comment.