Skip to content

Commit

Permalink
TimeDimension Grain Arg match check between Having Filter and Project…
Browse files Browse the repository at this point in the history
…ion (#1999)

* TimeDimension Grain Arg match check between Having Filter and Projection

* Additional Test Case

* Additional Test Case

* Update AggregationDataStoreIntegrationTest.java
  • Loading branch information
moizarafat authored Apr 14, 2021
1 parent 699e5df commit 8010ad9
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.yahoo.elide.datastores.aggregation.query.MetricProjection;
import com.yahoo.elide.datastores.aggregation.query.Query;
import com.yahoo.elide.datastores.aggregation.query.Queryable;
import com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -84,6 +85,22 @@ private void validateHavingClause(FilterExpression havingClause) {
fieldName));
}
}

if (queriedTable.getTimeDimensionProjection(fieldName) != null) {
dimensionProjections
.stream()
.filter(dim -> dim.getAlias().equals(fieldName)
&& TimeDimensionProjection.class.isAssignableFrom(dim.getClass()))
.forEach(dim -> {
Object grain = dim.getArguments().get("grain").getValue();
if (last.getArguments().stream().noneMatch(arg -> (arg.getValue()).equals(grain))) {
throw new InvalidOperationException(
String.format(
"Time Dimension field %s must use the same grain argument in the projection"
+ " and the having clause.", fieldName));
}
});
}
} else if (havingClause instanceof AndFilterExpression) {
validateHavingClause(((AndFilterExpression) havingClause).getLeft());
validateHavingClause(((AndFilterExpression) havingClause).getRight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,106 @@ public void testGraphQLDynamicAggregationModelDateTime() throws Exception {
runQueryWithExpectedResult(graphQLRequest, expected);
}

@Test
public void testTimeDimMismatchArgs() throws Exception {

String graphQLRequest = document(
selection(
field(
"orderDetails",
arguments(
argument("sort", "\"customerRegion\""),
argument("filter", "\"orderTime[grain:DAY]=='2020-08',orderTotal>50\"")
),
selections(
field("orderTotal"),
field("customerRegion"),
field("orderTime", arguments(
argument("grain", TimeGrain.MONTH) // Does not match grain argument in filter
))
)
)
)
).toQuery();

String expected = "Exception while fetching data (/orderDetails) : Invalid operation: Time Dimension field orderTime must use the same grain argument in the projection and the having clause.";

runQueryWithExpectedError(graphQLRequest, expected);
}

@Test
public void testTimeDimMismatchArgsWithDefaultSelect() throws Exception {

String graphQLRequest = document(
selection(
field(
"orderDetails",
arguments(
argument("sort", "\"customerRegion\""),
argument("filter", "\"orderTime[grain:DAY]=='2020-08',orderTotal>50\"")
),
selections(
field("orderTotal"),
field("customerRegion"),
field("orderTime") //Default Grain for OrderTime is Month.
)
)
)
).toQuery();

String expected = "Exception while fetching data (/orderDetails) : Invalid operation: Time Dimension field orderTime must use the same grain argument in the projection and the having clause.";

runQueryWithExpectedError(graphQLRequest, expected);
}

@Test
public void testTimeDimMismatchArgsWithDefaultFilter() throws Exception {

String graphQLRequest = document(
selection(
field(
"orderDetails",
arguments(
argument("sort", "\"orderTime\""),
argument("filter", "\"orderTime=='2020-08-01',orderTotal>50\"") //No Grain Arg passed, so works based on Alias's argument in Selection.
),
selections(
field("orderTotal"),
field("customerRegion"),
field("orderTime", arguments(
argument("grain", TimeGrain.DAY)
))
)
)
)
).toQuery();

String expected = document(
selections(
field(
"orderDetails",
selections(
field("orderTotal", 103.72F),
field("customerRegion", "Virginia"),
field("orderTime", "2020-08-30")
),
selections(
field("orderTotal", 181.47F),
field("customerRegion", "Virginia"),
field("orderTime", "2020-09-08")
),
selections(
field("orderTotal", 78.87F),
field("customerRegion", "Virginia"),
field("orderTime", "2020-09-09")
)
)
)
).toResponse();

runQueryWithExpectedResult(graphQLRequest, expected);
}

@Test
public void testAdminRole() throws Exception {

Expand Down

0 comments on commit 8010ad9

Please sign in to comment.