NIFI-22: fixed issues with newly added join and count functions

This commit is contained in:
Mark Payne 2015-01-07 14:16:51 -05:00
parent 49256d9d83
commit 14cba2a57e
4 changed files with 35 additions and 27 deletions

View File

@ -116,6 +116,7 @@ import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream; import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.Tree; import org.antlr.runtime.tree.Tree;
import org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator;
/** /**
* Class used for creating and evaluating NiFi Expression Language. Once a Query * Class used for creating and evaluating NiFi Expression Language. Once a Query
@ -559,7 +560,6 @@ public class Query {
} }
} }
private static void verifyMappingEvaluatorReduced(final Evaluator<?> evaluator) { private static void verifyMappingEvaluatorReduced(final Evaluator<?> evaluator) {
// if the result type of the evaluator is BOOLEAN, then it will always // if the result type of the evaluator is BOOLEAN, then it will always
// be reduced when evaluator. // be reduced when evaluator.
@ -572,16 +572,16 @@ public class Query {
if (rootEvaluator != null && rootEvaluator instanceof MultiAttributeEvaluator) { if (rootEvaluator != null && rootEvaluator instanceof MultiAttributeEvaluator) {
final MultiAttributeEvaluator multiAttrEval = (MultiAttributeEvaluator) rootEvaluator; final MultiAttributeEvaluator multiAttrEval = (MultiAttributeEvaluator) rootEvaluator;
switch (multiAttrEval.getEvaluationType()) { switch (multiAttrEval.getEvaluationType()) {
case ALL_ATTRIBUTES: case ALL_ATTRIBUTES:
case ALL_MATCHING_ATTRIBUTES: case ALL_MATCHING_ATTRIBUTES:
case ALL_DELINEATED_VALUES: { case ALL_DELINEATED_VALUES: {
if (!(evaluator instanceof ReduceEvaluator)) { if (!(evaluator instanceof ReduceEvaluator)) {
throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function"); throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
}
break;
} }
break; default:
} throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
default:
throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
} }
} }
} }
@ -819,9 +819,17 @@ public class Query {
break; break;
case ALL_ATTRIBUTES: case ALL_ATTRIBUTES:
case ALL_MATCHING_ATTRIBUTES: case ALL_MATCHING_ATTRIBUTES:
case ALL_DELINEATED_VALUES: case ALL_DELINEATED_VALUES: {
chosenEvaluator = new AllAttributesEvaluator((BooleanEvaluator) evaluator, multiAttrEval); final ResultType resultType = evaluator.getResultType();
if (resultType == ResultType.BOOLEAN) {
chosenEvaluator = new AllAttributesEvaluator((BooleanEvaluator) evaluator, multiAttrEval);
} else if (evaluator instanceof ReduceEvaluator) {
chosenEvaluator = new MappingEvaluator((ReduceEvaluator) evaluator, multiAttrEval);
} else {
throw new AttributeExpressionLanguageException("Cannot evaluate Expression because it attempts to reference multiple attributes but does not use a reducing function");
}
break; break;
}
} }
} }
} }

View File

@ -37,9 +37,9 @@ public class JoinEvaluator extends StringEvaluator implements ReduceEvaluator<St
@Override @Override
public QueryResult<String> evaluate(final Map<String, String> attributes) { public QueryResult<String> evaluate(final Map<String, String> attributes) {
final String subject = subjectEvaluator.evaluate(attributes).getValue(); String subject = subjectEvaluator.evaluate(attributes).getValue();
if ( subject == null ) { if ( subject == null ) {
return new StringQueryResult(""); subject = "";
} }
final String delimiter = delimiterEvaluator.evaluate(attributes).getValue(); final String delimiter = delimiterEvaluator.evaluate(attributes).getValue();

View File

@ -55,7 +55,7 @@ public class MappingEvaluator<T> implements Evaluator<T> {
@Override @Override
public Evaluator<?> getSubjectEvaluator() { public Evaluator<?> getSubjectEvaluator() {
return mappingEvaluator; return null;
} }
} }

View File

@ -235,8 +235,8 @@ public class TestQuery {
public void testJoin() { public void testJoin() {
final Map<String, String> attributes = new HashMap<>(); final Map<String, String> attributes = new HashMap<>();
attributes.put("a.a", "a"); attributes.put("a.a", "a");
attributes.put("b.b", "b"); attributes.put("a.b", "b");
attributes.put("c.c", "c"); attributes.put("a.c", "c");
verifyEquals("${allAttributes( 'a.a', 'a.b', 'a.c' ):join(', ')}", attributes, "a, b, c"); verifyEquals("${allAttributes( 'a.a', 'a.b', 'a.c' ):join(', ')}", attributes, "a, b, c");
verifyEquals("${x:join(', ')}", attributes, ""); verifyEquals("${x:join(', ')}", attributes, "");
verifyEquals("${a.a:join(', ')}", attributes, "a"); verifyEquals("${a.a:join(', ')}", attributes, "a");
@ -282,7 +282,7 @@ public class TestQuery {
public void testCount() { public void testCount() {
final Map<String, String> attributes = new HashMap<>(); final Map<String, String> attributes = new HashMap<>();
attributes.put("a", "a"); attributes.put("a", "a");
attributes.put("b", ""); attributes.put("b", "abc");
attributes.put("c", " \n"); attributes.put("c", " \n");
attributes.put("n1", "111"); attributes.put("n1", "111");
attributes.put("n2", "222"); attributes.put("n2", "222");