Skip to content

Commit

Permalink
[CALCITE-6323] Serialize return type during RelJson.toJson(RexNode no…
Browse files Browse the repository at this point in the history
…de) for SqlKind.SAFE_CAST
  • Loading branch information
tanclary committed Mar 12, 2024
1 parent 325b1ba commit b412fa4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ public Object toJson(RexNode node) {
switch (node.getKind()) {
case MINUS:
case CAST:
case SAFE_CAST:
map.put("type", toJson(node.getType()));
break;
default:
Expand Down
30 changes: 30 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 @@ -1091,6 +1091,36 @@ void testAggregateWithAlias(SqlExplainFormat format) {
assertThat(result, isLinux(expected));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6323">[CALCITE-6323]</a>
*
* <p>Before the fix, RelJson.toRex would throw an ArrayIndexOutOfBounds error
* when deserializing SAFE_CAST due to type inference requiring 2 operands.
*
* <p>The solution is to add in 'type' when serializing to JSON.
*/
@Test void testDeserializeSafeCastOperator() {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
final RexBuilder rb = builder.getRexBuilder();
final RelDataTypeFactory typeFactory = rb.getTypeFactory();
final RelDataType type = typeFactory.createSqlType(SqlTypeName.DATE);
final RelNode rel = builder
.scan("EMP")
.project(builder.field("JOB"),
rb.makeCall(type, SqlLibraryOperators.SAFE_CAST,
ImmutableList.of(builder.field("SAL")))).build();
final RelJsonWriter jsonWriter =
new RelJsonWriter(new JsonBuilder(), RelJson::withLibraryOperatorTable);
rel.explain(jsonWriter);
String relJson = jsonWriter.asString();
String result = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
final String expected =
"LogicalProject(JOB=[$2], $f1=[SAFE_CAST($5)])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n";
assertThat(result, isLinux(expected));
}

@Test void testAggregateWithoutAlias() {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
Expand Down

0 comments on commit b412fa4

Please sign in to comment.