Skip to content

Commit

Permalink
[CALCITE-5439] Validation of Pivot fails after creating a deep copy o…
Browse files Browse the repository at this point in the history
…f SqlNode
  • Loading branch information
vvysotskyi authored and libenchao committed Dec 28, 2022
1 parent bc4cfa3 commit 81efd2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlPivot.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

/**
* Parse tree node that represents a PIVOT applied to a table reference
* (or sub-query).
Expand Down Expand Up @@ -195,5 +197,16 @@ static class Operator extends SqlSpecialOperator {
Operator(SqlKind kind) {
super(kind.name(), kind);
}

@Override public SqlCall createCall(
@Nullable SqlLiteral functionQualifier,
SqlParserPos pos,
@Nullable SqlNode... operands) {
assert operands.length == 4;
return new SqlPivot(pos, requireNonNull(operands[0], "query"),
requireNonNull((SqlNodeList) operands[1], "aggList"),
requireNonNull((SqlNodeList) operands[2], "axisList"),
requireNonNull((SqlNodeList) operands[3], "inList"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9044,6 +9044,28 @@ private static Consumer<List<? extends Throwable>> checkWarnings(
sql(sql).ok(expected);
}

@Test void testPivotThroughShuttle() {
final String sql = ""
+ "SELECT *\n"
+ "FROM (SELECT job, deptno FROM \"EMP\")\n"
+ "PIVOT (COUNT(*) AS \"COUNT\" FOR deptno IN (10, 50, 20))";
final String expected = ""
+ "SELECT *\n"
+ "FROM (SELECT `JOB`, `DEPTNO`\n"
+ "FROM `EMP`) PIVOT (COUNT(*) AS `COUNT` FOR `DEPTNO` IN (10, 50, 20))";
final SqlNode sqlNode = sql(sql).node();

final SqlNode shuttled = sqlNode.accept(new SqlShuttle() {
@Override public @Nullable SqlNode visit(final SqlCall call) {
// Handler always creates a new copy of 'call'
CallCopyingArgHandler argHandler = new CallCopyingArgHandler(call, true);
call.getOperator().acceptCall(this, call, false, argHandler);
return argHandler.result();
}
});
assertThat(toLinux(shuttled.toString()), is(expected));
}

@Test void testMatchRecognize1() {
final String sql = "select *\n"
+ " from t match_recognize\n"
Expand Down

0 comments on commit 81efd2a

Please sign in to comment.