From f90ea2a04098b7fff4c8c8159fd81e4d9856928b Mon Sep 17 00:00:00 2001 From: Rasmus Jokinen <146736881+51-code@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:20:50 +0200 Subject: [PATCH 1/3] Pair up renaming under a new rule with the renamed column in | timechart --- .../imports/DPLParserTransform_timechart.g4 | 10 ++- .../pth_03/tests/TimechartSyntaxTests.java | 72 +++++++++++++++++-- .../timechart/timechartEvaledField.txt | 46 ++++++++++++ 3 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/antlr4/commands/timechart/timechartEvaledField.txt diff --git a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 index 137f83f..cf7a28d 100644 --- a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 +++ b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 @@ -46,11 +46,15 @@ parser grammar DPLParserTransform_timechart; timechartTransformation - : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter t_timechart_fieldRenameInstruction?)*? (t_timechart_binOptParameter)*? ((aggregateFunction t_timechart_fieldRenameInstruction? | t_timechart_evaledField t_timechart_fieldRenameInstruction?) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? + : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter)*? (t_timechart_binOptParameter)*? ((t_timechart_aggregation | t_timechart_evaledField) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? ; t_timechart_aggParameter - : COMMAND_TIMECHART_MODE_AGG(((aggregateFunction | t_timechart_evaledField | fieldType))) + : COMMAND_TIMECHART_MODE_AGG(((aggregateFunction | t_timechart_evaledField | fieldType))) t_timechart_fieldRenameInstruction? + ; + +t_timechart_aggregation + : aggregateFunction t_timechart_fieldRenameInstruction? ; t_timechart_binOptParameter @@ -150,7 +154,7 @@ t_timechart_useotherParameter t_timechart_evaledField - : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R + : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R t_timechart_fieldRenameInstruction? ; t_timechart_fieldRenameInstruction diff --git a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java index 2a3123c..1acabba 100644 --- a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java +++ b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java @@ -48,6 +48,7 @@ import com.teragrep.pth_03.ParserStructureTestingUtility; import com.teragrep.pth_03.ParserSyntaxTestingUtility; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.params.ParameterizedTest; @@ -67,6 +68,7 @@ public class TimechartSyntaxTests { "timechart9", "timechart10", "timechart11", + "timechartEvaledField" }) public void timechartSyntaxParseTest(String arg) throws Exception { String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; @@ -148,7 +150,7 @@ void xpathTest5(String arg) throws Exception { void xpathTest6(String arg) throws Exception { ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - String xpathExp = "/root/transformStatement/timechartTransformation/aggregateFunction/aggregateMethodCount/value[1]"; + String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodCount/value[1]"; NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false); // Check that 1 found @@ -187,7 +189,7 @@ void xpathTest8(String arg) throws Exception { void xpathTest9(String arg) throws Exception { ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - String xpathExp = "/root/transformStatement/timechartTransformation/aggregateFunction/aggregateMethodCount/value"; + String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodCount/value"; NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false); // Check that 1 found @@ -343,7 +345,7 @@ void xpathTest20(String arg) throws Exception { void xpathTest21(String arg) throws Exception { ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - String xpathExp = "/root/transformStatement/timechartTransformation/aggregateFunction/aggregateMethodAvg/aggregate_fieldType/value"; + String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodAvg/aggregate_fieldType/value"; NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false); // Check that 1 found @@ -411,7 +413,7 @@ void xpathTest25(String arg) throws Exception { void xpathTest26(String arg) throws Exception { ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_fieldRenameInstruction/fieldType/value"; + String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_aggParameter/t_timechart_fieldRenameInstruction/fieldType/value"; NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false); // Check that 1 found @@ -425,11 +427,71 @@ void xpathTest26(String arg) throws Exception { void xpathTest27(String arg) throws Exception { ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - String xpathExp = "/root/transformStatement/timechartTransformation/aggregateFunction/aggregateMethodPercentileVariable/aggregate_fieldType/value"; + String xpathExp = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodPercentileVariable/aggregate_fieldType/value"; NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false); // Check that 1 found assertEquals(1,nodesA.getLength()); } + + @ParameterizedTest + @ValueSource(strings = { + "timechart11", + }) + public void testAggregationWithRename(String arg) { + final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); + final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; + + final String aggregateFunctionPath = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodSum/aggregate_fieldType/value"; + final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_aggregation/t_timechart_fieldRenameInstruction/fieldType/value"; + + NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); + NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + + assertEquals(1, aggregateFunctionNodes.getLength()); + assertEquals(1, renameNodes.getLength()); + assertEquals("sales", aggregateFunctionNodes.item(0).getTextContent()); + assertEquals("sales", renameNodes.item(0).getTextContent()); + } + + @ParameterizedTest + @ValueSource(strings = { + "timechart4", + }) + public void testAggParameterWithRename(String arg) { + final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); + final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; + + final String aggregateFunctionPath = "/root/transformStatement/timechartTransformation/t_timechart_aggParameter/aggregateFunction/aggregateMethodCount/aggregate_fieldType/value"; + final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_aggParameter/t_timechart_fieldRenameInstruction/fieldType/value"; + + NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); + NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + + assertEquals(1, aggregateFunctionNodes.getLength()); + assertEquals(1, renameNodes.getLength()); + assertEquals("ddd", aggregateFunctionNodes.item(0).getTextContent()); + assertEquals("D", renameNodes.item(0).getTextContent()); + } + + @ParameterizedTest + @ValueSource(strings = { + "timechartEvaledField", + }) + public void testEvaledFieldWithRename(String arg) { + final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); + final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; + + final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; + final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_evaledField/t_timechart_fieldRenameInstruction/fieldType/value"; + + NodeList evalNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, evalPath, false)); + NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + + assertEquals(1, evalNodes.getLength()); + assertEquals(1, renameNodes.getLength()); + assertEquals("sales", evalNodes.item(0).getTextContent()); + assertEquals("rounded_sales", renameNodes.item(0).getTextContent()); + } } diff --git a/src/test/resources/antlr4/commands/timechart/timechartEvaledField.txt b/src/test/resources/antlr4/commands/timechart/timechartEvaledField.txt new file mode 100644 index 0000000..b43a7e2 --- /dev/null +++ b/src/test/resources/antlr4/commands/timechart/timechartEvaledField.txt @@ -0,0 +1,46 @@ + +| timechart eval(round(sales, 3)) as rounded_sales \ No newline at end of file From 48b046d792e6d09bb9c14648928033c0e176d103 Mon Sep 17 00:00:00 2001 From: Rasmus Jokinen <146736881+51-code@users.noreply.github.com> Date: Tue, 18 Feb 2025 08:20:59 +0200 Subject: [PATCH 2/3] Divide eval field with a rename and without a rename to separate rules in | timechart --- .../imports/DPLParserTransform_timechart.g4 | 8 +++- .../pth_03/tests/TimechartSyntaxTests.java | 39 ++++++++++++---- .../timechart/timechartAggParamEval.txt | 46 +++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/test/resources/antlr4/commands/timechart/timechartAggParamEval.txt diff --git a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 index cf7a28d..b306337 100644 --- a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 +++ b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 @@ -46,7 +46,7 @@ parser grammar DPLParserTransform_timechart; timechartTransformation - : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter)*? (t_timechart_binOptParameter)*? ((t_timechart_aggregation | t_timechart_evaledField) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? + : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter)*? (t_timechart_binOptParameter)*? ((t_timechart_aggregation | t_timechart_renamedEvaledField) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? ; t_timechart_aggParameter @@ -154,7 +154,11 @@ t_timechart_useotherParameter t_timechart_evaledField - : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R t_timechart_fieldRenameInstruction? + : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R + ; + +t_timechart_renamedEvaledField + : t_timechart_evaledField t_timechart_fieldRenameInstruction? ; t_timechart_fieldRenameInstruction diff --git a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java index 1acabba..e085979 100644 --- a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java +++ b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java @@ -68,7 +68,8 @@ public class TimechartSyntaxTests { "timechart9", "timechart10", "timechart11", - "timechartEvaledField" + "timechartEvaledField", + "timechartAggParamEval" }) public void timechartSyntaxParseTest(String arg) throws Exception { String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; @@ -445,8 +446,8 @@ public void testAggregationWithRename(String arg) { final String aggregateFunctionPath = "/root/transformStatement/timechartTransformation/t_timechart_aggregation/aggregateFunction/aggregateMethodSum/aggregate_fieldType/value"; final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_aggregation/t_timechart_fieldRenameInstruction/fieldType/value"; - NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); - NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + final NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); + final NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); assertEquals(1, aggregateFunctionNodes.getLength()); assertEquals(1, renameNodes.getLength()); @@ -465,8 +466,8 @@ public void testAggParameterWithRename(String arg) { final String aggregateFunctionPath = "/root/transformStatement/timechartTransformation/t_timechart_aggParameter/aggregateFunction/aggregateMethodCount/aggregate_fieldType/value"; final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_aggParameter/t_timechart_fieldRenameInstruction/fieldType/value"; - NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); - NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + final NodeList aggregateFunctionNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggregateFunctionPath, false)); + final NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); assertEquals(1, aggregateFunctionNodes.getLength()); assertEquals(1, renameNodes.getLength()); @@ -482,16 +483,36 @@ public void testEvaledFieldWithRename(String arg) { final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; - final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_evaledField/t_timechart_fieldRenameInstruction/fieldType/value"; + final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_renamedEvaledField/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; + final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_renamedEvaledField/t_timechart_fieldRenameInstruction/fieldType/value"; - NodeList evalNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, evalPath, false)); - NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); + final NodeList evalNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, evalPath, false)); + final NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false)); assertEquals(1, evalNodes.getLength()); assertEquals(1, renameNodes.getLength()); assertEquals("sales", evalNodes.item(0).getTextContent()); assertEquals("rounded_sales", renameNodes.item(0).getTextContent()); } + + @ParameterizedTest + @ValueSource(strings = { + "timechartAggParamEval", + }) + public void testEvaledFieldInAggParam(String arg) { + final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); + final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; + + final String aggPath = "/root/transformStatement/timechartTransformation/t_timechart_aggParameter/value"; + final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_aggParameter/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; + + final NodeList aggNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, aggPath, false)); + final NodeList evalNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, evalPath, false)); + + assertEquals(1, aggNodes.getLength()); + assertEquals(1, evalNodes.getLength()); + assertEquals("agg=", aggNodes.item(0).getTextContent()); + assertEquals("sales", evalNodes.item(0).getTextContent()); + } } diff --git a/src/test/resources/antlr4/commands/timechart/timechartAggParamEval.txt b/src/test/resources/antlr4/commands/timechart/timechartAggParamEval.txt new file mode 100644 index 0000000..40d3446 --- /dev/null +++ b/src/test/resources/antlr4/commands/timechart/timechartAggParamEval.txt @@ -0,0 +1,46 @@ + +| timechart agg=eval(round(sales, 3)) count by x \ No newline at end of file From 9cd29fbc00ac3e4d5dcf34c105d40a0c80009174 Mon Sep 17 00:00:00 2001 From: Rasmus Jokinen <146736881+51-code@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:30:24 +0200 Subject: [PATCH 3/3] Change naming of renamedEvaledField rule to renameableEvaledField --- src/main/antlr4/imports/DPLParserTransform_timechart.g4 | 4 ++-- .../java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 index b306337..5001123 100644 --- a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 +++ b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 @@ -46,7 +46,7 @@ parser grammar DPLParserTransform_timechart; timechartTransformation - : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter)*? (t_timechart_binOptParameter)*? ((t_timechart_aggregation | t_timechart_renamedEvaledField) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? + : COMMAND_MODE_TIMECHART (t_timechart_sepParameter | t_timechart_formatParameter | t_timechart_partialParameter | t_timechart_fixedrangeParameter | t_timechart_contParameter | t_timechart_limitParameter)*? (t_timechart_aggParameter)*? (t_timechart_binOptParameter)*? ((t_timechart_aggregation | t_timechart_renameableEvaledField) t_timechart_divideByInstruction?)+ t_timechart_dedupSplitParameter? ; t_timechart_aggParameter @@ -157,7 +157,7 @@ t_timechart_evaledField : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R ; -t_timechart_renamedEvaledField +t_timechart_renameableEvaledField : t_timechart_evaledField t_timechart_fieldRenameInstruction? ; diff --git a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java index e085979..8f044f2 100644 --- a/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java +++ b/src/test/java/com/teragrep/pth_03/tests/TimechartSyntaxTests.java @@ -483,8 +483,8 @@ public void testEvaledFieldWithRename(String arg) { final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility(); final String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; - final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_renamedEvaledField/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; - final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_renamedEvaledField/t_timechart_fieldRenameInstruction/fieldType/value"; + final String evalPath = "/root/transformStatement/timechartTransformation/t_timechart_renameableEvaledField/t_timechart_evaledField/evalStatement/evalFunctionStatement/evalMethodRound/evalStatement/evalFieldType/value"; + final String renamePath = "root/transformStatement/timechartTransformation/t_timechart_renameableEvaledField/t_timechart_fieldRenameInstruction/fieldType/value"; final NodeList evalNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, evalPath, false)); final NodeList renameNodes = assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, renamePath, false));