Search in sources :

Example 1 with Comparable

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);
}
Also used : Comparable(au.csiro.pathling.fhirpath.Comparable) Operator.checkArgumentsAreComparable(au.csiro.pathling.fhirpath.operator.Operator.checkArgumentsAreComparable) FhirPath(au.csiro.pathling.fhirpath.FhirPath) DatasetWithColumn(au.csiro.pathling.QueryHelpers.DatasetWithColumn) QueryHelpers.createColumn(au.csiro.pathling.QueryHelpers.createColumn) NonLiteralPath.findEidColumn(au.csiro.pathling.fhirpath.NonLiteralPath.findEidColumn) Column(org.apache.spark.sql.Column) NonLiteralPath.findThisColumn(au.csiro.pathling.fhirpath.NonLiteralPath.findThisColumn) DatasetWithColumn(au.csiro.pathling.QueryHelpers.DatasetWithColumn) Row(org.apache.spark.sql.Row) Nonnull(javax.annotation.Nonnull)

Example 2 with Comparable

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);
}
Also used : Comparable(au.csiro.pathling.fhirpath.Comparable) Operator.checkArgumentsAreComparable(au.csiro.pathling.fhirpath.operator.Operator.checkArgumentsAreComparable) FhirPath(au.csiro.pathling.fhirpath.FhirPath) Column(org.apache.spark.sql.Column) Row(org.apache.spark.sql.Row) Nonnull(javax.annotation.Nonnull)

Example 3 with Comparable

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);
}
Also used : Comparable(au.csiro.pathling.fhirpath.Comparable) FhirPath(au.csiro.pathling.fhirpath.FhirPath)

Aggregations

Comparable (au.csiro.pathling.fhirpath.Comparable)3 FhirPath (au.csiro.pathling.fhirpath.FhirPath)3 Operator.checkArgumentsAreComparable (au.csiro.pathling.fhirpath.operator.Operator.checkArgumentsAreComparable)2 Nonnull (javax.annotation.Nonnull)2 Column (org.apache.spark.sql.Column)2 Row (org.apache.spark.sql.Row)2 DatasetWithColumn (au.csiro.pathling.QueryHelpers.DatasetWithColumn)1 QueryHelpers.createColumn (au.csiro.pathling.QueryHelpers.createColumn)1 NonLiteralPath.findEidColumn (au.csiro.pathling.fhirpath.NonLiteralPath.findEidColumn)1 NonLiteralPath.findThisColumn (au.csiro.pathling.fhirpath.NonLiteralPath.findThisColumn)1