Skip to content

Commit

Permalink
Adding pruning of synthetic fields in grouping columns for AggregateQ…
Browse files Browse the repository at this point in the history
…ueryExecutor and enabling test cases with issues resolved in previous commits.
  • Loading branch information
piotrszul committed Jan 23, 2025
1 parent 07b4b6e commit 8bfb834
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ void queryWithIntegerGroupings() {
response);
}

@Disabled("TODO: strong FHIR typing for mathematical operations, e.g: "
+ "positiveInteger + positiveInteger = positiveInteger")
@Test
void queryWithMathExpression() {
subjectResource = ResourceType.CLAIM;
Expand Down Expand Up @@ -142,7 +140,6 @@ void queryWithChoiceElement() {
response);
}

@Disabled("TODO: toExpression() - parentheses and operator precedence")
@Test
void queryWithDateComparison() {
subjectResource = ResourceType.PATIENT;
Expand Down Expand Up @@ -329,7 +326,6 @@ void queryWithWhereAsComparisonOperand() {
response);
}

@Disabled("TODO: toExpression() - parentheses and operator precedence")
@Test
void queryWithAmbiguousSelfJoin() {
subjectResource = ResourceType.MEDICATIONREQUEST;
Expand Down Expand Up @@ -362,7 +358,6 @@ void queryWithWhereAndMembership() {
response);
}

@Disabled("TODO: toExpression() - parentheses and operator precedence")
@Test
void queryWithWhereAndBoolean() {
subjectResource = ResourceType.PATIENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import au.csiro.pathling.fhirpath.path.Paths;
import au.csiro.pathling.io.Database;
import au.csiro.pathling.io.source.DataSource;
import au.csiro.pathling.sql.SqlExpressions;
import au.csiro.pathling.terminology.TerminologyServiceFactory;
import ca.uhn.fhir.context.FhirContext;
import jakarta.annotation.Nonnull;
Expand Down Expand Up @@ -134,6 +135,7 @@ public ResultWithExpressions buildQuery(@Nonnull final AggregateRequest query) {

// now we need to explode all array columns in the dataset
final Dataset<Row> expandeDataset = Stream.of(inputDataset.schema().fields())
.skip(1)
.reduce(inputDataset, (dataset, field) -> {
if (field.dataType() instanceof ArrayType) {
return dataset.withColumn(field.name(),
Expand All @@ -146,12 +148,14 @@ public ResultWithExpressions buildQuery(@Nonnull final AggregateRequest query) {
// The combiner function is used to aggregate partial aggregation results from agg fhirpaths.
// For example for `count()` the aggregation is SQL SUM(), for

final Column[] expandedColumns = Stream.of(expandeDataset.columns())
final Column valueColumn = Stream.of(expandeDataset.columns())
.limit(1)
.map(expandeDataset::col)
.toArray(Column[]::new);
.findFirst().orElseThrow();

final Column[] groupingColumns = Stream.of(expandedColumns)
final Column[] groupingColumns = Stream.of(expandeDataset.columns())
.skip(1)
.map(c -> SqlExpressions.pruneSyntheticFields(functions.col(c)).alias(c))
.toArray(Column[]::new);

final FhirpathEvaluator aggEvaluator = SingleFhirpathEvaluator.of(query.getSubjectResource(),
Expand All @@ -161,7 +165,7 @@ public ResultWithExpressions buildQuery(@Nonnull final AggregateRequest query) {

//then we need to group by the grouping columns and collect the resource column to a list
final Dataset<Row> grouppedAggSource = expandeDataset.groupBy(groupingColumns)
.agg(functions.collect_list(expandedColumns[0]).alias(query.getSubjectResource().toCode()));
.agg(functions.collect_list(valueColumn).alias(query.getSubjectResource().toCode()));

final List<EvaluatedPath> evaluatedAggs = evalPaths(aggPaths, aggEvaluator);
final Column[] aggColumns = evaluatedAggs.stream()
Expand All @@ -178,7 +182,7 @@ public ResultWithExpressions buildQuery(@Nonnull final AggregateRequest query) {
evaluatedFilters
);
}

@Nonnull
List<EvaluatedPath> evalPaths(@Nonnull final List<FhirPath> paths,
@Nonnull final FhirpathEvaluator fhirEvaluator) {
Expand Down

0 comments on commit 8bfb834

Please sign in to comment.