use of au.csiro.pathling.fhirpath.Comparable in project pathling by aehrc.
the class ComparisonOperator method invoke.
@Nonnull
@Override
public FhirPath invoke(@Nonnull final OperatorInput input) {
final FhirPath left = input.getLeft();
final FhirPath right = input.getRight();
checkUserInput(left.isSingular(), "Left operand must be singular: " + left.getExpression());
checkUserInput(right.isSingular(), "Right operand must be singular: " + right.getExpression());
checkArgumentsAreComparable(input, type.toString());
final String expression = buildExpression(input, type.toString());
final Dataset<Row> dataset = join(input.getContext(), left, right, JoinType.LEFT_OUTER);
final Comparable leftComparable = (Comparable) left;
final Comparable rightComparable = (Comparable) right;
final Column valueColumn = leftComparable.getComparison(type).apply(rightComparable);
final Column idColumn = left.getIdColumn();
final Optional<Column> eidColumn = findEidColumn(left, right);
final Optional<Column> thisColumn = findThisColumn(left, right);
final DatasetWithColumn datasetWithColumn = createColumn(dataset, valueColumn);
return ElementPath.build(expression, datasetWithColumn.getDataset(), idColumn, eidColumn, datasetWithColumn.getColumn(), true, Optional.empty(), thisColumn, FHIRDefinedType.BOOLEAN);
}
use of au.csiro.pathling.fhirpath.Comparable in project pathling by aehrc.
the class MembershipOperator method invoke.
@Nonnull
@Override
public FhirPath invoke(@Nonnull final OperatorInput input) {
final FhirPath left = input.getLeft();
final FhirPath right = input.getRight();
final FhirPath element = type.equals(MembershipOperatorType.IN) ? left : right;
final FhirPath collection = type.equals(MembershipOperatorType.IN) ? right : left;
checkUserInput(element.isSingular(), "Element operand used with " + type + " operator is not singular: " + element.getExpression());
checkArgumentsAreComparable(input, type.toString());
final Column elementValue = element.getValueColumn();
final Column collectionValue = collection.getValueColumn();
final String expression = left.getExpression() + " " + type + " " + right.getExpression();
final Comparable leftComparable = (Comparable) left;
final Comparable rightComparable = (Comparable) right;
final Column equality = leftComparable.getComparison(ComparisonOperation.EQUALS).apply(rightComparable);
// If the left-hand side of the operator (element) is empty, the result is empty. If the
// right-hand side (collection) is empty, the result is false. Otherwise, a Boolean is returned
// based on whether the element is present in the collection, using equality semantics.
final Column equalityWithNullChecks = when(elementValue.isNull(), lit(null)).when(collectionValue.isNull(), lit(false)).otherwise(equality);
// We need to join the datasets in order to access values from both operands.
final Dataset<Row> dataset = join(input.getContext(), left, right, JoinType.LEFT_OUTER);
// In order to reduce the result to a single Boolean, we take the max of the boolean equality
// values.
final Column valueColumn = max(equalityWithNullChecks);
return buildAggregateResult(dataset, input.getContext(), Arrays.asList(left, right), valueColumn, expression, FHIRDefinedType.BOOLEAN);
}
use of au.csiro.pathling.fhirpath.Comparable in project pathling by aehrc.
the class Operator method checkArgumentsAreComparable.
/**
* Performs a set of validation checks on inputs that are intended to be used within a comparison
* operation.
*
* @param input The inputs to the operator
* @param operatorName The FHIRPath representation of the operator
*/
static void checkArgumentsAreComparable(@Nonnull final OperatorInput input, @Nonnull final String operatorName) {
final FhirPath left = input.getLeft();
final FhirPath right = input.getRight();
checkUserInput(left instanceof Comparable, operatorName + " operator does not support left operand: " + left.getExpression());
checkUserInput(right instanceof Comparable, operatorName + " operator does not support right operand: " + left.getExpression());
final Comparable leftComparable = (Comparable) left;
final Comparable rightComparable = (Comparable) right;
final String expression = buildExpression(input, operatorName);
checkUserInput(leftComparable.isComparableTo(rightComparable.getClass()), "Left operand to " + operatorName + " operator is not comparable to right operand: " + expression);
}
Aggregations