@Test public void testGeneralReplace() { SqlParser parser = new SqlParser(); String expr = "a.id>'E100' and a.age<18"; Expression expression = parser.createExpression(expr); RowConstructor constructor = new RowConstructor(); constructor.appendString("'E101'"); constructor.appendString("'Geroge'"); constructor.appendString("'Beijing'"); constructor.appendInt(12); Row row = constructor.build(); String[] col = new String[]{"id", "name", "city", "age"}; List<Column> cols = new ArrayList<>(); for (int i = 0; i < col.length; i++) { Column c = new Column(); c.setColumnName(col[i]); c.setTableName("a"); c.setDataType(DataTypeInt.VARCHAR); if (i == col.length - 1) { c.setDataType(DataTypeInt.INT); } cols.add(c); } boolean match = match(expression, row, cols); System.out.println(match); expr = "a.id>'E200' and a.age<18"; expression = parser.createExpression(expr); match = match(expression, row, cols); System.out.println(match); }
public static Expr generalReplace(Expr e1, Item from, Item to) { Expr e = Expr.clone(e1); if (e instanceof SingleExpr) { SingleExpr se = (SingleExpr) e; Item lv = se.getLvalue(); Item rv = se.getRvalue(); if (lv.equals(from)) { lv = Item.clone(to); } if (rv.equals(from)) { rv = Item.clone(to); } return new SingleExpr(lv, rv, se.getCompareType()); } else if (e instanceof CompositionExpr) { CompositionExpr ce = (CompositionExpr) e; for (int i = 0; i < ce.getConditions().size(); i++) { Expr ex = ce.getConditions().get(i); ce.getConditions().set(i, generalReplace(ex, from, to)); } return ce; } else if (e instanceof UnaryExpr) { UnaryExpr ue = (UnaryExpr) e; return new UnaryExpr(ue.getCompareType(), generalReplace(ue.getExpression(), from, to)); } else if (e instanceof TrueExpr || e instanceof FalseExpr) { return e; } return e; }
Expr implements Serializable { public static Expr generalReplace(Expr e1, Item from, Item to) { Expr e = Expr.clone(e1); if (e instanceof SingleExpr) { SingleExpr se = (SingleExpr) e; Item lv = se.getLvalue(); Item rv = se.getRvalue(); if (lv.equals(from)) { lv = Item.clone(to); } if (rv.equals(from)) { rv = Item.clone(to); } return new SingleExpr(lv, rv, se.getCompareType()); } else if (e instanceof CompositionExpr) { CompositionExpr ce = (CompositionExpr) e; for (int i = 0; i < ce.getConditions().size(); i++) { Expr ex = ce.getConditions().get(i); ce.getConditions().set(i, generalReplace(ex, from, to)); } return ce; } else if (e instanceof UnaryExpr) { UnaryExpr ue = (UnaryExpr) e; return new UnaryExpr(ue.getCompareType(), generalReplace(ue.getExpression(), from, to)); } else if (e instanceof TrueExpr || e instanceof FalseExpr) { return e; } return e; } }
Expr implements Serializable { public static Expr generalReplace(Expr e1, Item from, Item to) { Expr e = Expr.clone(e1); if (e instanceof SingleExpr) { SingleExpr se = (SingleExpr) e; Item lv = se.getLvalue(); Item rv = se.getRvalue(); if (lv.equals(from)) { lv = Item.clone(to); } if (rv.equals(from)) { rv = Item.clone(to); } return new SingleExpr(lv, rv, se.getCompareType()); } else if (e instanceof CompositionExpr) { CompositionExpr ce = (CompositionExpr) e; for (int i = 0; i < ce.getConditions().size(); i++) { Expr ex = ce.getConditions().get(i); ce.getConditions().set(i, generalReplace(ex, from, to)); } return ce; } else if (e instanceof UnaryExpr) { UnaryExpr ue = (UnaryExpr) e; return new UnaryExpr(ue.getCompareType(), generalReplace(ue.getExpression(), from, to)); } else if (e instanceof TrueExpr || e instanceof FalseExpr) { return e; } return e; } Expr(); }
Expr implements Serializable { public static Expr generalReplace(Expr e1, Item from, Item to) { Expr e = Expr.clone(e1); if (e instanceof SingleExpr) { SingleExpr se = (SingleExpr) e; Item lv = se.getLvalue(); Item rv = se.getRvalue(); if (lv.equals(from)) { lv = Item.clone(to); } if (rv.equals(from)) { rv = Item.clone(to); } return new SingleExpr(lv, rv, se.getCompareType()); } else if (e instanceof CompositionExpr) { CompositionExpr ce = (CompositionExpr) e; for (int i = 0; i < ce.getConditions().size(); i++) { Expr ex = ce.getConditions().get(i); ce.getConditions().set(i, generalReplace(ex, from, to)); } return ce; } else if (e instanceof UnaryExpr) { UnaryExpr ue = (UnaryExpr) e; return new UnaryExpr(ue.getCompareType(), generalReplace(ue.getExpression(), from, to)); } else if (e instanceof TrueExpr || e instanceof FalseExpr) { return e; } return e; } Expr(); static Expr replace(Expr e1, ColumnItem from, ColumnItem to); static Expr replaceTableName(Expr e1, String from, String to); static Expr generalReplace(Expr e1, Item from, Item to); static Expr clone(Expr expr); static List<String> extractTableColumn(Expr expr, String tableName); static List<SingleExpr> extractTableJoinExpr(Expr expr); static Expr extractTableColumnFilter(Expr expr, List<String> projectList); static Expr extractTableFilter(Expr expr, String tableName); static Expr parse(Condition cond, String tableName); static Expr optimize(Expr e1, LogicOperator opt); static Expr and(Expr e1, Expr e2, LogicOperator opt); static Expr or(Expr e1, Expr e2, LogicOperator opt); static Expr parse(List<Condition> conditions, String tableName); static Expr parse(Expression expr); abstract String toString(); abstract int hashCode(); abstract boolean equals(Object o); abstract Expression toExpression(); }
Expr implements Serializable { public static Expr generalReplace(Expr e1, Item from, Item to) { Expr e = Expr.clone(e1); if (e instanceof SingleExpr) { SingleExpr se = (SingleExpr) e; Item lv = se.getLvalue(); Item rv = se.getRvalue(); if (lv.equals(from)) { lv = Item.clone(to); } if (rv.equals(from)) { rv = Item.clone(to); } return new SingleExpr(lv, rv, se.getCompareType()); } else if (e instanceof CompositionExpr) { CompositionExpr ce = (CompositionExpr) e; for (int i = 0; i < ce.getConditions().size(); i++) { Expr ex = ce.getConditions().get(i); ce.getConditions().set(i, generalReplace(ex, from, to)); } return ce; } else if (e instanceof UnaryExpr) { UnaryExpr ue = (UnaryExpr) e; return new UnaryExpr(ue.getCompareType(), generalReplace(ue.getExpression(), from, to)); } else if (e instanceof TrueExpr || e instanceof FalseExpr) { return e; } return e; } Expr(); static Expr replace(Expr e1, ColumnItem from, ColumnItem to); static Expr replaceTableName(Expr e1, String from, String to); static Expr generalReplace(Expr e1, Item from, Item to); static Expr clone(Expr expr); static List<String> extractTableColumn(Expr expr, String tableName); static List<SingleExpr> extractTableJoinExpr(Expr expr); static Expr extractTableColumnFilter(Expr expr, List<String> projectList); static Expr extractTableFilter(Expr expr, String tableName); static Expr parse(Condition cond, String tableName); static Expr optimize(Expr e1, LogicOperator opt); static Expr and(Expr e1, Expr e2, LogicOperator opt); static Expr or(Expr e1, Expr e2, LogicOperator opt); static Expr parse(List<Condition> conditions, String tableName); static Expr parse(Expression expr); abstract String toString(); abstract int hashCode(); abstract boolean equals(Object o); abstract Expression toExpression(); static PushDownLaw pdAnd; static PushDownLaw pdOr; static TrueFalseLaw tfLaw; static MinimalItemLaw milaw; }