FilterSpliter { public static Map<FilterType, IFilter> splitByIndex(List<IFilter> DNFNode, TableNode table) { Map<FilterType, IFilter> filters = new HashMap(); Map<Object, List<IFilter>> columnAndItsFilters = FilterUtils.toColumnFiltersMap(DNFNode); IndexMeta index = table.getIndexUsed(); if (index == null) { index = table.getTableMeta().getPrimaryIndex(); } List<ISelectable> indexKeyColumns = new ArrayList(0); List<ISelectable> indexValueColumns = new ArrayList(0); if (index != null) { indexKeyColumns = OptimizerUtils.columnMetaListToIColumnList(index.getKeyColumns(), table.getTableName()); indexValueColumns = OptimizerUtils.columnMetaListToIColumnList(index.getValueColumns(), table.getTableName()); } List<IFilter> indexQueryKeyFilters = new LinkedList(); List<IFilter> indexQueryValueFilters = new LinkedList(); List<IFilter> resultFilters = new LinkedList(DNFNode); for (int i = 0; i < indexKeyColumns.size(); i++) { List<IFilter> fs = columnAndItsFilters.get(indexKeyColumns.get(i)); if (fs != null) { for (IFilter f : fs) { if (f instanceof IGroupFilter) { indexQueryValueFilters.add(f); continue; } if (((IBooleanFilter) f).getValue() != null && (((IBooleanFilter) f).getValue() instanceof ISelectable)) { continue; } if (f != null && f.getOperation() != OPERATION.NOT_EQ && f.getOperation() != OPERATION.IS_NOT_NULL && f.getOperation() != OPERATION.IS_NULL && f.getOperation() != OPERATION.LIKE) { indexQueryKeyFilters.add(f); } else { indexQueryValueFilters.add(f); } } fs.clear(); } } for (int i = 0; i < indexValueColumns.size(); i++) { List<IFilter> fs = columnAndItsFilters.get(indexValueColumns.get(i)); if (fs != null) { for (IFilter f : fs) { if (f instanceof IBooleanFilter && ((IBooleanFilter) f).getValue() != null && (((IBooleanFilter) f).getValue() instanceof ISelectable)) { continue; } indexQueryValueFilters.add(f); } fs.clear(); } } resultFilters.removeAll(indexQueryKeyFilters); resultFilters.removeAll(indexQueryValueFilters); IFilter indexQueryKeyTree = FilterUtils.DNFToAndLogicTree(indexQueryKeyFilters); IFilter indexQueryValueTree = FilterUtils.DNFToAndLogicTree(indexQueryValueFilters); IFilter resultTree = FilterUtils.DNFToAndLogicTree(resultFilters); filters.put(FilterType.IndexQueryKeyFilter, indexQueryKeyTree); filters.put(FilterType.IndexQueryValueFilter, indexQueryValueTree); filters.put(FilterType.ResultFilter, resultTree); return filters; }  static List<QueryTreeNode> splitByDNF(TableNode node, Map<String, Object> extraCmd); static Map<FilterType, IFilter> splitByIndex(List<IFilter> DNFNode, TableNode table);  }
@Test public void test_主键索引条件() { TableNode table = new TableNode("TABLE1"); table.query("ID = 1"); build(table); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals("TABLE1.ID = 1", result.get(FilterType.IndexQueryKeyFilter).toString()); Assert.assertEquals(null, result.get(FilterType.IndexQueryValueFilter)); Assert.assertEquals(null, result.get(FilterType.ResultFilter)); }
@Test public void test_主键索引GroupOr条件() { TableNode table = new TableNode("TABLE1"); table.query("ID = 1 OR ID > 2"); build(table); FilterPreProcessor.optimize(table, true, null); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals(null, result.get(FilterType.IndexQueryKeyFilter)); Assert.assertEquals("(TABLE1.ID = 1 OR TABLE1.ID > 2)", result.get(FilterType.IndexQueryValueFilter).toString()); Assert.assertEquals(null, result.get(FilterType.ResultFilter)); }
@Test public void test_主键索引条件_特殊条件不走索引() { TableNode table = new TableNode("TABLE1"); table.query("ID != 1 AND ID IS NULL AND ID IS NOT NULL AND ID LIKE '%A' AND ID = 2"); build(table); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals("TABLE1.ID = 2", result.get(FilterType.IndexQueryKeyFilter).toString()); Assert.assertEquals("(TABLE1.ID != 1 AND TABLE1.ID IS NULL AND TABLE1.ID IS NOT NULL AND TABLE1.ID LIKE %A)", result.get(FilterType.IndexQueryValueFilter).toString()); Assert.assertEquals(null, result.get(FilterType.ResultFilter)); }
@Test public void test_二级索引条件() { TableNode table = new TableNode("TABLE1"); table.query("NAME = 1"); build(table); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals("TABLE1.NAME = 1", result.get(FilterType.IndexQueryKeyFilter).toString()); Assert.assertEquals(null, result.get(FilterType.IndexQueryValueFilter)); Assert.assertEquals(null, result.get(FilterType.ResultFilter)); }
@Test public void test_混合索引条件() { TableNode table = new TableNode("TABLE1"); table.query("NAME = 1 AND SCHOOL = 2 AND ID > 3"); build(table); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals("TABLE1.NAME = 1", result.get(FilterType.IndexQueryKeyFilter).toString()); Assert.assertEquals("TABLE1.ID > 3", result.get(FilterType.IndexQueryValueFilter).toString()); Assert.assertEquals("TABLE1.SCHOOL = 2", result.get(FilterType.ResultFilter).toString()); }
@Test public void test_无索引条件() { TableNode table = new TableNode("TABLE1"); table.query("SCHOOL = 2"); build(table); Map<FilterType, IFilter> result = FilterSpliter.splitByIndex(FilterUtils.toDNFNode(table.getWhereFilter()), table); Assert.assertEquals(null, result.get(FilterType.IndexQueryKeyFilter)); Assert.assertEquals("TABLE1.SCHOOL = 2", result.get(FilterType.IndexQueryValueFilter).toString()); Assert.assertEquals(null, result.get(FilterType.ResultFilter)); }