use of org.apache.nifi.record.path.RecordPathEvaluationContext in project nifi by apache.
the class BinaryOperatorFilter method filter.
@Override
public Stream<FieldValue> filter(final RecordPathEvaluationContext context, final boolean invert) {
final Stream<FieldValue> rhsStream = rhs.evaluate(context);
final Optional<FieldValue> firstMatch = rhsStream.filter(fieldVal -> fieldVal.getValue() != null).findFirst();
if (!firstMatch.isPresent()) {
return Stream.empty();
}
final FieldValue fieldValue = firstMatch.get();
final Object value = fieldValue.getValue();
final Stream<FieldValue> lhsStream = lhs.evaluate(context);
return lhsStream.filter(fieldVal -> {
final boolean result = test(fieldVal, value);
return invert ? !result : result;
});
}
use of org.apache.nifi.record.path.RecordPathEvaluationContext in project nifi by apache.
the class Substring method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> fieldValues = recordPath.evaluate(context);
return fieldValues.filter(fv -> fv.getValue() != null).map(fv -> {
final OptionalInt startIndex = getIndex(startIndexPath, context);
if (!startIndex.isPresent()) {
return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
}
final OptionalInt endIndex = getIndex(endIndexPath, context);
if (!endIndex.isPresent()) {
return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
}
final int start = startIndex.getAsInt();
final int end = endIndex.getAsInt();
final String value = DataTypeUtils.toString(fv.getValue(), (String) null);
// Allow for negative indices to be used to reference offset from string length. We add 1 here because we want -1 to refer
// to the actual length of the string.
final int evaluatedEndIndex = end < 0 ? value.length() + 1 + end : end;
final int evaluatedStartIndex = start < 0 ? value.length() + 1 + start : start;
if (evaluatedEndIndex <= evaluatedStartIndex || evaluatedStartIndex < 0 || evaluatedStartIndex > value.length()) {
return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
}
final String substring = value.substring(evaluatedStartIndex, Math.min(evaluatedEndIndex, value.length()));
return new StandardFieldValue(substring, fv.getField(), fv.getParent().orElse(null));
});
}
use of org.apache.nifi.record.path.RecordPathEvaluationContext in project nifi by apache.
the class MultiArrayIndexPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).flatMap(fieldValue -> {
final ArrayDataType arrayDataType = (ArrayDataType) fieldValue.getField().getDataType();
final DataType elementDataType = arrayDataType.getElementType();
final RecordField arrayField = new RecordField(fieldValue.getField().getFieldName(), elementDataType);
final Object[] values = (Object[]) fieldValue.getValue();
return indices.stream().filter(range -> values.length > Math.abs(range.getMin())).flatMap(range -> {
final int min = range.getMin() < 0 ? values.length + range.getMin() : range.getMin();
final int max = range.getMax() < 0 ? values.length + range.getMax() : range.getMax();
final List<FieldValue> indexFieldValues = new ArrayList<>(Math.max(0, max - min));
for (int i = min; i <= max; i++) {
if (values.length > i) {
final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
final FieldValue arrayIndexFieldValue = new ArrayIndexFieldValue(values[i], elementField, fieldValue, i);
indexFieldValues.add(arrayIndexFieldValue);
}
}
return indexFieldValues.stream();
});
});
}
use of org.apache.nifi.record.path.RecordPathEvaluationContext in project nifi by apache.
the class RecordPathSegment method evaluate.
@Override
public final RecordPathResult evaluate(final Record record, final FieldValue contextNode) {
final RecordPathEvaluationContext context = new StandardRecordPathEvaluationContext(record);
context.setContextNode(contextNode);
final Stream<FieldValue> selectedFields = evaluate(context);
return new RecordPathResult() {
@Override
public String getPath() {
return RecordPathSegment.this.getPath();
}
@Override
public Stream<FieldValue> getSelectedFields() {
return selectedFields;
}
};
}
Aggregations