From 557e0b9f27fe8fd99aa0ac6feda597ea26fc3357 Mon Sep 17 00:00:00 2001 From: Matt Burgess Date: Tue, 18 Oct 2016 09:47:17 -0400 Subject: [PATCH] NIFI-1662: Added support for decimal literal in Expression Language --- .../language/antlr/AttributeExpressionLexer.g | 7 +++ .../antlr/AttributeExpressionParser.g | 2 +- .../attribute/expression/language/Query.java | 5 +++ .../literals/DecimalLiteralEvaluator.java | 44 +++++++++++++++++++ .../expression/language/TestQuery.java | 3 ++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java diff --git a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g index 31dfe90593..34ef48be56 100644 --- a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g +++ b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g @@ -79,6 +79,13 @@ DOT : '.'; SEMICOLON : ';'; WHOLE_NUMBER : ('0'..'9')+; +DECIMAL : ('0'..'9')+ '.' ('0'..'9')* EXP? + | '.' ('0'..'9')+ EXP? + | ('0'..'9')+ EXP + | ('0'..'9')+ ; + +fragment EXP : ('e'|'E') ('+'|'-')? ('0'..'9')+ ; + TRUE : 'true'; FALSE : 'false'; diff --git a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g index 2eb8e7bf5e..64644c6751 100644 --- a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g +++ b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g @@ -100,7 +100,7 @@ stringFunctionRef : zeroArgString | oneArgString | twoArgString | fiveArgString; booleanFunctionRef : zeroArgBool | oneArgBool | multiArgBool; numberFunctionRef : zeroArgNum | oneArgNum; -anyArg : WHOLE_NUMBER | numberFunctionRef | STRING_LITERAL | zeroArgString | oneArgString | twoArgString | fiveArgString | booleanLiteral | zeroArgBool | oneArgBool | multiArgBool | expression; +anyArg : WHOLE_NUMBER | DECIMAL | numberFunctionRef | STRING_LITERAL | zeroArgString | oneArgString | twoArgString | fiveArgString | booleanLiteral | zeroArgBool | oneArgBool | multiArgBool | expression; stringArg : STRING_LITERAL | zeroArgString | oneArgString | twoArgString | expression; functionRef : stringFunctionRef | booleanFunctionRef | numberFunctionRef; diff --git a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java index b0c2ab6f16..e8b1f31898 100644 --- a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java +++ b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java @@ -97,6 +97,7 @@ import org.apache.nifi.attribute.expression.language.evaluation.functions.Base64 import org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.UuidEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.literals.BooleanLiteralEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.literals.DecimalLiteralEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.literals.ToLiteralEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.literals.WholeNumberLiteralEvaluator; @@ -132,6 +133,7 @@ import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpre import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ATTRIBUTE_REFERENCE; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ATTR_NAME; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.CONTAINS; +import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.DECIMAL; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.IN; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.COUNT; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.DIVIDE; @@ -680,6 +682,9 @@ public class Query { case STRING_LITERAL: { return newStringLiteralEvaluator(tree.getText()); } + case DECIMAL: { + return new DecimalLiteralEvaluator(tree.getText()); + } case TRUE: case FALSE: return buildBooleanEvaluator(tree); diff --git a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java new file mode 100644 index 0000000000..9673e165fb --- /dev/null +++ b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.literals; + +import org.apache.nifi.attribute.expression.language.evaluation.DecimalEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.DecimalQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +import java.util.Map; + + +public class DecimalLiteralEvaluator extends DecimalEvaluator { + + private final double literal; + + public DecimalLiteralEvaluator(final String value) { + this.literal = Double.parseDouble(value); + } + + @Override + public QueryResult evaluate(final Map attributes) { + return new DecimalQueryResult(literal); + } + + @Override + public Evaluator getSubjectEvaluator() { + return null; + } +} diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java index 0eefab554e..01183eceb2 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java @@ -807,6 +807,9 @@ public class TestQuery { // The expected resulted is calculated instead of a set number due to the inaccuracy of double arithmetic verifyEquals("${ten:divide(${two:plus(3)}):toDecimal()}", attributes, (10.1 / (2.2 + 3))); + // The expected resulted is calculated instead of a set number due to the inaccuracy of double arithmetic + verifyEquals("${ten:divide(${two:plus(3.1)}):toDecimal()}", attributes, (10.1 / (2.2 + 3.1))); + verifyEquals("${ten:divide(${two:plus(3)}):toDate():format(\"SSS\")}", attributes, "001"); }