/** 如果是1 = id的情况,转化为id = 1 */ private static IFilter exchage(IFilter root) { IBooleanFilter bf = (IBooleanFilter) root; if (!FilterUtils.isConstValue(bf.getValue()) && FilterUtils.isConstValue(bf.getColumn())) { Object val = bf.getColumn(); bf.setColumn(bf.getValue()); bf.setValue(val); OPERATION newOp = bf.getOperation(); switch (bf.getOperation()) { case GT: newOp = OPERATION.LT; break; case LT: newOp = OPERATION.GT; break; case GT_EQ: newOp = OPERATION.LT_EQ; break; case LT_EQ: newOp = OPERATION.GT_EQ; break; default: break; } bf.setOperation(newOp); } return bf; }
/** 将0=1/1=1/true的恒等式进行优化 */ private static IFilter shortestFilter(IFilter root) throws EmptyResultFilterException { IFilter filter = FilterUtils.toDNFAndFlat(root); List<List<IFilter>> DNFfilter = FilterUtils.toDNFNodesArray(filter); List<List<IFilter>> newDNFfilter = new ArrayList<List<IFilter>>(); for (List<IFilter> andDNFfilter : DNFfilter) { boolean isShortest = false; List<IFilter> newAndDNFfilter = new ArrayList<IFilter>(); for (IFilter one : andDNFfilter) { if (one.getOperation() == OPERATION.CONSTANT) { boolean flag = false; if (((IBooleanFilter) one).getColumn() instanceof ISelectable) { // 可能是个not函数 newAndDNFfilter.add(one); // 不能丢弃 } else { String value = ((IBooleanFilter) one).getColumn().toString(); if (StringUtils.isNumeric(value)) { flag = BooleanUtils.toBoolean(Integer.valueOf(value)); } else { flag = BooleanUtils.toBoolean(((IBooleanFilter) one).getColumn().toString()); } if (!flag) { isShortest = true; break; } } } else { newAndDNFfilter.add(one); } } if (!isShortest) { if (newAndDNFfilter.isEmpty()) { // 代表出现为true or xxx,直接返回true IBooleanFilter f = ASTNodeFactory.getInstance().createBooleanFilter(); f.setOperation(OPERATION.CONSTANT); f.setColumn("1"); f.setColumnName(ObjectUtils.toString("1")); return f; } else { // 针对非false的情况 newDNFfilter.add(newAndDNFfilter); } } } if (newDNFfilter.isEmpty()) { throw new EmptyResultFilterException("空结果"); } return FilterUtils.DNFToOrLogicTree(newDNFfilter); }
private static IFilter processFilter(IFilter root) { if (root == null) { return null; } root = shortestFilter(root); // 短路一下 root = processOneFilter(root); // 做一下转换处理 root = FilterUtils.merge(root); // 合并一下flter return root; }
private static IFilter typeConvert(IFilter root) { IBooleanFilter bf = (IBooleanFilter) root; // 如果是id in (xx) if (bf.getValues() != null && bf.getColumn() instanceof ISelectable) { for (int i = 0; i < bf.getValues().size(); i++) { bf.getValues() .set( i, OptimizerUtils.convertType( bf.getValues().get(i), ((ISelectable) bf.getColumn()).getDataType())); } } else { // 如果是 1 = id情况 if (FilterUtils.isConstValue(bf.getColumn()) && !FilterUtils.isConstValue(bf.getValue())) { DATA_TYPE type = null; if (bf.getValue() instanceof IColumn) { type = ((IColumn) bf.getValue()).getDataType(); } if (bf.getValue() instanceof IFunction) { type = ((IFunction) bf.getValue()).getDataType(); } bf.setColumn(OptimizerUtils.convertType(bf.getColumn(), type)); } // 如果是 id = 1情况 if (FilterUtils.isConstValue(bf.getValue()) && !FilterUtils.isConstValue(bf.getColumn())) { DATA_TYPE type = null; if (bf.getColumn() instanceof IColumn) { type = ((IColumn) bf.getColumn()).getDataType(); } if (bf.getColumn() instanceof IFunction) { type = ((IFunction) bf.getColumn()).getDataType(); } bf.setValue(OptimizerUtils.convertType(bf.getValue(), type)); } } return bf; }
private List<IFilter> toDNFFilter(IFilter where) { return FilterUtils.toDNFNodesArray(where).get(0); }