Walks over a org.apache.calcite.sql.SqlNode tree and returns the ancestry stack when it finds a given node.
| 1370 | /** Walks over a {@link org.apache.calcite.sql.SqlNode} tree and returns the |
| 1371 | * ancestry stack when it finds a given node. */ |
| 1372 | private static class Genealogist extends SqlBasicVisitor<Void> { |
| 1373 | private final List<SqlNode> ancestors = new ArrayList<>(); |
| 1374 | private final Predicate<SqlNode> predicate; |
| 1375 | private final Predicate<SqlNode> postPredicate; |
| 1376 | |
| 1377 | Genealogist(Predicate<SqlNode> predicate, |
| 1378 | Predicate<SqlNode> postPredicate) { |
| 1379 | this.predicate = predicate; |
| 1380 | this.postPredicate = postPredicate; |
| 1381 | } |
| 1382 | |
| 1383 | private Void check(SqlNode node) { |
| 1384 | preCheck(node); |
| 1385 | postCheck(node); |
| 1386 | return null; |
| 1387 | } |
| 1388 | |
| 1389 | private Void preCheck(SqlNode node) { |
| 1390 | if (predicate.test(node)) { |
| 1391 | throw new Util.FoundOne(ImmutableList.copyOf(ancestors)); |
| 1392 | } |
| 1393 | return null; |
| 1394 | } |
| 1395 | |
| 1396 | private Void postCheck(SqlNode node) { |
| 1397 | if (postPredicate.test(node)) { |
| 1398 | throw new Util.FoundOne(ImmutableList.copyOf(ancestors)); |
| 1399 | } |
| 1400 | return null; |
| 1401 | } |
| 1402 | |
| 1403 | private void visitChild(@Nullable SqlNode node) { |
| 1404 | if (node == null) { |
| 1405 | return; |
| 1406 | } |
| 1407 | ancestors.add(node); |
| 1408 | node.accept(this); |
| 1409 | ancestors.remove(ancestors.size() - 1); |
| 1410 | } |
| 1411 | |
| 1412 | @Override public Void visit(SqlIdentifier id) { |
| 1413 | return check(id); |
| 1414 | } |
| 1415 | |
| 1416 | @Override public Void visit(SqlCall call) { |
| 1417 | preCheck(call); |
| 1418 | for (SqlNode node : call.getOperandList()) { |
| 1419 | visitChild(node); |
| 1420 | } |
| 1421 | return postCheck(call); |
| 1422 | } |
| 1423 | |
| 1424 | @Override public Void visit(SqlIntervalQualifier intervalQualifier) { |
| 1425 | return check(intervalQualifier); |
| 1426 | } |
| 1427 | |
| 1428 | @Override public Void visit(SqlLiteral literal) { |
| 1429 | return check(literal); |
nothing calls this directly
no outgoing calls
no test coverage detected