Search in sources :

Example 1 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class Query method prepare.

public static PreparedQuery prepare(final String query) throws AttributeExpressionLanguageParsingException {
    if (query == null) {
        return new EmptyPreparedQuery(null);
    }
    final List<Range> ranges = extractExpressionRanges(query);
    if (ranges.isEmpty()) {
        return new EmptyPreparedQuery(query.replace("$$", "$"));
    }
    final ExpressionCompiler compiler = new ExpressionCompiler();
    try {
        final List<Expression> expressions = new ArrayList<>();
        int lastIndex = 0;
        for (final Range range : ranges) {
            if (range.getStart() > lastIndex) {
                final String substring = query.substring(lastIndex, range.getStart()).replace("$$", "$");
                expressions.add(new StringLiteralExpression(substring));
                lastIndex = range.getEnd() + 1;
            }
            final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$");
            final CompiledExpression compiledExpression = compiler.compile(treeText);
            expressions.add(compiledExpression);
            lastIndex = range.getEnd() + 1;
        }
        final Range lastRange = ranges.get(ranges.size() - 1);
        if (lastRange.getEnd() + 1 < query.length()) {
            final String treeText = query.substring(lastRange.getEnd() + 1).replace("$$", "$");
            expressions.add(new StringLiteralExpression(treeText));
        }
        return new StandardPreparedQuery(expressions);
    } catch (final AttributeExpressionLanguageParsingException e) {
        return new InvalidPreparedQuery(query, e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) ExpressionCompiler(org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler)

Example 2 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class ExpressionCompiler method buildFunctionExpressionEvaluator.

private Evaluator<?> buildFunctionExpressionEvaluator(final Tree tree, final int offset) {
    if (tree.getChildCount() == 0) {
        throw new AttributeExpressionLanguageParsingException("EXPRESSION tree node has no children");
    }
    final int firstChildIndex = tree.getChildCount() - offset - 1;
    if (firstChildIndex == 0) {
        return buildEvaluator(tree.getChild(0));
    }
    final Tree functionTree = tree.getChild(firstChildIndex);
    final Evaluator<?> subjectEvaluator = buildFunctionExpressionEvaluator(tree, offset + 1);
    final Tree functionNameTree = functionTree.getChild(0);
    final List<Evaluator<?>> argEvaluators = new ArrayList<>();
    for (int i = 1; i < functionTree.getChildCount(); i++) {
        argEvaluators.add(buildEvaluator(functionTree.getChild(i)));
    }
    return buildFunctionEvaluator(functionNameTree, subjectEvaluator, argEvaluators);
}
Also used : ArrayList(java.util.ArrayList) Tree(org.antlr.runtime.tree.Tree) IfElseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IfElseEvaluator) OrEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OrEvaluator) GreaterThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanEvaluator) LastIndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LastIndexOfEvaluator) UrlEncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlEncodeEvaluator) IteratingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator) SubstringAfterLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterLastEvaluator) ReplaceNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceNullEvaluator) ToLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.ToLiteralEvaluator) AppendEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AppendEvaluator) GetStateVariableEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetStateVariableEvaluator) MinusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MinusEvaluator) ReplaceAllEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceAllEvaluator) Base64DecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64DecodeEvaluator) ToStringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToStringEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) AttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AttributeEvaluator) GreaterThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanOrEqualEvaluator) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) MappingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator) AndEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AndEvaluator) NumberToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NumberToDateEvaluator) OneUpSequenceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OneUpSequenceEvaluator) StringLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator) WholeNumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.WholeNumberCastEvaluator) IsEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsEmptyEvaluator) InEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.InEvaluator) LessThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanOrEqualEvaluator) UuidEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UuidEvaluator) IPEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IPEvaluator) EqualsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsEvaluator) CharSequenceTranslatorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.CharSequenceTranslatorEvaluator) IsNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsNullEvaluator) NowEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NowEvaluator) IndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IndexOfEvaluator) CountEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.CountEvaluator) ToRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator) ReplaceFirstEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceFirstEvaluator) ToUpperEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToUpperEvaluator) ReplaceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEvaluator) EqualsIgnoreCaseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsIgnoreCaseEvaluator) ContainsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ContainsEvaluator) PlusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PlusEvaluator) FindEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FindEvaluator) MatchesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MatchesEvaluator) Evaluator(org.apache.nifi.attribute.expression.language.evaluation.Evaluator) ModEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ModEvaluator) Base64EncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator) GetDelimitedFieldEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetDelimitedFieldEvaluator) DateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator) TrimEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.TrimEvaluator) FromRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FromRadixEvaluator) JsonPathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.JsonPathEvaluator) BooleanLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.BooleanLiteralEvaluator) DecimalLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.DecimalLiteralEvaluator) LessThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanEvaluator) WholeNumberLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.WholeNumberLiteralEvaluator) StringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator) LengthEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LengthEvaluator) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) SubstringAfterEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterEvaluator) MathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MathEvaluator) MultiplyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MultiplyEvaluator) AllAttributesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator) SubstringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringEvaluator) MultiNamedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator) NotEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotEvaluator) SubstringBeforeLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeLastEvaluator) FormatEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FormatEvaluator) StartsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StartsWithEvaluator) PrependEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PrependEvaluator) EndsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EndsWithEvaluator) ToLowerEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToLowerEvaluator) MultiMatchAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator) BooleanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator) ReplaceEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEmptyEvaluator) DelineatedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.DelineatedAttributeEvaluator) NumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator) HostnameEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.HostnameEvaluator) StringCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.StringCastEvaluator) BooleanCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator) UrlDecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlDecodeEvaluator) RandomNumberGeneratorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.RandomNumberGeneratorEvaluator) NotNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotNullEvaluator) DecimalCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator) JoinEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.JoinEvaluator) SubstringBeforeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeEvaluator) StringToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StringToDateEvaluator) DateCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator) DivideEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.DivideEvaluator) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)

Example 3 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class ExpressionCompiler method compileTree.

private Tree compileTree(final String expression) throws AttributeExpressionLanguageParsingException {
    try {
        final CharStream input = new ANTLRStringStream(expression);
        final AttributeExpressionLexer lexer = new AttributeExpressionLexer(input);
        final CommonTokenStream lexerTokenStream = new CommonTokenStream(lexer);
        final AttributeExpressionParser parser = new AttributeExpressionParser(lexerTokenStream);
        final Tree ast = (Tree) parser.query().getTree();
        final Tree tree = ast.getChild(0);
        // ensure that we are able to build the evaluators, so that we validate syntax
        final Evaluator<?> evaluator = buildEvaluator(tree);
        verifyMappingEvaluatorReduced(evaluator);
        return tree;
    } catch (final AttributeExpressionLanguageParsingException e) {
        throw e;
    } catch (final Exception e) {
        throw new AttributeExpressionLanguageParsingException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) AttributeExpressionParser(org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser) Tree(org.antlr.runtime.tree.Tree) AttributeExpressionLexer(org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionLexer) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) CharStream(org.antlr.runtime.CharStream) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) UnknownHostException(java.net.UnknownHostException) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)

Example 4 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class ExpressionCompiler method verifyMappingEvaluatorReduced.

private void verifyMappingEvaluatorReduced(final Evaluator<?> evaluator) {
    final Evaluator<?> rightMostEvaluator;
    if (evaluator instanceof IteratingEvaluator) {
        rightMostEvaluator = ((IteratingEvaluator<?>) evaluator).getLogicEvaluator();
    } else {
        rightMostEvaluator = evaluator;
    }
    Evaluator<?> eval = rightMostEvaluator.getSubjectEvaluator();
    Evaluator<?> lastEval = rightMostEvaluator;
    while (eval != null) {
        if (eval instanceof ReduceEvaluator) {
            throw new AttributeExpressionLanguageParsingException("Expression attempts to call function '" + lastEval.getToken() + "' on the result of '" + eval.getToken() + "'. This is not allowed. Instead, use \"${literal( ${<embedded expression>} ):" + lastEval.getToken() + "(...)}\"");
        }
        lastEval = eval;
        eval = eval.getSubjectEvaluator();
    }
    // if the result type of the evaluator is BOOLEAN, then it will always
    // be reduced when evaluator.
    final ResultType resultType = evaluator.getResultType();
    if (resultType == ResultType.BOOLEAN) {
        return;
    }
    final Evaluator<?> rootEvaluator = getRootSubjectEvaluator(evaluator);
    if (rootEvaluator != null && rootEvaluator instanceof MultiAttributeEvaluator) {
        final MultiAttributeEvaluator multiAttrEval = (MultiAttributeEvaluator) rootEvaluator;
        switch(multiAttrEval.getEvaluationType()) {
            case ALL_ATTRIBUTES:
            case ALL_MATCHING_ATTRIBUTES:
            case ALL_DELINEATED_VALUES:
                {
                    if (!(evaluator instanceof ReduceEvaluator)) {
                        throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
                    }
                    break;
                }
            default:
                throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
        }
    }
}
Also used : IteratingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator) ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)

Example 5 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class Query method compile.

public static Query compile(final String query) throws AttributeExpressionLanguageParsingException {
    try {
        final ExpressionCompiler compiler = new ExpressionCompiler();
        final CompiledExpression compiledExpression = compiler.compile(query);
        return new Query(compiledExpression.getExpression(), compiledExpression.getTree(), compiledExpression.getRootEvaluator());
    } catch (final AttributeExpressionLanguageParsingException e) {
        throw e;
    } catch (final Exception e) {
        throw new AttributeExpressionLanguageParsingException(e);
    }
}
Also used : ExpressionCompiler(org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Aggregations

AttributeExpressionLanguageParsingException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)7 ReduceEvaluator (org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator)4 MultiAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator)4 Tree (org.antlr.runtime.tree.Tree)3 BooleanEvaluator (org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator)3 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CharStream (org.antlr.runtime.CharStream)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 AttributeExpressionLexer (org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionLexer)2 AttributeExpressionParser (org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser)2 ExpressionCompiler (org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler)2 DateEvaluator (org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator)2 Evaluator (org.apache.nifi.attribute.expression.language.evaluation.Evaluator)2 StringEvaluator (org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator)2 BooleanCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator)2 DateCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator)2 DecimalCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator)2 NumberCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator)2