@Test public void testFlattenAndOrFilter() { String query = "SELECT * FROM testTable WHERE ((int = 4 OR (long = 5 AND (float = 9 AND double = 7.5))) OR string = 'foo') OR bytes = 'abc'"; 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.OR); List<FilterQueryTree> children = filterQueryTree.getChildren(); assertEquals(children.size(), 4); assertEquals(children.get(0).toString(), "int EQUALITY [4]"); assertEquals(children.get(2).toString(), "string EQUALITY [foo]"); assertEquals(children.get(3).toString(), "bytes EQUALITY [abc]"); FilterQueryTree andFilter = children.get(1); assertEquals(andFilter.getOperator(), FilterOperator.AND); List<FilterQueryTree> andFilterChildren = andFilter.getChildren(); assertEquals(andFilterChildren.size(), 3); assertEquals(andFilterChildren.get(0).toString(), "long EQUALITY [5]"); assertEquals(andFilterChildren.get(1).toString(), "float EQUALITY [9]"); assertEquals(andFilterChildren.get(2).toString(), "double EQUALITY [7.5]"); } OPTIMIZER.optimize(pinotQuery, SCHEMA); Function filterFunction = pinotQuery.getFilterExpression().getFunctionCall(); assertEquals(filterFunction.getOperator(), FilterKind.OR.name()); List<Expression> children = filterFunction.getOperands(); assertEquals(children.size(), 4); assertEquals(children.get(0), getEqFilterExpression("int", 4)); assertEquals(children.get(2), getEqFilterExpression("string", "foo")); assertEquals(children.get(3), getEqFilterExpression("bytes", "abc")); Function secondChildFunction = children.get(1).getFunctionCall(); assertEquals(secondChildFunction.getOperator(), FilterKind.AND.name()); List<Expression> secondChildChildren = secondChildFunction.getOperands(); assertEquals(secondChildChildren.size(), 3); assertEquals(secondChildChildren.get(0), getEqFilterExpression("long", 5)); assertEquals(secondChildChildren.get(1), getEqFilterExpression("float", 9)); assertEquals(secondChildChildren.get(2), getEqFilterExpression("double", 7.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);  }