Skip to content

Commit

Permalink
[CALCITE-4019] Visit SqlInsert with SqlShuttle cause NullPointerExcep…
Browse files Browse the repository at this point in the history
…tion (Xu ZhaoHui)
  • Loading branch information
xy2953396112 authored and yanlin-Lynn committed Jun 22, 2020
1 parent b184707 commit 4389a9c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 1 addition & 5 deletions core/src/main/java/org/apache/calcite/sql/SqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ public SqlOperator getOperator() {
}

public List<SqlNode> getOperandList() {
if (columnList == null) {
return ImmutableNullableList.of(keywords, targetTable, source);
} else {
return ImmutableNullableList.of(keywords, targetTable, source, columnList);
}
return ImmutableNullableList.of(keywords, targetTable, source, columnList);
}

/** Returns whether this is an UPSERT statement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ public int size() {
}

private static Iterable<SqlParserPos> toPos(Iterable<SqlNode> nodes) {
return Iterables.transform(nodes, SqlNode::getParserPosition);
return Iterables.transform(nodes, node -> {
if (node == null) {
return null;
} else {
return node.getParserPosition();
}
});
}

/**
Expand Down
26 changes: 13 additions & 13 deletions core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5749,19 +5749,6 @@ public void subTestIntervalMonthFailsValidation() {
.ok("INTERVAL '0' MONTH(0)");
}

@Test void testSqlParserPosPlus() throws Exception {
final String sql = "insert into emps select * from emps";
final SqlNode sqlNode = getSqlParser(sql).parseStmt();
final SqlNode sqlNodeVisited = sqlNode.accept(new SqlShuttle() {
@Override public SqlNode visit(SqlIdentifier identifier) {
return new SqlIdentifier(identifier.names,
identifier.getParserPosition());
}
});
assertTrue(sqlNodeVisited != sqlNode);
assertTrue(sqlNodeVisited.getKind() == SqlKind.INSERT);
}

/**
* Runs tests for INTERVAL... DAY that should pass parser but fail
* validator. A substantially identical set of tests exists in
Expand Down Expand Up @@ -5816,6 +5803,19 @@ public void subTestIntervalDayFailsValidation() {
.ok("INTERVAL '0' DAY(0)");
}

@Test void testVisitSqlInsertWithSqlShuttle() throws Exception {
final String sql = "insert into emps select * from emps";
final SqlNode sqlNode = getSqlParser(sql).parseStmt();
final SqlNode sqlNodeVisited = sqlNode.accept(new SqlShuttle() {
@Override public SqlNode visit(SqlIdentifier identifier) {
return new SqlIdentifier(identifier.names,
identifier.getParserPosition());
}
});
assertTrue(sqlNodeVisited != sqlNode);
assertTrue(sqlNodeVisited.getKind() == SqlKind.INSERT);
}

/**
* Runs tests for INTERVAL... DAY TO HOUR that should pass parser but fail
* validator. A substantially identical set of tests exists in
Expand Down

0 comments on commit 4389a9c

Please sign in to comment.