@Test public void testMergeEqInFilter() { String query = "SELECT * FROM testTable WHERE int IN (1, 1) AND (long IN (2, 3) OR long IN (3, 4) OR long = 2) AND (float = 3.5 OR double IN (1.1, 1.2) OR float = 4.5 OR float > 5.5 OR double = 1.3)"; BrokerRequest sqlBrokerRequest = SQL_COMPILER.compileToBrokerRequest(query); PinotQuery pinotQuery = sqlBrokerRequest.getPinotQuery(); BrokerRequest pqlBrokerRequest = PQL_COMPILER.compileToBrokerRequest(query); for (BrokerRequest brokerRequest : Arrays.asList(sqlBrokerRequest, pqlBrokerRequest)) { OPTIMIZER.optimize(brokerRequest, SCHEMA); FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(brokerRequest.getFilterQuery().getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); assertEquals(filterQueryTree.getOperator(), FilterOperator.AND); List<FilterQueryTree> children = filterQueryTree.getChildren(); assertEquals(children.size(), 3); assertEquals(children.get(0).toString(), "int EQUALITY [1]"); FilterQueryTree secondChild = children.get(1); assertEquals(secondChild.getColumn(), "long"); assertEquals(secondChild.getOperator(), FilterOperator.IN); assertEqualsNoOrder(secondChild.getValue().toArray(), new Object[]{"2", "3", "4"}); FilterQueryTree thirdChild = children.get(2); assertEquals(thirdChild.getOperator(), FilterOperator.OR); List<FilterQueryTree> orFilterChildren = thirdChild.getChildren(); assertEquals(orFilterChildren.size(), 3); assertEquals(orFilterChildren.get(0).toString(), "float RANGE [(5.5\t\t*)]"); FilterQueryTree secondOrFilterChild = orFilterChildren.get(1); assertEquals(secondOrFilterChild.getOperator(), FilterOperator.IN); FilterQueryTree thirdOrFilterChild = orFilterChildren.get(2); assertEquals(thirdOrFilterChild.getOperator(), FilterOperator.IN); if (secondOrFilterChild.getColumn().equals("float")) { assertEqualsNoOrder(secondOrFilterChild.getValue().toArray(), new Object[]{"3.5", "4.5"}); assertEquals(thirdOrFilterChild.getColumn(), "double"); assertEqualsNoOrder(thirdOrFilterChild.getValue().toArray(), new Object[]{"1.1", "1.2", "1.3"}); } else { assertEquals(secondOrFilterChild.getColumn(), "double"); assertEqualsNoOrder(secondOrFilterChild.getValue().toArray(), new Object[]{"1.1", "1.2", "1.3"}); assertEquals(thirdOrFilterChild.getColumn(), "float"); assertEqualsNoOrder(thirdOrFilterChild.getValue().toArray(), new Object[]{"3.5", "4.5"}); } } OPTIMIZER.optimize(pinotQuery, SCHEMA); Function filterFunction = pinotQuery.getFilterExpression().getFunctionCall(); assertEquals(filterFunction.getOperator(), FilterKind.AND.name()); List<Expression> children = filterFunction.getOperands(); assertEquals(children.size(), 3); assertEquals(children.get(0), getEqFilterExpression("int", 1)); checkInFilterFunction(children.get(1).getFunctionCall(), "long", Arrays.asList(2, 3, 4)); Function thirdChildFunction = children.get(2).getFunctionCall(); assertEquals(thirdChildFunction.getOperator(), FilterKind.OR.name()); List<Expression> thirdChildChildren = thirdChildFunction.getOperands(); assertEquals(thirdChildChildren.size(), 3); assertEquals(thirdChildChildren.get(0).getFunctionCall().getOperator(), FilterKind.GREATER_THAN.name()); Function secondGrandChildFunction = thirdChildChildren.get(1).getFunctionCall(); assertEquals(secondGrandChildFunction.getOperator(), FilterKind.IN.name()); Function thirdGrandChildFunction = thirdChildChildren.get(2).getFunctionCall(); assertEquals(thirdGrandChildFunction.getOperator(), FilterKind.IN.name()); if (secondGrandChildFunction.getOperands().get(0).getIdentifier().getName().equals("float")) { checkInFilterFunction(secondGrandChildFunction, "float", Arrays.asList(3.5, 4.5)); checkInFilterFunction(thirdGrandChildFunction, "double", Arrays.asList(1.1, 1.2, 1.3)); } else { checkInFilterFunction(secondGrandChildFunction, "double", Arrays.asList(1.1, 1.2, 1.3)); checkInFilterFunction(thirdGrandChildFunction, "float", Arrays.asList(3.5, 4.5)); } }
public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) { FilterQuery filterQuery = brokerRequest.getFilterQuery(); if (filterQuery != null) { FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) { filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema); } RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest); } }
QueryOptimizer { public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) { FilterQuery filterQuery = brokerRequest.getFilterQuery(); if (filterQuery != null) { FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) { filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema); } RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest); } } }
QueryOptimizer { public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) { FilterQuery filterQuery = brokerRequest.getFilterQuery(); if (filterQuery != null) { FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) { filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema); } RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest); } }  }
QueryOptimizer { public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) { FilterQuery filterQuery = brokerRequest.getFilterQuery(); if (filterQuery != null) { FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) { filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema); } RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest); } }  void optimize(BrokerRequest brokerRequest, @Nullable Schema schema); void optimize(PinotQuery pinotQuery, @Nullable Schema schema); }
QueryOptimizer { public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) { FilterQuery filterQuery = brokerRequest.getFilterQuery(); if (filterQuery != null) { FilterQueryTree filterQueryTree = RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap()); for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) { filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema); } RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest); } }  void optimize(BrokerRequest brokerRequest, @Nullable Schema schema); void optimize(PinotQuery pinotQuery, @Nullable Schema schema);  }