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