diff --git a/src/main/antlr4/imports/DPLParserTransform_timechart.g4 b/src/main/antlr4/imports/DPLParserTransform_timechart.g4 index 137f83f..5001123 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_renameableEvaledField) 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 @@ -153,6 +157,10 @@ t_timechart_evaledField : COMMAND_TIMECHART_MODE_EVAL_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R ; +t_timechart_renameableEvaledField + : t_timechart_evaledField t_timechart_fieldRenameInstruction? + ; + t_timechart_fieldRenameInstruction : (COMMAND_TIMECHART_MODE_AS) fieldType ; 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..8f044f2 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,8 @@ public class TimechartSyntaxTests { "timechart9", "timechart10", "timechart11", + "timechartEvaledField", + "timechartAggParamEval" }) public void timechartSyntaxParseTest(String arg) throws Exception { String fileName = "src/test/resources/antlr4/commands/timechart/" + arg + ".txt"; @@ -148,7 +151,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 +190,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 +346,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 +414,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 +428,91 @@ 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"; + + 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()); + 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"; + + 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()); + 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_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)); + + 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 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