Skip to content

Commit 6e58b81

Browse files
authored
Merge pull request #458 from InseeFr/fix/union
Document INSEE issue (Fix #456)
2 parents bab2e88 + da2b8c3 commit 6e58b81

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

vtl-engine/src/test/java/fr/insee/vtl/engine/visitors/expression/functions/SetFunctionsVisitorTest.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
import fr.insee.vtl.model.Dataset;
88
import fr.insee.vtl.model.InMemoryDataset;
9+
import fr.insee.vtl.model.PersistentDataset;
910
import fr.insee.vtl.model.Structured;
11+
import java.util.Arrays;
1012
import java.util.List;
1113
import java.util.Map;
1214
import java.util.stream.Collectors;
13-
import javax.script.ScriptContext;
14-
import javax.script.ScriptEngine;
15-
import javax.script.ScriptEngineManager;
16-
import javax.script.ScriptException;
15+
import javax.script.*;
1716
import org.junit.jupiter.api.BeforeEach;
1817
import org.junit.jupiter.api.Test;
1918

@@ -247,4 +246,45 @@ public void testUnionMultiple() throws ScriptException {
247246
Map.of("name", "Franck2", "age", 12L, "weight", 9L),
248247
Map.of("name", "Hadrien2", "age", 10L, "weight", 11L));
249248
}
249+
250+
@Test
251+
public void testUnion456Issue() throws ScriptException {
252+
InMemoryDataset multimodeDs =
253+
new InMemoryDataset(
254+
List.of(
255+
new Structured.Component("interrogationId", String.class, Dataset.Role.IDENTIFIER),
256+
new Structured.Component("LOOP", String.class, Dataset.Role.IDENTIFIER),
257+
new Structured.Component("LOOP.FOO1", String.class, Dataset.Role.MEASURE),
258+
new Structured.Component("FOO", String.class, Dataset.Role.MEASURE)),
259+
Arrays.asList("T01", "LOOP-01", "foo11", "foo1"),
260+
Arrays.asList("T01", "LOOP-02", "foo12", "foo1"),
261+
Arrays.asList("T02", null, "foo21", "foo2"));
262+
263+
engine.put("$vtl.engine.use_dag", "false");
264+
ScriptContext context = engine.getContext();
265+
context.getBindings(ScriptContext.ENGINE_SCOPE).put("MULTIMODE", multimodeDs);
266+
267+
engine.eval(
268+
"TEMP_RACINE := MULTIMODE [keep interrogationId, FOO];\n"
269+
+ "RACINE := union(TEMP_RACINE, TEMP_RACINE) ;\n"
270+
+ "TEMP_LOOP := MULTIMODE [keep interrogationId, LOOP, LOOP.FOO1]\n"
271+
+ " [filter LOOP <> \"\"]\n"
272+
+ " [rename LOOP.FOO1 to FOO1];\n"
273+
+ "LOOP <- union(TEMP_LOOP, TEMP_LOOP);");
274+
275+
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
276+
Map<String, PersistentDataset> persitentDatasets =
277+
bindings.entrySet().stream()
278+
.filter(entry -> entry.getValue() instanceof PersistentDataset)
279+
.collect(Collectors.toMap(Map.Entry::getKey, e -> (PersistentDataset) e.getValue()));
280+
281+
assertThat(persitentDatasets.keySet().size()).isEqualTo(1);
282+
assertThat(persitentDatasets.containsKey("LOOP")).isTrue();
283+
284+
Dataset loopDs = persitentDatasets.get("LOOP");
285+
286+
assertThat(loopDs.getDataAsList().size()).isEqualTo(2);
287+
assertThat(loopDs.getDataAsList().stream().map(l -> l.get(1)))
288+
.isEqualTo(List.of("LOOP-01", "LOOP-02"));
289+
}
250290
}

0 commit comments

Comments
 (0)