Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aggr groupby var cast to identifier (Fix #309) #311

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@
import fr.insee.vtl.engine.exceptions.InvalidArgumentException;
import fr.insee.vtl.engine.exceptions.VtlRuntimeException;
import fr.insee.vtl.engine.visitors.expression.ExpressionVisitor;
import fr.insee.vtl.model.AggregationExpression;
import fr.insee.vtl.model.Dataset;
import fr.insee.vtl.model.DatasetExpression;
import fr.insee.vtl.model.ProcessingEngine;
import fr.insee.vtl.model.ResolvableExpression;
import fr.insee.vtl.model.Structured;
import fr.insee.vtl.model.*;
import fr.insee.vtl.model.exceptions.VtlScriptException;
import fr.insee.vtl.parser.VtlBaseVisitor;
import fr.insee.vtl.parser.VtlParser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import static fr.insee.vtl.engine.VtlScriptEngine.fromContext;
Expand Down Expand Up @@ -208,13 +198,21 @@ public DatasetExpression visitAggrClause(VtlParser.AggrClauseContext ctx) {
agg -> getSource(agg.aggrOperatorsGrouping())
));

DatasetExpression normalizedDataset = processingEngine.executeCalc(this.datasetExpression, expressions, roles, expressionStrings);

var dataStructure = datasetExpression.getDataStructure();
var groupBy = new GroupByVisitor(dataStructure).visit(ctx);

DatasetExpression normalizedDataset = processingEngine.executeCalc(this.datasetExpression, expressions, roles, expressionStrings);

// TODO: Move to engine
Structured.DataStructure normalizedStructure = normalizedDataset.getDataStructure();

// Transform column roles into IDENTIFIER when defined in groupingClause
normalizedStructure.forEach((k, v) -> {
if (groupBy.contains(k)) {
normalizedStructure.put(k, new Structured.Component(k, v.getType(), Dataset.Role.IDENTIFIER));
}
});

Map<String, AggregationExpression> collectorMap = new LinkedHashMap<>();
for (VtlParser.AggrFunctionClauseContext functionCtx : ctx.aggregateClause().aggrFunctionClause()) {
String alias = getName(functionCtx.componentID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,16 @@ public void testAggregate() throws ScriptException {
Map.of("name", "Franck", "country", "france", "age", 12L, "weight", 9D)
),
Map.of("name", String.class, "country", String.class, "age", Long.class, "weight", Double.class),
Map.of("name", Role.IDENTIFIER, "country", Role.IDENTIFIER, "age", Role.MEASURE, "weight", Role.MEASURE),
Map.of("name", Role.IDENTIFIER, "country", Role.MEASURE, "age", Role.MEASURE, "weight", Role.MEASURE),
Map.of("name", false, "country", true)
);

ScriptContext context = engine.getContext();
context.setAttribute("ds1", dataset, ScriptContext.ENGINE_SCOPE);

// test := ds1[aggr sumAge := sum(age) group by country];
// test := ds1[aggr sumAge := sum(age group by country)];
// test := ds1[aggr sumAge := sum(age group by country), totalWeight := sum(weight group by country)];
// test := ds1[aggr sumAge := sum(age), totalWeight := sum(weight) group by country];
engine.eval("res := ds1[aggr sumAge := sum(age) group by country];");
Dataset res = (Dataset) engine.getContext().getAttribute("res");
assertThat(res.getDataStructure().get("country").getRole()).isEqualTo(Role.IDENTIFIER);

engine.eval("res := ds1[aggr " +
"sumAge := sum(age)," +
Expand Down