From 38d95287c88e5e1678d0b478c88d59e1c0f6cc7c Mon Sep 17 00:00:00 2001 From: dbulahov Date: Thu, 26 Nov 2026 11:32:58 +0300 Subject: [PATCH] unparser: a searched CASE is written as a searched CASE SimpleUnparser matched CaseOperationAtom without reading its name, so the searched form (_CaseTest) was written like the simple one (_CaseMatch): the first condition became the operand. CASE WHEN [Measures].[A] > 1 THEN 1 ELSE 0 END came back as CASE [Measures].[A] > 1 WHEN 1 THEN 0 END, valid MDX with another meaning. An ELSE without a WHEN was dropped (CASE a ELSE d END gave CASE a END), and CASE END threw IndexOutOfBoundsException. The unparser now writes each form by its name; an unknown name, or a simple CASE without its operand, is refused with IllegalArgumentException. CaseOperationAtom gains the constants CASE_MATCH and CASE_TEST, used by both grammars and the unparser. Tests: unparser.simple SimpleUnparserExpressionTest, combinedtest RoundTripTest (searched and mixed nesting, ccc and cccx), parser.tck ExpressionTest pins the _CaseTest atom. Signed-off-by: dbulahov --- .../combinedtest/roundtrip/RoundTripTest.java | 7 ++ .../operation/CaseOperationAtom.java | 15 +++- parser.ccc/src/main/ccc/Grammer.ccc | 4 +- parser.cccx/src/main/ccc/Expressions.inc.ccc | 2 +- .../daanse/mdx/parser/tck/ExpressionTest.java | 30 ++++++-- .../mdx/unparser/simple/SimpleUnparser.java | 39 ++++++----- .../simple/SimpleUnparserExpressionTest.java | 70 +++++++++++++++++++ 7 files changed, 141 insertions(+), 26 deletions(-) diff --git a/combinedtest/src/test/java/org/eclipse/daanse/mdx/combinedtest/roundtrip/RoundTripTest.java b/combinedtest/src/test/java/org/eclipse/daanse/mdx/combinedtest/roundtrip/RoundTripTest.java index 78cba24..61739d1 100644 --- a/combinedtest/src/test/java/org/eclipse/daanse/mdx/combinedtest/roundtrip/RoundTripTest.java +++ b/combinedtest/src/test/java/org/eclipse/daanse/mdx/combinedtest/roundtrip/RoundTripTest.java @@ -80,6 +80,13 @@ class RoundTripTest { "SELECT {CAST(IIF(1 = 1, \"a,b\", 'c') AS STRING)} ON 0 FROM [Sales]", "SELECT {CAST(1 AS [INT]]EGER])} ON 0 FROM [Sales]", "SELECT {CASE [x] WHEN 1 THEN \"one\" WHEN 2 THEN \"two\" ELSE \"many\" END} ON 0 FROM [Sales]", + "SELECT {CASE WHEN [Measures].[A] > 1 THEN 1 ELSE 0 END} ON 0 FROM [Sales]", + "SELECT {CASE WHEN a > 1 THEN \"high\" WHEN a > 0 THEN \"low\" ELSE \"none\" END} ON 0 FROM [Sales]", + "SELECT {CASE WHEN IsEmpty([Measures].[A]) THEN NULL ELSE [Measures].[A] / [Measures].[B] END} ON 0 FROM [Sales]", + "SELECT {CASE WHEN a THEN CASE WHEN b THEN 1 ELSE 2 END ELSE 3 END} ON 0 FROM [Sales]", + "SELECT {CASE a WHEN 1 THEN CASE WHEN b THEN 1 ELSE 2 END ELSE 3 END} ON 0 FROM [Sales]", + "SELECT {CASE WHEN a THEN CASE b WHEN 1 THEN \"x\" END ELSE \"y\" END} ON 0 FROM [Sales]", + "WITH MEMBER [Measures].[X] AS CASE WHEN [Measures].[A] > 1 THEN 1 ELSE 0 END SELECT [Measures].[X] ON 0 FROM [Sales]", "SELECT {(1 + 2) * 3 - -4, NOT ([a] IS NULL), [a] : [b]} ON 0 FROM [Sales]", "SELECT Crossjoin([a].Children, [b].Members).Item(0) ON 0 FROM [Sales]", "SELECT Foo().[Quoted ]] Property] ON 0, Foo().&[Key ]] Property] ON 1 FROM [Sales]", diff --git a/model.api/src/main/java/org/eclipse/daanse/mdx/model/api/expression/operation/CaseOperationAtom.java b/model.api/src/main/java/org/eclipse/daanse/mdx/model/api/expression/operation/CaseOperationAtom.java index 867f329..79be15b 100644 --- a/model.api/src/main/java/org/eclipse/daanse/mdx/model/api/expression/operation/CaseOperationAtom.java +++ b/model.api/src/main/java/org/eclipse/daanse/mdx/model/api/expression/operation/CaseOperationAtom.java @@ -16,15 +16,24 @@ import java.util.Objects; /** - * Defines a Case-Operation. + * Defines a Case-Operation. The name tells the two forms apart; the parts are + * one flat list in the call's expressions. * * */ public record CaseOperationAtom(String name) implements OperationAtom { + /** The simple form, with an operand after {@code CASE}. */ + public static final String CASE_MATCH = "_CaseMatch"; + + /** The searched form, with a condition after each {@code WHEN}. */ + public static final String CASE_TEST = "_CaseTest"; + public CaseOperationAtom { Objects.requireNonNull(name, "name must not be null"); } diff --git a/parser.ccc/src/main/ccc/Grammer.ccc b/parser.ccc/src/main/ccc/Grammer.ccc index 70b7df8..209cec4 100644 --- a/parser.ccc/src/main/ccc/Grammer.ccc +++ b/parser.ccc/src/main/ccc/Grammer.ccc @@ -1704,12 +1704,12 @@ MdxExpression parseCaseExpression : if (match) { return new CallExpressionR( - new CaseOperationAtom("_CaseMatch"), expressions); + new CaseOperationAtom(CaseOperationAtom.CASE_MATCH), expressions); } else { return new CallExpressionR( - new CaseOperationAtom("_CaseTest"), expressions); + new CaseOperationAtom(CaseOperationAtom.CASE_TEST), expressions); } } ; diff --git a/parser.cccx/src/main/ccc/Expressions.inc.ccc b/parser.cccx/src/main/ccc/Expressions.inc.ccc index a535426..2609d5d 100644 --- a/parser.cccx/src/main/ccc/Expressions.inc.ccc +++ b/parser.cccx/src/main/ccc/Expressions.inc.ccc @@ -622,7 +622,7 @@ INJECT CaseExpression : { @Override public OperationAtom operationAtom() { - return new CaseOperationAtom((get(1) instanceof org.eclipse.daanse.mdx.model.api.expression.MdxExpression) ? "_CaseMatch" : "_CaseTest"); + return new CaseOperationAtom((get(1) instanceof org.eclipse.daanse.mdx.model.api.expression.MdxExpression) ? CaseOperationAtom.CASE_MATCH : CaseOperationAtom.CASE_TEST); } } diff --git a/parser.tck/src/main/java/org/eclipse/daanse/mdx/parser/tck/ExpressionTest.java b/parser.tck/src/main/java/org/eclipse/daanse/mdx/parser/tck/ExpressionTest.java index 7ec00c1..56812d8 100644 --- a/parser.tck/src/main/java/org/eclipse/daanse/mdx/parser/tck/ExpressionTest.java +++ b/parser.tck/src/main/java/org/eclipse/daanse/mdx/parser/tck/ExpressionTest.java @@ -236,6 +236,32 @@ void testCallExpressionTermCase(@InjectService MdxParserProvider mdxParserProvid checkArgument((CallExpression) clause, 2, "c"); } + @Test + void testCallExpressionTermCaseSearched(@InjectService MdxParserProvider mdxParserProvider) + throws MdxParserException { + MdxExpression clause = mdxParserProvider.newParser("CASE WHEN a THEN b ELSE c END", propertyWords) + .parseExpression(); + assertThat(clause).isNotNull().isInstanceOf(CallExpression.class); + assertThat(((CallExpression) clause).operationAtom()) + .isEqualTo(new CaseOperationAtom(CaseOperationAtom.CASE_TEST)); + assertThat(((CallExpression) clause).expressions()).hasSize(3); + checkArgument((CallExpression) clause, 0, "a"); + checkArgument((CallExpression) clause, 1, "b"); + checkArgument((CallExpression) clause, 2, "c"); + } + + @Test + void testCallExpressionTermCaseSearchedTwoBranches(@InjectService MdxParserProvider mdxParserProvider) + throws MdxParserException { + MdxExpression clause = mdxParserProvider.newParser("CASE WHEN a THEN b WHEN c THEN d END", propertyWords) + .parseExpression(); + assertThat(((CallExpression) clause).operationAtom()) + .isEqualTo(new CaseOperationAtom(CaseOperationAtom.CASE_TEST)); + assertThat(((CallExpression) clause).expressions()).hasSize(4); + checkArgument((CallExpression) clause, 0, "a"); + checkArgument((CallExpression) clause, 3, "d"); + } + @Test void testCallExpressionBraces1(@InjectService MdxParserProvider mdxParserProvider) throws MdxParserException { MdxExpression clause = mdxParserProvider.newParser("{ expression }", propertyWords).parseExpression(); @@ -258,8 +284,6 @@ void testCallExpressionBraces2(@InjectService MdxParserProvider mdxParserProvide void testCallExpressionBraces3(@InjectService MdxParserProvider mdxParserProvider) throws MdxParserException { MdxExpression clause = mdxParserProvider.newParser("{ [a] : [c] }", propertyWords).parseExpression(); assertThat(clause).isNotNull().isInstanceOf(CallExpression.class); - // assertThat(((CallExpression) clause).operationAtom()).isEqualTo(new - // CaseOperationAtom("_CaseMatch")); assertThat(((CallExpression) clause).expressions()).hasSize(1); assertThat(((CallExpression) clause).expressions().get(0)).isInstanceOf(CallExpression.class); @@ -304,8 +328,6 @@ void testCallExpressionBraces4(@InjectService MdxParserProvider mdxParserProvide .parseExpression(); assertThat(clause).isNotNull().isInstanceOf(CallExpression.class); CallExpression callExpression = ((CallExpression) clause); - // assertThat(((CallExpression) clause).operationAtom()).isEqualTo(new - // CaseOperationAtom("_CaseMatch")); assertThat(callExpression.expressions()).hasSize(3); assertThat(callExpression.expressions().get(0)).isInstanceOf(CompoundId.class); diff --git a/unparser.simple/src/main/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparser.java b/unparser.simple/src/main/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparser.java index 4c44e97..555acea 100644 --- a/unparser.simple/src/main/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparser.java +++ b/unparser.simple/src/main/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparser.java @@ -339,25 +339,32 @@ private CharSequence unparseCallExpression(CallExpression callExpression) { sb.append("CAST(").append(unparseExpression(expressions.get(0))).append(" AS ") .append(unparseExpression(expressions.get(1))).append(")"); } - case CaseOperationAtom _UNNAMED -> { - sb.append("CASE "); - sb.append(unparseExpression(expressions.get(0))); - int i = 0; - boolean hasElse = false; - if ((expressions.size() - 1) > 2 && (expressions.size() - 1) % 2 != 0) { - hasElse = true; + case CaseOperationAtom caseAtom -> { + boolean match; + if (CaseOperationAtom.CASE_MATCH.equals(caseAtom.name())) { + match = true; + } else if (CaseOperationAtom.CASE_TEST.equals(caseAtom.name())) { + match = false; + } else { + throw new IllegalArgumentException("unknown CASE form: " + caseAtom.name()); } - for (int j = 1; j < expressions.size() - 1; j += 2) { - sb.append(" WHEN "); - sb.append(unparseExpression(expressions.get(j))); - if (j + 1 < expressions.size()) { - sb.append(" THEN "); - sb.append(unparseExpression(expressions.get(j + 1))); - } + if (match && expressions.isEmpty()) { + throw new IllegalArgumentException("a simple CASE needs its operand"); + } + int first = match ? 1 : 0; + boolean hasElse = (expressions.size() - first) % 2 != 0; + int pairsEnd = expressions.size() - (hasElse ? 1 : 0); + + sb.append("CASE"); + if (match) { + sb.append(' ').append(unparseExpression(expressions.get(0))); + } + for (int j = first; j < pairsEnd; j += 2) { + sb.append(" WHEN ").append(unparseExpression(expressions.get(j))); + sb.append(" THEN ").append(unparseExpression(expressions.get(j + 1))); } if (hasElse) { - sb.append(" ELSE "); - sb.append(unparseExpression(expressions.get(expressions.size() - 1))); + sb.append(" ELSE ").append(unparseExpression(expressions.get(expressions.size() - 1))); } sb.append(" END"); } diff --git a/unparser.simple/src/test/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparserExpressionTest.java b/unparser.simple/src/test/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparserExpressionTest.java index e20ff6b..6fbdc1f 100644 --- a/unparser.simple/src/test/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparserExpressionTest.java +++ b/unparser.simple/src/test/java/org/eclipse/daanse/mdx/unparser/simple/SimpleUnparserExpressionTest.java @@ -17,11 +17,13 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import java.math.BigDecimal; +import java.util.Arrays; import java.util.List; import org.eclipse.daanse.mdx.model.api.expression.CallExpression; import org.eclipse.daanse.mdx.model.api.expression.CompoundId; import org.eclipse.daanse.mdx.model.api.expression.KeyObjectIdentifier; +import org.eclipse.daanse.mdx.model.api.expression.MdxExpression; import org.eclipse.daanse.mdx.model.api.expression.NameObjectIdentifier; import org.eclipse.daanse.mdx.model.api.expression.NullLiteral; import org.eclipse.daanse.mdx.model.api.expression.NumericLiteral; @@ -261,6 +263,74 @@ void testCallExpressionTermCaseMultyWithoutElse() { List.of(compoundId1, compoundId2, compoundId3, compoundId4, compoundId5)); assertThat(unparser.unparseExpression(callExpression)).asString().isEqualTo("CASE a WHEN b1 THEN c1 WHEN b2 THEN c2 END"); } + private static CompoundId id(String name) { + return new CompoundIdR(List.of(new NameObjectIdentifierR(name, Quoting.UNQUOTED))); + } + + private String caseOf(String form, String... parts) { + List list = Arrays.stream(parts) + .map(p -> id(p)).toList(); + return unparser.unparseExpression(new CallExpressionR(new CaseOperationAtom(form), list)).toString(); + } + + @Test + void testCaseSearchedWithoutElse() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "c", "t")).isEqualTo("CASE WHEN c THEN t END"); + } + + @Test + void testCaseSearchedWithElse() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "c", "t", "e")).isEqualTo("CASE WHEN c THEN t ELSE e END"); + } + + @Test + void testCaseSearchedTwoBranches() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "c1", "t1", "c2", "t2")) + .isEqualTo("CASE WHEN c1 THEN t1 WHEN c2 THEN t2 END"); + } + + @Test + void testCaseSearchedTwoBranchesWithElse() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "c1", "t1", "c2", "t2", "e")) + .isEqualTo("CASE WHEN c1 THEN t1 WHEN c2 THEN t2 ELSE e END"); + } + + @Test + void testCaseSearchedThreeBranchesWithElse() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "c1", "t1", "c2", "t2", "c3", "t3", "e")) + .isEqualTo("CASE WHEN c1 THEN t1 WHEN c2 THEN t2 WHEN c3 THEN t3 ELSE e END"); + } + + @Test + void testCaseSearchedEmpty() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST)).isEqualTo("CASE END"); + } + + @Test + void testCaseSearchedOnlyElse() { + assertThat(caseOf(CaseOperationAtom.CASE_TEST, "e")).isEqualTo("CASE ELSE e END"); + } + + @Test + void testCaseMatchOnlyOperand() { + assertThat(caseOf(CaseOperationAtom.CASE_MATCH, "a")).isEqualTo("CASE a END"); + } + + @Test + void testCaseMatchOperandAndElse() { + assertThat(caseOf(CaseOperationAtom.CASE_MATCH, "a", "d")).isEqualTo("CASE a ELSE d END"); + } + + @Test + void testCaseMatchWithoutOperandIsRefused() { + assertThatIllegalArgumentException().isThrownBy(() -> caseOf(CaseOperationAtom.CASE_MATCH)); + } + + @Test + void testCaseUnknownFormIsRefused() { + assertThatIllegalArgumentException().isThrownBy(() -> caseOf("_CaseOther", "a", "b", "c")); + } + @Test void testCallExpressionBraces1() { NameObjectIdentifier objectIdentifier = new NameObjectIdentifierR("expression", Quoting.UNQUOTED);