@Test public void testEqualsAST() throws Exception { ASTNode expr1 = parseExpr("T1.a + T2.b - T2.c"); ASTNode expr2 = parseExpr("t1.A + t2.B - t2.C"); Assert.assertTrue(HQLParser.equalsAST(expr1, expr2)); ASTNode literalExpr1 = parseExpr("A = 'FooBar'"); ASTNode literalExpr2 = parseExpr("a = 'FooBar'"); Assert.assertTrue(HQLParser.equalsAST(literalExpr1, literalExpr2)); ASTNode literalExpr3 = parseExpr("A = 'fOObAR'"); Assert.assertFalse(HQLParser.equalsAST(literalExpr1, literalExpr3)); ASTNode literalExpr4 = parseExpr("A <> 'FooBar'"); Assert.assertFalse(HQLParser.equalsAST(literalExpr1, literalExpr4)); }
public static boolean equalsAST(ASTNode n1, ASTNode n2) { if (n1 == null && n2 != null) { return false; } if (n1 != null && n2 == null) { return false; } if (n1 == null) { return true; } if (n1.getType() != n2.getType()) { return false; } if ((n1.getType() == StringLiteral && !StringUtils.equals(n1.getText(), n2.getText()))) { return false; } if (!StringUtils.equalsIgnoreCase(n1.getText(), n2.getText())) { return false; } if (n1.getChildCount() != n2.getChildCount()) { return false; } for (int i = 0; i < n1.getChildCount(); i++) { if (!equalsAST((ASTNode) n1.getChild(i), (ASTNode) n2.getChild(i))) { return false; } } return true; }
HQLParser { public static boolean equalsAST(ASTNode n1, ASTNode n2) { if (n1 == null && n2 != null) { return false; } if (n1 != null && n2 == null) { return false; } if (n1 == null) { return true; } if (n1.getType() != n2.getType()) { return false; } if ((n1.getType() == StringLiteral && !StringUtils.equals(n1.getText(), n2.getText()))) { return false; } if (!StringUtils.equalsIgnoreCase(n1.getText(), n2.getText())) { return false; } if (n1.getChildCount() != n2.getChildCount()) { return false; } for (int i = 0; i < n1.getChildCount(); i++) { if (!equalsAST((ASTNode) n1.getChild(i), (ASTNode) n2.getChild(i))) { return false; } } return true; } }
HQLParser { public static boolean equalsAST(ASTNode n1, ASTNode n2) { if (n1 == null && n2 != null) { return false; } if (n1 != null && n2 == null) { return false; } if (n1 == null) { return true; } if (n1.getType() != n2.getType()) { return false; } if ((n1.getType() == StringLiteral && !StringUtils.equals(n1.getText(), n2.getText()))) { return false; } if (!StringUtils.equalsIgnoreCase(n1.getText(), n2.getText())) { return false; } if (n1.getChildCount() != n2.getChildCount()) { return false; } for (int i = 0; i < n1.getChildCount(); i++) { if (!equalsAST((ASTNode) n1.getChild(i), (ASTNode) n2.getChild(i))) { return false; } } return true; } private  HQLParser(); }
HQLParser { public static boolean equalsAST(ASTNode n1, ASTNode n2) { if (n1 == null && n2 != null) { return false; } if (n1 != null && n2 == null) { return false; } if (n1 == null) { return true; } if (n1.getType() != n2.getType()) { return false; } if ((n1.getType() == StringLiteral && !StringUtils.equals(n1.getText(), n2.getText()))) { return false; } if (!StringUtils.equalsIgnoreCase(n1.getText(), n2.getText())) { return false; } if (n1.getChildCount() != n2.getChildCount()) { return false; } for (int i = 0; i < n1.getChildCount(); i++) { if (!equalsAST((ASTNode) n1.getChild(i), (ASTNode) n2.getChild(i))) { return false; } } return true; } private  HQLParser(); static boolean isTableColumnAST(ASTNode astNode); static boolean isPrimitiveBooleanExpression(ASTNode ast); static boolean isPrimitiveBooleanFunction(ASTNode ast); static ASTNode getDotAST(String tableAlias, String fieldAlias); static ASTNode parseHQL(String query, HiveConf conf); static ASTNode parseExpr(String expr); static ASTNode parseExpr(String expr, Configuration conf); static ASTNode parseExpression(String command, Configuration conf); static void printAST(ASTNode node); static void printAST(Map<Integer, String> tokenMapping, ASTNode node, int level, int child); static Map<Integer, String> getHiveTokenMapping(); static ASTNode findNodeByPath(ASTNode root, int... path); static void bft(ASTNode root, ASTNodeVisitor visitor); static void toInfixString(ASTNode root, StringBuilder buf); static void toInfixString(ASTNode root, StringBuilder buf, AppendMode appendMode); static String getString(ASTNode tree, AppendMode appendMode); static String getString(ASTNode tree); static String getColName(ASTNode node); static Set<String> getColsInExpr(final String tableAlias, ASTNode expr); static boolean isAggregateAST(ASTNode node); static boolean isNonAggregateFunctionAST(ASTNode node); static boolean isSelectASTNode(final ASTNode node); static boolean hasAggregate(ASTNode node); static boolean equalsAST(ASTNode n1, ASTNode n2); static ASTNode leftMostChild(ASTNode node); }
HQLParser { public static boolean equalsAST(ASTNode n1, ASTNode n2) { if (n1 == null && n2 != null) { return false; } if (n1 != null && n2 == null) { return false; } if (n1 == null) { return true; } if (n1.getType() != n2.getType()) { return false; } if ((n1.getType() == StringLiteral && !StringUtils.equals(n1.getText(), n2.getText()))) { return false; } if (!StringUtils.equalsIgnoreCase(n1.getText(), n2.getText())) { return false; } if (n1.getChildCount() != n2.getChildCount()) { return false; } for (int i = 0; i < n1.getChildCount(); i++) { if (!equalsAST((ASTNode) n1.getChild(i), (ASTNode) n2.getChild(i))) { return false; } } return true; } private  HQLParser(); static boolean isTableColumnAST(ASTNode astNode); static boolean isPrimitiveBooleanExpression(ASTNode ast); static boolean isPrimitiveBooleanFunction(ASTNode ast); static ASTNode getDotAST(String tableAlias, String fieldAlias); static ASTNode parseHQL(String query, HiveConf conf); static ASTNode parseExpr(String expr); static ASTNode parseExpr(String expr, Configuration conf); static ASTNode parseExpression(String command, Configuration conf); static void printAST(ASTNode node); static void printAST(Map<Integer, String> tokenMapping, ASTNode node, int level, int child); static Map<Integer, String> getHiveTokenMapping(); static ASTNode findNodeByPath(ASTNode root, int... path); static void bft(ASTNode root, ASTNodeVisitor visitor); static void toInfixString(ASTNode root, StringBuilder buf); static void toInfixString(ASTNode root, StringBuilder buf, AppendMode appendMode); static String getString(ASTNode tree, AppendMode appendMode); static String getString(ASTNode tree); static String getColName(ASTNode node); static Set<String> getColsInExpr(final String tableAlias, ASTNode expr); static boolean isAggregateAST(ASTNode node); static boolean isNonAggregateFunctionAST(ASTNode node); static boolean isSelectASTNode(final ASTNode node); static boolean hasAggregate(ASTNode node); static boolean equalsAST(ASTNode n1, ASTNode n2); static ASTNode leftMostChild(ASTNode node); static final Pattern P_WSPACE; static final Set<Integer> BINARY_OPERATORS; static final Set<Integer> N_ARY_OPERATORS; static final Set<Integer> FILTER_OPERATORS; static final Set<Integer> ARITHMETIC_OPERATORS; static final Set<Integer> UNARY_OPERATORS; static final Set<Integer> PRIMITIVE_TYPES; }