NIFI-1662: Added support for decimal literal in Expression Language

This commit is contained in:
Matt Burgess 2016-10-18 09:47:17 -04:00
parent 94ab999026
commit 557e0b9f27
5 changed files with 60 additions and 1 deletions

View File

@ -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';

View File

@ -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;

View File

@ -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);

View File

@ -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<Double> evaluate(final Map<String, String> attributes) {
return new DecimalQueryResult(literal);
}
@Override
public Evaluator<?> getSubjectEvaluator() {
return null;
}
}

View File

@ -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");
}