use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.
the class CommonExpressionVisitor method visitWithQueryMods.
/**
* Visits a context with a configuration of query modifiers.
*
* @param ctx any context
* @param mods the query modifiers
* @return the value with the applied offset
*/
public Object visitWithQueryMods(ParserRuleContext ctx, QueryModifiers mods) {
QueryModifiers savedQueryMods = state.getQueryMods();
state.setQueryMods(mods);
Object result = visit(ctx);
state.setQueryMods(savedQueryMods);
return result;
}
use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.
the class ExpressionServiceTest method testIndicatorFunctionParsing.
@Test
void testIndicatorFunctionParsing() {
DimensionalItemId id;
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", (QueryModifiers) null);
assertEquals(id, parseItemId("#{dataElemenA}"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().aggregationType(LAST).build());
assertEquals(id, parseItemId("#{dataElemenA}.aggregationType(LAST)"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(10).build());
assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(10)"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(-5).build());
assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(-2).periodOffset(-3)"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().minDate(parseDate("2020-01-01")).build());
assertEquals(id, parseItemId("#{dataElemenA}.minDate(2020-1-1)"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().maxDate(parseDate("2021-12-31")).build());
assertEquals(id, parseItemId("#{dataElemenA}.maxDate(2021-12-31)"));
id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(-3).minDate(parseDate("2021-04-01")).maxDate(parseDate("2021-04-30")).build());
assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(-3).minDate(2021-04-1).maxDate(2021-4-30)"));
}
use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.
the class QueryPlannerUtils method getMinMaxDateDateElementMap.
/**
* Creates a mapping between minDate/maxDate query modifiers and data
* elements for the given data elements.
*
* @param dataElements list of data elements.
*/
public static ListMap<QueryModifiers, DimensionalItemObject> getMinMaxDateDateElementMap(List<DimensionalItemObject> dataElements) {
ListMap<QueryModifiers, DimensionalItemObject> map = new ListMap<>();
for (DimensionalItemObject element : dataElements) {
QueryModifiers queryMods = element.getQueryMods();
// Get QueryModifiers but only with min and max date, nothing else
QueryModifiers minMaxDateModifiers = (queryMods == null) ? QueryModifiers.builder().build() : QueryModifiers.builder().minDate(queryMods.getMinDate()).maxDate(queryMods.getMaxDate()).build();
map.putValue(minMaxDateModifiers, element);
}
return map;
}
use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.
the class QueryPlannerTest method planQueryN.
/**
* Query spans 5 QueryModifiers minDate/MaxDate combinations.
*/
@Test
void planQueryN() {
QueryModifiers modsA = QueryModifiers.builder().minDate(parseDate("2022-01-01")).build();
QueryModifiers modsB = QueryModifiers.builder().minDate(parseDate("2022-02-01")).build();
QueryModifiers modsC = QueryModifiers.builder().maxDate(parseDate("2022-12-31")).build();
QueryModifiers modsD = QueryModifiers.builder().minDate(parseDate("2022-01-01")).maxDate(parseDate("2022-12-31")).build();
deC.setQueryMods(modsA);
deD.setQueryMods(modsB);
deG.setQueryMods(modsC);
deH.setQueryMods(modsD);
deI.setQueryMods(modsD);
deC.setAggregationType(AggregationType.SUM);
deD.setAggregationType(AggregationType.SUM);
deI.setAggregationType(AggregationType.SUM);
DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC, deD, deG, deH, deI)).withPeriods(getList(createPeriod("2022"))).build();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
assertEquals(5, queryGroups.getAllQueries().size());
assertEquals(1, queryGroups.getSequentialQueries().size());
assertEquals(5, queryGroups.getLargestGroupSize());
List<DataQueryParams> group = queryGroups.getAllQueries();
assertQueryMods(group, null, deA, deB);
assertQueryMods(group, modsA, deC);
assertQueryMods(group, modsB, deD);
assertQueryMods(group, modsC, deG);
assertQueryMods(group, modsD, deH, deI);
}
use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.
the class FunctionAggregationType method evaluate.
@Override
public Object evaluate(ExprContext ctx, CommonExpressionVisitor visitor) {
AggregationType aggregationType = parseAggregationType(ctx.aggregationType.getText());
QueryModifiers queryMods = visitor.getState().getQueryModsBuilder().aggregationType(aggregationType).build();
return visitor.visitWithQueryMods(ctx.expr(0), queryMods);
}
Aggregations