Skip to content
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 @@ -5,6 +5,7 @@

import fr.insee.vtl.engine.exceptions.VtlRuntimeException;
import fr.insee.vtl.engine.visitors.expression.functions.GenericFunctionsVisitor;
import fr.insee.vtl.model.ConstantExpression;
import fr.insee.vtl.model.Positioned;
import fr.insee.vtl.model.ResolvableExpression;
import fr.insee.vtl.model.exceptions.InvalidTypeException;
Expand Down Expand Up @@ -165,11 +166,14 @@ private ResolvableExpression caseToIfIt(
}

ResolvableExpression nextWhen = whenExpr.next();
ResolvableExpression caseCondition =
genericFunctionsVisitor.invokeFunction(
"nvl", List.of(nextWhen, new ConstantExpression(false, nextWhen)), nextWhen);

return genericFunctionsVisitor.invokeFunction(
"ifThenElse",
List.of(nextWhen, thenExpr.next(), caseToIfIt(whenExpr, thenExpr, elseExpression)),
nextWhen);
List.of(caseCondition, thenExpr.next(), caseToIfIt(whenExpr, thenExpr, elseExpression)),
caseCondition);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public void testCaseExpr() throws ScriptException {
assertThat(context.getAttribute("s2")).isEqualTo("yes");
engine.eval("s3 := case when false then \"no\" when 1=2 then \"yes\" else \"else\";");
assertThat(context.getAttribute("s3")).isEqualTo("else");
engine.eval("s4 := case when cast(null, boolean) then \"no\" else \"else\";");
assertThat(context.getAttribute("s4")).isEqualTo("else");

engine.getContext().setAttribute("ds_1", DatasetSamples.ds1, ScriptContext.ENGINE_SCOPE);
engine.getContext().setAttribute("ds_2", DatasetSamples.ds2, ScriptContext.ENGINE_SCOPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.insee.vtl.model.Dataset;
import fr.insee.vtl.model.InMemoryDataset;
import fr.insee.vtl.model.Structured;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptContext;
Expand Down Expand Up @@ -79,4 +80,40 @@ public void testCalcClause() throws ScriptException, InterruptedException {
new Structured.Component("weight", Long.class, Dataset.Role.MEASURE),
new Structured.Component("wisdom", Double.class, Dataset.Role.ATTRIBUTE));
}

@Test
public void testCaseElseAppliesWhenWhenConditionIsNull() throws ScriptException {
ScriptContext context = engine.getContext();

Map<String, Object> r4 = new HashMap<>();
r4.put("name", "NullGuy");
r4.put("me_1", null);

InMemoryDataset dsWithNull =
new InMemoryDataset(
List.of(
Map.of("name", "A", "me_1", 0.12D),
Map.of("name", "B", "me_1", 3.5D),
Map.of("name", "C", "me_1", 10.7D),
r4),
Map.of("name", String.class, "me_1", Double.class),
Map.of("name", Dataset.Role.IDENTIFIER, "me_1", Dataset.Role.MEASURE));

context.setAttribute("ds_null", dsWithNull, ScriptContext.ENGINE_SCOPE);
engine.eval(
"res := ds_null[calc me_2 := case when me_1 <= 1 then 0 when me_1 > 1 and me_1 <= 10 then 1 when me_1 > 10 then 10 else 100];");

Dataset res = (Dataset) context.getAttribute("res");
assertThat(res.getDataAsMap())
.contains(
Map.of("name", "A", "me_1", 0.12D, "me_2", 0L),
Map.of("name", "B", "me_1", 3.5D, "me_2", 1L),
Map.of("name", "C", "me_1", 10.7D, "me_2", 10L));

Map<String, Object> expectedNullRow = new HashMap<>();
expectedNullRow.put("name", "NullGuy");
expectedNullRow.put("me_1", null);
expectedNullRow.put("me_2", 100L);
assertThat(res.getDataAsMap()).contains(expectedNullRow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fr.insee.vtl.model.InMemoryDataset;
import fr.insee.vtl.model.Structured;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptContext;
Expand Down Expand Up @@ -80,4 +81,40 @@ public void testCalcClause() throws ScriptException, InterruptedException {
new Structured.Component("weight", Long.class, Dataset.Role.MEASURE),
new Structured.Component("wisdom", Double.class, Dataset.Role.ATTRIBUTE));
}

@Test
public void testCaseElseAppliesWhenWhenConditionIsNull() throws ScriptException {
ScriptContext context = engine.getContext();

Map<String, Object> r4 = new HashMap<>();
r4.put("name", "NullGuy");
r4.put("me_1", null);

InMemoryDataset dsWithNull =
new InMemoryDataset(
List.of(
Map.of("name", "A", "me_1", 0.12D),
Map.of("name", "B", "me_1", 3.5D),
Map.of("name", "C", "me_1", 10.7D),
r4),
Map.of("name", String.class, "me_1", Double.class),
Map.of("name", Dataset.Role.IDENTIFIER, "me_1", Dataset.Role.MEASURE));

context.setAttribute("ds_null", dsWithNull, ScriptContext.ENGINE_SCOPE);
engine.eval(
"res := ds_null[calc me_2 := case when me_1 <= 1 then 0 when me_1 > 1 and me_1 <= 10 then 1 when me_1 > 10 then 10 else 100];");

Dataset res = (Dataset) context.getAttribute("res");
assertThat(res.getDataAsMap())
.contains(
Map.of("name", "A", "me_1", 0.12D, "me_2", 0L),
Map.of("name", "B", "me_1", 3.5D, "me_2", 1L),
Map.of("name", "C", "me_1", 10.7D, "me_2", 10L));

Map<String, Object> expectedNullRow = new HashMap<>();
expectedNullRow.put("name", "NullGuy");
expectedNullRow.put("me_1", null);
expectedNullRow.put("me_2", 100L);
assertThat(res.getDataAsMap()).contains(expectedNullRow);
}
}
Loading