|
| 1 | +package sqlancer.databend; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import sqlancer.common.ast.newast.ColumnReferenceNode; |
| 6 | +import sqlancer.common.ast.newast.NewAliasNode; |
| 7 | +import sqlancer.common.ast.newast.NewBetweenOperatorNode; |
| 8 | +import sqlancer.common.ast.newast.NewBinaryOperatorNode; |
| 9 | +import sqlancer.common.ast.newast.NewFunctionNode; |
| 10 | +import sqlancer.common.ast.newast.NewInOperatorNode; |
| 11 | +import sqlancer.common.ast.newast.NewOrderingTerm; |
| 12 | +import sqlancer.common.ast.newast.NewPostfixTextNode; |
| 13 | +import sqlancer.common.ast.newast.NewUnaryPostfixOperatorNode; |
| 14 | +import sqlancer.common.ast.newast.NewUnaryPrefixOperatorNode; |
| 15 | +import sqlancer.common.ast.newast.Node; |
| 16 | +import sqlancer.common.ast.newast.TableReferenceNode; |
| 17 | +import sqlancer.databend.DatabendSchema.DatabendColumn; |
| 18 | +import sqlancer.databend.ast.DatabendConstant; |
| 19 | +import sqlancer.databend.ast.DatabendExpression; |
| 20 | +import sqlancer.databend.ast.DatabendJoin; |
| 21 | +import sqlancer.databend.ast.DatabendSelect; |
| 22 | + |
| 23 | +public class DatabendExpectedValueVisitor { |
| 24 | + |
| 25 | + protected final StringBuilder sb = new StringBuilder(); |
| 26 | + |
| 27 | + private void print(Node<DatabendExpression> expr) { |
| 28 | + sb.append(DatabendToStringVisitor.asString(expr)); |
| 29 | + sb.append(" -- "); |
| 30 | + sb.append(((DatabendExpression) expr).getExpectedValue()); |
| 31 | + sb.append("\n"); |
| 32 | + } |
| 33 | + |
| 34 | + @SuppressWarnings("unchecked") |
| 35 | + public void visit(Node<DatabendExpression> expr) { |
| 36 | + assert expr != null; |
| 37 | + if (expr instanceof ColumnReferenceNode<?, ?>) { |
| 38 | + visit((ColumnReferenceNode<DatabendExpression, DatabendColumn>) expr); |
| 39 | + } else if (expr instanceof NewUnaryPostfixOperatorNode<?>) { |
| 40 | + visit((NewUnaryPostfixOperatorNode<DatabendExpression>) expr); |
| 41 | + } else if (expr instanceof NewUnaryPrefixOperatorNode<?>) { |
| 42 | + visit((NewUnaryPrefixOperatorNode<DatabendExpression>) expr); |
| 43 | + } else if (expr instanceof NewBinaryOperatorNode<?>) { |
| 44 | + visit((NewBinaryOperatorNode<DatabendExpression>) expr); |
| 45 | + } else if (expr instanceof TableReferenceNode<?, ?>) { |
| 46 | + visit((TableReferenceNode<DatabendExpression, ?>) expr); |
| 47 | + } else if (expr instanceof NewFunctionNode<?, ?>) { |
| 48 | + visit((NewFunctionNode<DatabendExpression, ?>) expr); |
| 49 | + } else if (expr instanceof NewBetweenOperatorNode<?>) { |
| 50 | + visit((NewBetweenOperatorNode<DatabendExpression>) expr); |
| 51 | + } else if (expr instanceof NewInOperatorNode<?>) { |
| 52 | + visit((NewInOperatorNode<DatabendExpression>) expr); |
| 53 | + } else if (expr instanceof NewOrderingTerm<?>) { |
| 54 | + visit((NewOrderingTerm<DatabendExpression>) expr); |
| 55 | + } else if (expr instanceof NewAliasNode<?>) { |
| 56 | + visit((NewAliasNode<DatabendExpression>) expr); |
| 57 | + } else if (expr instanceof NewPostfixTextNode<?>) { |
| 58 | + visit((NewPostfixTextNode<DatabendExpression>) expr); |
| 59 | + } else if (expr instanceof DatabendConstant) { |
| 60 | + visit((DatabendConstant) expr); |
| 61 | + } else if (expr instanceof DatabendSelect) { |
| 62 | + visit((DatabendSelect) expr); |
| 63 | + } else if (expr instanceof DatabendJoin) { |
| 64 | + visit((DatabendJoin) expr); |
| 65 | + } else { |
| 66 | + throw new AssertionError(expr); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void visit(ColumnReferenceNode<DatabendExpression, DatabendColumn> c) { |
| 71 | + print(c); |
| 72 | + } |
| 73 | + |
| 74 | + public void visit(NewUnaryPostfixOperatorNode<DatabendExpression> op) { |
| 75 | + print(op); |
| 76 | + visit(op.getExpr()); |
| 77 | + } |
| 78 | + |
| 79 | + public void visit(NewUnaryPrefixOperatorNode<DatabendExpression> op) { |
| 80 | + print(op); |
| 81 | + visit(op.getExpr()); |
| 82 | + } |
| 83 | + |
| 84 | + public void visit(NewBinaryOperatorNode<DatabendExpression> op) { |
| 85 | + print(op); |
| 86 | + visit(op.getLeft()); |
| 87 | + visit(op.getRight()); |
| 88 | + } |
| 89 | + |
| 90 | + public void visit(TableReferenceNode<DatabendExpression, ?> t) { |
| 91 | + print(t); |
| 92 | + } |
| 93 | + |
| 94 | + public void visit(NewFunctionNode<DatabendExpression, ?> fun) { |
| 95 | + print(fun); |
| 96 | + visit(fun.getArgs()); |
| 97 | + } |
| 98 | + |
| 99 | + public void visit(List<Node<DatabendExpression>> expressions) { |
| 100 | + for (Node<DatabendExpression> expression : expressions) { |
| 101 | + visit(expression); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void visit(NewBetweenOperatorNode<DatabendExpression> op) { |
| 106 | + print(op); |
| 107 | + visit(op.getLeft()); |
| 108 | + visit(op.getMiddle()); |
| 109 | + visit(op.getRight()); |
| 110 | + } |
| 111 | + |
| 112 | + public void visit(NewInOperatorNode<DatabendExpression> op) { |
| 113 | + print(op); |
| 114 | + visit(op.getLeft()); |
| 115 | + visit(op.getRight()); |
| 116 | + } |
| 117 | + |
| 118 | + public void visit(NewOrderingTerm<DatabendExpression> op) { |
| 119 | + print(op); |
| 120 | + visit(op.getExpr()); |
| 121 | + } |
| 122 | + |
| 123 | + public void visit(NewAliasNode<DatabendExpression> op) { |
| 124 | + print(op); |
| 125 | + visit(op.getExpr()); |
| 126 | + } |
| 127 | + |
| 128 | + public void visit(NewPostfixTextNode<DatabendExpression> postFixText) { |
| 129 | + print(postFixText); |
| 130 | + visit(postFixText.getExpr()); |
| 131 | + } |
| 132 | + |
| 133 | + public void visit(DatabendConstant constant) { |
| 134 | + print(constant); |
| 135 | + } |
| 136 | + |
| 137 | + public void visit(DatabendSelect select) { |
| 138 | + print(select.getWhereClause()); |
| 139 | + } |
| 140 | + |
| 141 | + public void visit(DatabendJoin join) { |
| 142 | + print(join.getOnCondition()); |
| 143 | + } |
| 144 | + |
| 145 | + public String get() { |
| 146 | + return sb.toString(); |
| 147 | + } |
| 148 | + |
| 149 | + public static String asExpectedValues(Node<DatabendExpression> expr) { |
| 150 | + DatabendExpectedValueVisitor v = new DatabendExpectedValueVisitor(); |
| 151 | + v.visit(expr); |
| 152 | + return v.get(); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments