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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void GivenStartAndEndExpressions_WhenRewritten_AnUpperBoundOnStartIsAdded
Expression rewrittenExpression = inputExpression.AcceptVisitor(DateTimeEqualityRewriter.Instance);

Assert.Equal(
"(And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000))",
"(Or (And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000)) (And (FieldLessThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldGreaterThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000)))",
rewrittenExpression.ToString());
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public void GivenStartAndEndExpressionsInReverseOrder_WhenRewritten_AnUpperBound
Expression rewrittenExpression = inputExpression.AcceptVisitor(DateTimeEqualityRewriter.Instance);

Assert.Equal(
"(And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000))",
"(Or (And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000)) (And (FieldLessThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldGreaterThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000)))",
rewrittenExpression.ToString());
}

Expand All @@ -72,7 +72,7 @@ public void GivenStartAndEndExclusiveExpressions_WhenRewritten_AnUpperBoundOnSta
Expression rewrittenExpression = inputExpression.AcceptVisitor(DateTimeEqualityRewriter.Instance);

Assert.Equal(
"(And (FieldGreaterThan DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThan DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThan DateTimeEnd 2021-01-01T23:59:59.0000000))",
"(Or (And (FieldGreaterThan DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThan DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThan DateTimeEnd 2021-01-01T23:59:59.0000000)) (And (FieldLessThan DateTimeStart 2021-01-01T00:00:00.0000000) (FieldGreaterThan DateTimeEnd 2021-01-01T23:59:59.0000000)))",
rewrittenExpression.ToString());
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public void GivenStartAndEndExpressionsAndAnUnrelatedExpression_WhenRewritten_An
Expression rewrittenExpression = inputExpression.AcceptVisitor(DateTimeEqualityRewriter.Instance);

Assert.Equal(
"(And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000) (FieldEqual Number 1))",
"(Or (And (FieldGreaterThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldLessThanOrEqual DateTimeStart 2021-01-01T23:59:59.0000000) (FieldLessThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000) (FieldEqual Number 1)) (And (FieldLessThanOrEqual DateTimeStart 2021-01-01T00:00:00.0000000) (FieldGreaterThanOrEqual DateTimeEnd 2021-01-01T23:59:59.0000000)))",
rewrittenExpression.ToString());
}

Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: For clarity, I think we shouldn't nest ternary operators :)

}

private static (BinaryExpression low, BinaryExpression high) MatchPattern(Expression e1, Expression e2)
Expand Down
Loading
Loading