Skip to content

Commit

Permalink
[CALCITE-3246] NullPointerException while deserializing udf operator …
Browse files Browse the repository at this point in the history
…(Wang Yanlin).
  • Loading branch information
yanlin-Lynn authored and chunweilei committed Dec 6, 2019
1 parent ff44204 commit 1268950
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,15 @@ RexNode toRex(RelInput relInput, Object o) {
final Map<String, Object> opMap = (Map) map.get("op");
final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
if (opMap != null) {
final String op = (String) opMap.get("name");
if (map.containsKey("class")) {
opMap.put("class", map.get("class"));
}
final List operands = (List) map.get("operands");
final List<RexNode> rexOperands = toRexList(relInput, operands);
final Object jsonType = map.get("type");
final Map window = (Map) map.get("window");
if (window != null) {
final SqlAggFunction operator = toAggregation(relInput, op, opMap);
final SqlAggFunction operator = toAggregation(opMap);
final RelDataType type = toType(typeFactory, jsonType);
List<RexNode> partitionKeys = new ArrayList<>();
if (window.containsKey("partition")) {
Expand Down Expand Up @@ -492,7 +494,7 @@ RexNode toRex(RelInput relInput, Object o) {
ImmutableList.copyOf(orderKeys), lowerBound, upperBound, physical,
true, false, distinct, false);
} else {
final SqlOperator operator = toOp(relInput, opMap);
final SqlOperator operator = toOp(opMap);
final RelDataType type;
if (jsonType != null) {
type = toType(typeFactory, jsonType);
Expand Down Expand Up @@ -632,7 +634,7 @@ private List<RexNode> toRexList(RelInput relInput, List operands) {
return list;
}

SqlOperator toOp(RelInput relInput, Map<String, Object> map) {
SqlOperator toOp(Map<String, Object> map) {
// in case different operator has the same kind, check with both name and kind.
String name = map.get("name").toString();
String kind = map.get("kind").toString();
Expand All @@ -658,8 +660,8 @@ SqlOperator toOp(RelInput relInput, Map<String, Object> map) {
return null;
}

SqlAggFunction toAggregation(RelInput relInput, String agg, Map<String, Object> map) {
return (SqlAggFunction) toOp(relInput, map);
SqlAggFunction toAggregation(Map<String, Object> map) {
return (SqlAggFunction) toOp(map);
}

private Map toJson(SqlOperator operator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public ImmutableList<RexLiteral> getTuple(List jsonTuple) {
private AggregateCall toAggCall(RelInput relInput, Map<String, Object> jsonAggCall) {
final Map<String, Object> aggMap = (Map) jsonAggCall.get("agg");
final SqlAggFunction aggregation =
relJson.toAggregation(relInput, (String) aggMap.get("name"), aggMap);
relJson.toAggregation(aggMap);
final Boolean distinct = (Boolean) jsonAggCall.get("distinct");
@SuppressWarnings("unchecked")
final List<Integer> operands = (List<Integer>) jsonAggCall.get("operands");
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.test.JdbcTest;
import org.apache.calcite.test.MockSqlOperatorTable;
import org.apache.calcite.test.RelBuilderTest;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
Expand Down Expand Up @@ -742,6 +743,24 @@ public class RelWriterTest {
assertThat(s, isLinux(expected));
}

@Test public void testUdf() {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
final RelNode rel = builder
.scan("EMP")
.project(
builder.call(new MockSqlOperatorTable.MyFunction(),
builder.field("EMPNO")))
.build();
String relJson = RelOptUtil.dumpPlan("", rel,
SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
final String expected = ""
+ "LogicalProject($f0=[MYFUN($0)])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n";
assertThat(s, isLinux(expected));
}

/** Returns the schema of a {@link org.apache.calcite.rel.core.TableScan}
* in this plan, or null if there are no scans. */
private RelOptSchema getSchema(RelNode rel) {
Expand Down

0 comments on commit 1268950

Please sign in to comment.