Skip to content

Commit

Permalink
Add avg() to eval methods (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
51-code authored Jul 22, 2024
1 parent 7a077b3 commit 6c2cbec
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/antlr4/imports/COMMAND_EVAL_SUBMODE_BASE.g4
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ COMMAND_EVAL_SUBMODE_BASE_ASINH: 'asinh' -> type(EVAL_LANGUAGE_MODE_ASINH);
COMMAND_EVAL_SUBMODE_BASE_ATAN: 'atan' -> type(EVAL_LANGUAGE_MODE_ATAN);
COMMAND_EVAL_SUBMODE_BASE_ATAN2: 'atan2' -> type(EVAL_LANGUAGE_MODE_ATAN2);
COMMAND_EVAL_SUBMODE_BASE_ATANH: 'atanh' -> type(EVAL_LANGUAGE_MODE_ATANH);
COMMAND_EVAL_SUBMODE_BASE_AVG: 'avg' -> type(EVAL_LANGUAGE_MODE_AVG);
COMMAND_EVAL_SUBMODE_BASE_CASE: 'case' -> type(EVAL_LANGUAGE_MODE_CASE);
COMMAND_EVAL_SUBMODE_BASE_CEILING: ('ceil'|'ceiling') -> type(EVAL_LANGUAGE_MODE_CEILING);
COMMAND_EVAL_SUBMODE_BASE_CIDRMATCH: 'cidrmatch' -> type(EVAL_LANGUAGE_MODE_CIDRMATCH);
Expand Down
5 changes: 5 additions & 0 deletions src/main/antlr4/imports/DPLParserEvals.g4
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ evalFunctionStatement
| evalMethodAtan // NumericFunction
| evalMethodAtan2 // NumericFunction
| evalMethodAtanh // NumericFunction
| evalMethodAvg // NumericFunction
| evalMethodCeiling // NumericFunction
| evalMethodCos // NumericFunction
| evalMethodCosh // NumericFunction
Expand Down Expand Up @@ -213,6 +214,10 @@ evalMethodAtanh
: EVAL_LANGUAGE_MODE_ATANH EVAL_LANGUAGE_MODE_PARENTHESIS_L evalStatement EVAL_LANGUAGE_MODE_PARENTHESIS_R
;

evalMethodAvg
: EVAL_LANGUAGE_MODE_AVG EVAL_LANGUAGE_MODE_PARENTHESIS_L evalStatement (EVAL_LANGUAGE_MODE_COMMA evalStatement)+ EVAL_LANGUAGE_MODE_PARENTHESIS_R
;

evalMethodCase
: EVAL_LANGUAGE_MODE_CASE EVAL_LANGUAGE_MODE_PARENTHESIS_L evalStatement (EVAL_LANGUAGE_MODE_COMMA evalStatement)+ EVAL_LANGUAGE_MODE_PARENTHESIS_R
;
Expand Down
1 change: 1 addition & 0 deletions src/main/antlr4/imports/EVAL_LANGUAGE_MODE.g4
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ EVAL_LANGUAGE_MODE_ASINH: 'asinh';
EVAL_LANGUAGE_MODE_ATAN: 'atan';
EVAL_LANGUAGE_MODE_ATAN2: 'atan2';
EVAL_LANGUAGE_MODE_ATANH: 'atanh';
EVAL_LANGUAGE_MODE_AVG: 'avg';
EVAL_LANGUAGE_MODE_CASE: 'case';
EVAL_LANGUAGE_MODE_CEILING: ('ceil'|'ceiling');
EVAL_LANGUAGE_MODE_CIDRMATCH: 'cidrmatch';
Expand Down
33 changes: 32 additions & 1 deletion src/test/java/com/teragrep/pth_03/tests/EvalSyntaxTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public class EvalSyntaxTests {
"eval_sum",
"eval_multiple_statements",
"eval_multiple_statements_with_functions",
"eval_validate"
"eval_validate",
"eval_avg"
})
public void evalSyntaxParseTest(String arg) {
String fileName = "src/test/resources/antlr4/commands/eval/" + arg + ".txt";
Expand Down Expand Up @@ -434,6 +435,36 @@ void xpathTest25(String arg) {
assertEquals(1, nodesA.getLength());
}

@ParameterizedTest
@ValueSource(strings = {
"eval_avg"
})
void avgTest(String arg) {
ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
String fileName = "src/test/resources/antlr4/commands/eval/" + arg + ".txt";

String avgMethod = "/root/transformStatement/evalTransformation/t_eval_evalParameter/evalStatement/evalFunctionStatement/evalMethodAvg";
String parameters = "/root/transformStatement/evalTransformation/t_eval_evalParameter/evalStatement/evalFunctionStatement/evalMethodAvg/evalStatement";
String fieldParam = "/root/transformStatement/evalTransformation/t_eval_evalParameter/evalStatement/evalFunctionStatement/evalMethodAvg/evalStatement[1]/evalFieldType";
String numberParam = "/root/transformStatement/evalTransformation/t_eval_evalParameter/evalStatement/evalFunctionStatement/evalMethodAvg/evalStatement[2]/evalNumberType";
String stringParam = "/root/transformStatement/evalTransformation/t_eval_evalParameter/evalStatement/evalFunctionStatement/evalMethodAvg/evalStatement[3]/evalStringType";

NodeList avgNode = Assertions.assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, avgMethod, false));
assertEquals(1, avgNode.getLength());

NodeList evalStmts = Assertions.assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, parameters, false));
assertEquals(3, evalStmts.getLength());

NodeList fieldNode = Assertions.assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, fieldParam, false));
assertEquals(1, fieldNode.getLength());

NodeList numberNode = Assertions.assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, numberParam, false));
assertEquals(1, numberNode.getLength());

NodeList stringNode = Assertions.assertDoesNotThrow(() -> (NodeList) pstu.xpathQueryFile(fileName, stringParam, false));
assertEquals(1, stringNode.getLength());
}

@ParameterizedTest
@ValueSource(strings = {
"eval_sum"
Expand Down
46 changes: 46 additions & 0 deletions src/test/resources/antlr4/commands/eval/eval_avg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->
| eval a = avg(offset, 1, "1")

0 comments on commit 6c2cbec

Please sign in to comment.