private CalcResult countGranulesMatchingCalc(Filter filter, RasterManager manager) throws IOException { Query q = new Query(manager.getTypeName()); q.setFilter(filter); SimpleFeatureCollection lc = manager.getGranuleCatalog().getGranules(q); List<Expression> groupByExpressions = Arrays.asList((Expression) getLocationProperty(manager)); GroupByVisitor groupVisitor = new GroupByVisitor(Aggregate.COUNT, NilExpression.NIL, groupByExpressions, null); lc.accepts(groupVisitor, null); return groupVisitor.getResult(); }
protected boolean isCountVisitor(FeatureVisitor visitor) { if (visitor instanceof CountVisitor) { // is count visitor nothing else to test return true; } // the visitor maybe wrapper by a group by visitor return isGroupByVisitor(visitor) && ((GroupByVisitor) visitor).getAggregateVisitor() instanceof CountVisitor; }
/** * Checks if the groupBy is a supported one, that is, if it's possible to turn to SQL the * various {@link Expression} it's using * * @param visitor * @return */ private boolean isSupportedGroupBy(GroupByVisitor visitor) { return visitor.getGroupByAttributes().stream().allMatch(xp -> fullySupports(xp)); }
private void testVisitor( String aggregateAttribute, String aggregateVisitor, String[] groupByAttributes, Object[][] expectedResults) throws Exception { GroupByVisitor visitor = executeVisitor( featureCollection, aggregateAttribute, aggregateVisitor, groupByAttributes); checkResults(visitor.getResult(), expectedResults); }
private List<Object[]> genericGroupByTestTest( Query query, Aggregate aggregateVisitor, Expression aggregateAttribute, boolean expectOptimized, Expression... groupByAttributes) throws IOException { ContentFeatureSource featureSource = dataStore.getFeatureSource(tname("buildings_group_by_tests")); GroupByVisitorBuilder visitorBuilder = new GroupByVisitorBuilder() .withAggregateAttribute(aggregateAttribute) .withAggregateVisitor(aggregateVisitor); for (Expression groupByAttribute : groupByAttributes) { visitorBuilder.withGroupByAttribute(groupByAttribute); } GroupByVisitor visitor = visitorBuilder.build(); featureSource.accepts(query, visitor, null); assertEquals(expectOptimized, visitor.wasOptimized()); assertEquals(!expectOptimized, visitor.wasVisited()); List<Object[]> value = visitor.getResult().toList(); assertNotNull(value); return value; }
/** * Create a group by visitor checking that all mandatory values are present. * * @return a new group by visitor */ public GroupByVisitor build() { if (aggregateAttribute == null) { throw new IllegalArgumentException("An aggregate attribute is required."); } if (aggregateVisitor == null) { throw new IllegalArgumentException("An aggregate visitor is required."); } if (groupByAttributes == null || groupByAttributes.isEmpty()) { throw new IllegalArgumentException("At least one group by attribute is required."); } if (progressListener == null) { progressListener = new NullProgressListener(); } return new GroupByVisitor( aggregateVisitor, aggregateAttribute, groupByAttributes, progressListener); } }
@Test public void testFeatureAttributeVisitor() { GroupByVisitor visitor = buildVisitor("energy_consumption", "Average", new String[] {"building_type"}); List<Expression> expressions = visitor.getExpressions(); Set<String> names = new HashSet<>(); for (Expression expression : expressions) { PropertyName pn = (PropertyName) expression; names.add(pn.getPropertyName()); } assertEquals(2, names.size()); assertTrue(names.contains("energy_consumption")); assertTrue(names.contains("building_type")); }
groupByVisitors .stream() .map(visitor -> (Map<List<Object>, Object>) visitor.getResult().toMap()) .collect(Collectors.toList()); return new Results(
public void testSumAreaWithGroupBy() throws Exception { FilterFactory ff = dataStore.getFilterFactory(); PropertyName p = ff.property(aname("geom")); GroupByVisitor v = new GroupByVisitorBuilder() .withAggregateAttribute(ff.function("area2", p)) .withAggregateVisitor("SumArea") .withGroupByAttributes( Collections.singleton(aname("name")), dataStore.getSchema(tname("aggregate"))) .build(); dataStore.getFeatureSource(tname("aggregate")).accepts(Query.ALL, v, null); if (dataStore.getSupportedFunctions().containsKey(FilterFunction_area.NAME.getName())) { assertFalse(visited); } List groups = v.getResult().toList(); assertEquals(20.0, (Double) ((Object[]) groups.get(0))[1], 0.01); assertEquals(10.0, (Double) ((Object[]) groups.get(1))[1], 0.01); }
private Expression getAggregateExpression(FeatureVisitor visitor) { // if is a group by visitor we need to use the internal aggregate visitor FeatureVisitor aggregateVisitor = isGroupByVisitor(visitor) ? ((GroupByVisitor) visitor).getAggregateVisitor() : visitor; Expression expression = getExpression(aggregateVisitor); if (expression == null) { // no aggregate attribute available, NULL will be returned LOGGER.info("Visitor " + visitor.getClass() + " has no aggregate attribute."); return null; } return expression; }
/** * Helper method that extracts a list of group by attributes from a group by visitor. If the * visitor is not a group by visitor an empty list will be returned. * * @param visitor the feature visitor * @return the list of the group by attributes or an empty list */ protected List<Expression> extractGroupByExpressions(FeatureVisitor visitor) { // if is a group by visitor we get the list of attributes expressions otherwise we get an // empty list List<Expression> expressions = isGroupByVisitor(visitor) ? ((GroupByVisitor) visitor).getGroupByAttributes() : new ArrayList<>(); return expressions; }
new String[] {"building_type"}); checkResults( visitorA.getResult(), new Object[][] { new Object[] {"SCHOOL", 30.0}, new String[] {"building_type"}); checkResults( visitorB.getResult(), new Object[][] { new Object[] {"SCHOOL", 57.5}, visitorA.getResult().merge(visitorB.getResult()), new Object[][] { new Object[] {"SCHOOL", 36.875},
/** * Helper method that will try to match a feature visitor with an aggregate function. If no * aggregate function machs the visitor NULL will be returned. * * @param visitor the feature visitor * @return the match aggregate function name, or NULL if no match */ protected String matchAggregateFunction(FeatureVisitor visitor) { // if is a group by visitor we use use the internal aggregate visitor class otherwise we use // the visitor class Class visitorClass = isGroupByVisitor(visitor) ? ((GroupByVisitor) visitor).getAggregateVisitor().getClass() : visitor.getClass(); String function = null; // try to find a matching aggregate function walking up the hierarchy if necessary while (function == null && visitorClass != null) { function = getAggregateFunctions().get(visitorClass); visitorClass = visitorClass.getSuperclass(); } if (function == null) { // this visitor don't match any aggregate function NULL will be returned LOGGER.info( "Unable to find aggregate function matching visitor: " + visitor.getClass()); } return function; }
new String[] {"building_type"}); checkResults( visitorA.getResult(), new Object[][] { new Object[] {"SCHOOL", 30.0}, new String[] {"building_type"}); checkResults( visitorB.getResult(), new Object[][] { new Object[] {"SCHOOL", 57.5}, visitorA.getResult().merge(visitorB.getResult()), new Object[][] { new Object[] {"SCHOOL", 36.875},