Skip to content

Commit

Permalink
Make Coral IR friendly to missng table alias prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiqiang Ding authored and Yiqiang Ding committed Sep 7, 2023
1 parent 64ec9b5 commit 79cbc5e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.linkedin.coral.common;

import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.StructKind;

import java.util.List;

public class CoralJavaTypeFactoryImpl extends JavaTypeFactoryImpl {
public CoralJavaTypeFactoryImpl(RelDataTypeSystem typeSystem) {
super(typeSystem);
}

@Override
public RelDataType createStructType(
final List<RelDataType> typeList,
final List<String> fieldNameList) {
return createStructType(StructKind.PEEK_FIELDS, typeList, fieldNameList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.linkedin.coral.common;

import org.apache.calcite.plan.Context;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptSchema;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.RelBuilder;

public class HiveCommonRelBuilder extends RelBuilder {
private HiveCommonRelBuilder(Context context, RelOptCluster cluster, RelOptSchema relOptSchema) {
super(context, cluster, relOptSchema);
}

public static RelBuilder create(FrameworkConfig config) {
return Frameworks.withPrepare(config, (cluster, relOptSchema, rootSchema,
statement) -> { cluster = RelOptCluster.create(cluster.getPlanner(),
new RexBuilder(new CoralJavaTypeFactoryImpl(cluster.getTypeFactory().getTypeSystem())));
return new HiveCommonRelBuilder(config.getContext(), cluster, relOptSchema);});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected RelBuilder getRelBuilder() {
// 2. Converted expression is harder to validate for correctness(because it appears different from input)
if (relBuilder == null) {
Hook.REL_BUILDER_SIMPLIFY.add(Hook.propertyJ(false));
relBuilder = RelBuilder.create(config);
relBuilder = HiveCommonRelBuilder.create(config);
}
return relBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,16 @@ public void testSqlSelectAliasAppenderTransformer() {
+ "WHERE \"tablea\".\"a\" > 5";
assertEquals(expandedSql, expected);
}

@Test
public void testSqlSelectAliasAppenderTransformerWithoutTableAlias() {
// test.tableA(a int, b struct<b1:string>
RelNode relNode = TestUtils.getHiveToRelConverter().convertSql("SELECT b.b1 FROM test.tableA where a > 5");
RelToTrinoConverter relToTrinoConverter = TestUtils.getRelToTrinoConverter();
String expandedSql = relToTrinoConverter.convert(relNode);

String expected = "SELECT \"tablea\".\"b\".\"b1\" AS \"b1\"\n" + "FROM \"test\".\"tablea\" AS \"tablea\"\n"
+ "WHERE \"tablea\".\"a\" > 5";
assertEquals(expandedSql, expected);
}
}

0 comments on commit 79cbc5e

Please sign in to comment.