Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Date search doesn't match ranges properly #4762

Merged
merged 8 commits into from
Feb 5, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,28 @@ public override Expression VisitSearchParameter(SearchParameterExpression expres
public override Expression VisitMultiary(MultiaryExpression expression, object context)
{
expression = (MultiaryExpression)base.VisitMultiary(expression, context);
bool checkDateTimeAgainstPeriod = false;
if (expression.MultiaryOperation != MultiaryOperator.And)
{
return expression;
}

List<Expression> newExpressions = null;
List<Expression> dateInPeriodExpression = null;
int i = 0;
for (; i < expression.Expressions.Count - 1; i++)
{
switch (MatchPattern(expression.Expressions[i], expression.Expressions[i + 1]))
{
case ({ } low, { } high):
EnsureAllocatedAndPopulated(ref newExpressions, expression.Expressions, i);

EnsureAllocatedAndPopulated(ref dateInPeriodExpression, expression.Expressions, i);
newExpressions.Add(low);
newExpressions.Add(new BinaryExpression(high.BinaryOperator, low.FieldName, high.ComponentIndex, high.Value));
newExpressions.Add(high);

checkDateTimeAgainstPeriod = true;
dateInPeriodExpression.Add(new BinaryExpression(high.BinaryOperator, low.FieldName, low.ComponentIndex, low.Value));
dateInPeriodExpression.Add(new BinaryExpression(low.BinaryOperator, high.FieldName, high.ComponentIndex, high.Value));
i++;
break;
default:
Expand All @@ -65,7 +69,7 @@ public override Expression VisitMultiary(MultiaryExpression expression, object c
newExpressions.Add(expression.Expressions[^1]);
}

return newExpressions == null ? expression : Expression.And(newExpressions);
return newExpressions == null ? expression : checkDateTimeAgainstPeriod ? Expression.Or(Expression.And(newExpressions), Expression.And(dateInPeriodExpression)) : Expression.And(newExpressions);
}

private static (BinaryExpression low, BinaryExpression high) MatchPattern(Expression e1, Expression e2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ protected override async Task OnInitializedAsync()
};

Observations = await TestFhirClient.CreateResourcesAsync<Observation>(
p => SetObservation(p, "1979-12-31"), // 1979-12-31T00:00:00.0000000 <-> 1979-12-31T23:59:59.9999999
p => SetObservation(p, "1980"), // 1980-01-01T00:00:00.0000000 <-> 1980-12-31T23:59:59.9999999
p => SetObservation(p, "1980-05"), // 1980-05-01T00:00:00.0000000 <-> 1980-05-31T23:59:59.9999999
p => SetObservation(p, "1980-05-11"), // 1980-05-11T00:00:00.0000000 <-> 1980-05-11T23:59:59.9999999
p => SetObservation(p, "1980-05-11T16:32:15"), // 1980-05-11T16:32:15.0000000 <-> 1980-05-11T16:32:15.9999999
p => SetObservation(p, "1980-05-11T16:32:15.500"), // 1980-05-11T16:32:15.5000000 <-> 1980-05-11T16:32:15.5000000
p => SetObservation(p, "1981-01-01")); // 1981-01-01T00:00:00.0000000 <-> 1981-12-31T23:59:59.9999999
p => SetObservationWithPeriod(p, "1980-05-16", "1980-05-17")); // 1980-05-11T00:00:00.0000000 <-> 1980-05-12T23:59:59.9999999

// p => SetObservation(p, "1979-12-31"), // 1979-12-31T00:00:00.0000000 <-> 1979-12-31T23:59:59.9999999
// p => SetObservation(p, "1980"), // 1980-01-01T00:00:00.0000000 <-> 1980-12-31T23:59:59.9999999
// p => SetObservation(p, "1980-05"), // 1980-05-01T00:00:00.0000000 <-> 1980-05-31T23:59:59.9999999
// p => SetObservation(p, "1980-05-11"), // 1980-05-11T00:00:00.0000000 <-> 1980-05-11T23:59:59.9999999
// p => SetObservation(p, "1980-05-11T16:32:15"), // 1980-05-11T16:32:15.0000000 <-> 1980-05-11T16:32:15.9999999
// p => SetObservation(p, "1980-05-11T16:32:15.500"), // 1980-05-11T16:32:15.5000000 <-> 1980-05-11T16:32:15.5000000
// p => SetObservation(p, "1981-01-01"), // 1981-01-01T00:00:00.0000000 <-> 1981-12-31T23:59:59.9999999
/*
void SetObservation(Observation observation, string date)
{
observation.Status = ObservationStatus.Final;
Expand All @@ -52,6 +54,19 @@ void SetObservation(Observation observation, string date)
},
};
observation.Effective = new FhirDateTime(date);
}*/

void SetObservationWithPeriod(Observation observation, string startDate, string endDate)
{
observation.Status = ObservationStatus.Final;
observation.Code = new CodeableConcept
{
Coding = new List<Coding>
{
Coding,
},
};
observation.Effective = new Period(new FhirDateTime(startDate), new FhirDateTime(endDate));
}
}
}
Expand Down
Loading
Loading