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

feat: add support for logical operators #148

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -20,6 +20,8 @@
import org.hypertrace.core.graphql.common.schema.attributes.arguments.AttributeExpression;
import org.hypertrace.core.graphql.common.schema.results.ResultSet;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperatorArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.LimitArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.OffsetArgument;
Expand Down Expand Up @@ -89,6 +91,11 @@ public <O extends OrderArgument> Single<ResultSetRequest<O>> build(
.deserializeObjectList(arguments, orderArgumentClass)
.orElse(Collections.emptyList());

LogicalFilterOperator logicalFilterOperator =
this.argumentDeserializer
.deserializePrimitive(arguments, LogicalFilterOperatorArgument.class)
.orElse(LogicalFilterOperator.AND);

List<FilterArgument> requestedFilters =
this.argumentDeserializer
.deserializeObjectList(arguments, FilterArgument.class)
Expand All @@ -114,6 +121,7 @@ public <O extends OrderArgument> Single<ResultSetRequest<O>> build(
offset,
timeRange,
orders,
logicalFilterOperator,
filters,
this.getAttributeQueryableFields(selectionSet),
spaceId))
Expand All @@ -128,6 +136,7 @@ public <O extends OrderArgument> Single<ResultSetRequest<O>> build(
int offset,
TimeRangeArgument timeRange,
List<AttributeAssociation<O>> orderArguments,
LogicalFilterOperator logicalFilterOperator,
Collection<AttributeAssociation<FilterArgument>> filterArguments,
Stream<SelectedField> attributeQueryableFields,
Optional<String> spaceId) {
Expand All @@ -145,6 +154,7 @@ public <O extends OrderArgument> Single<ResultSetRequest<O>> build(
limit,
offset,
orderArguments,
logicalFilterOperator,
filterArguments,
spaceId));
}
Expand Down Expand Up @@ -184,6 +194,7 @@ public Single<ResultSetRequest<OrderArgument>> build(
limit,
0,
List.of(),
LogicalFilterOperator.AND,
filters,
Optional.empty()));
}
Expand All @@ -206,6 +217,7 @@ public <O extends OrderArgument> Single<ResultSetRequest<O>> rebuildWithAddition
originalRequest.limit(),
originalRequest.offset(),
originalRequest.orderArguments(),
originalRequest.logicalFilterOperator(),
mergedFilters,
originalRequest.spaceId()));
}
Expand Down Expand Up @@ -251,6 +263,7 @@ private static class DefaultResultSetRequest<O extends OrderArgument>
int limit;
int offset;
List<AttributeAssociation<O>> orderArguments;
LogicalFilterOperator logicalFilterOperator;
Collection<AttributeAssociation<FilterArgument>> filterArguments;
Optional<String> spaceId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Optional;
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;

public interface ResultSetRequest<O extends OrderArgument> extends ContextualRequest {
Expand All @@ -24,5 +25,7 @@ public interface ResultSetRequest<O extends OrderArgument> extends ContextualReq

Collection<AttributeAssociation<FilterArgument>> filterArguments();

LogicalFilterOperator logicalFilterOperator();

Optional<String> spaceId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.attributes.arguments.AttributeExpression;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.context.GraphQlRequestContext;

Expand All @@ -35,6 +36,7 @@ <O extends OrderArgument> Single<ResultSetRequest<O>> build(
int offset,
TimeRangeArgument timeRange,
List<AttributeAssociation<O>> orderArguments,
LogicalFilterOperator logicalFilterOperator,
Collection<AttributeAssociation<FilterArgument>> filterArguments,
Stream<SelectedField> attributeQueryableFields,
Optional<String> spaceId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hypertrace.core.graphql.common.schema.results.arguments.filter;

import graphql.annotations.annotationTypes.GraphQLName;

@GraphQLName(LogicalFilterOperator.TYPE_NAME)
public enum LogicalFilterOperator {
AND,
OR;

static final String TYPE_NAME = "LogicalFilterOperator";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hypertrace.core.graphql.common.schema.results.arguments.filter;

import org.hypertrace.core.graphql.deserialization.PrimitiveArgument;

public interface LogicalFilterOperatorArgument extends PrimitiveArgument<LogicalFilterOperator> {

String ARGUMENT_NAME = "logicalOperator";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hypertrace.core.graphql.attributes.AttributeModel;
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperatorArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.LimitArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.OffsetArgument;
Expand Down Expand Up @@ -94,6 +95,9 @@ void canBuildRequest() {
.thenReturn(Optional.of(List.of(this.mockFilterArgument)));
when(this.mockArgumentDeserializer.deserializePrimitive(any(), eq(SpaceArgument.class)))
.thenReturn(Optional.of(this.mockSpace));
when(this.mockArgumentDeserializer.deserializePrimitive(
any(), eq(LogicalFilterOperatorArgument.class)))
.thenReturn(Optional.empty());

ResultSetRequest<OrderArgument> request =
this.requestBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import javax.inject.Inject;
import org.hypertrace.core.graphql.common.request.AttributeAssociation;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.utils.BiConverter;
import org.hypertrace.core.graphql.common.utils.Converter;
import org.hypertrace.gateway.service.v1.common.Filter;
import org.hypertrace.gateway.service.v1.common.Operator;

class FilterConverter
implements Converter<Collection<AttributeAssociation<FilterArgument>>, Filter> {
implements Converter<Collection<AttributeAssociation<FilterArgument>>, Filter>,
BiConverter<
Collection<AttributeAssociation<FilterArgument>>, LogicalFilterOperator, Filter> {

private final AttributeExpressionConverter attributeExpressionConverter;
private final OperatorConverter operatorConverter;
Expand All @@ -32,6 +36,12 @@ class FilterConverter

@Override
public Single<Filter> convert(Collection<AttributeAssociation<FilterArgument>> filters) {
return this.convert(filters, LogicalFilterOperator.AND);
}

@Override
public Single<Filter> convert(
Collection<AttributeAssociation<FilterArgument>> filters, LogicalFilterOperator operator) {
if (filters.isEmpty()) {
return Single.just(Filter.getDefaultInstance());
}
Expand All @@ -42,11 +52,21 @@ public Single<Filter> convert(Collection<AttributeAssociation<FilterArgument>> f
.map(
filterList ->
Filter.newBuilder()
.setOperator(Operator.AND)
.setOperator(this.convertLogicalOperator(operator))
.addAllChildFilter(filterList)
.build());
}

private Operator convertLogicalOperator(LogicalFilterOperator logicalFilterOperator) {
switch (logicalFilterOperator) {
case OR:
return Operator.OR;
case AND:
default:
return Operator.AND;
}
}

private Single<Filter> buildFilter(AttributeAssociation<FilterArgument> filter) {
return zip(
this.attributeExpressionConverter.convert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hypertrace.core.graphql.common.schema.attributes.arguments.AttributeExpression;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterOperatorType;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderDirection;
import org.hypertrace.core.graphql.common.utils.BiConverter;
Expand Down Expand Up @@ -58,6 +59,13 @@ protected void configure() {
new TypeLiteral<
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter>>() {}))
.to(FilterConverter.class);
bind(Key.get(
new TypeLiteral<
BiConverter<
Collection<AttributeAssociation<FilterArgument>>,
LogicalFilterOperator,
Filter>>() {}))
.to(FilterConverter.class);

bind(Key.get(new TypeLiteral<Converter<AttributeModel, ColumnIdentifier>>() {}))
.to(ColumnIdentifierConverter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,35 @@
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import lombok.RequiredArgsConstructor;
import org.hypertrace.core.graphql.common.request.AttributeAssociation;
import org.hypertrace.core.graphql.common.request.AttributeRequest;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.utils.BiConverter;
import org.hypertrace.core.graphql.common.utils.Converter;
import org.hypertrace.core.graphql.log.event.request.LogEventRequest;
import org.hypertrace.gateway.service.v1.common.Expression;
import org.hypertrace.gateway.service.v1.common.Filter;
import org.hypertrace.gateway.service.v1.common.OrderByExpression;
import org.hypertrace.gateway.service.v1.log.events.LogEventsRequest;

@RequiredArgsConstructor(onConstructor_ = @Inject)
class GatewayServiceLogEventsRequestBuilder {

private final Converter<Collection<AttributeAssociation<FilterArgument>>, Filter> filterConverter;
private final BiConverter<
Collection<AttributeAssociation<FilterArgument>>, LogicalFilterOperator, Filter>
filterConverter;
private final Converter<List<AttributeAssociation<OrderArgument>>, List<OrderByExpression>>
orderConverter;
private final Converter<Collection<AttributeRequest>, Set<Expression>> attributeConverter;

@Inject
GatewayServiceLogEventsRequestBuilder(
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter> filterConverter,
Converter<List<AttributeAssociation<OrderArgument>>, List<OrderByExpression>> orderConverter,
Converter<Collection<AttributeRequest>, Set<Expression>> attributeConverter) {
this.filterConverter = filterConverter;
this.orderConverter = orderConverter;
this.attributeConverter = attributeConverter;
}

Single<LogEventsRequest> buildRequest(LogEventRequest gqlRequest) {
return zip(
this.attributeConverter.convert(gqlRequest.attributes()),
this.orderConverter.convert(gqlRequest.orderArguments()),
this.filterConverter.convert(gqlRequest.filterArguments()),
this.filterConverter.convert(
gqlRequest.filterArguments(), gqlRequest.logicalFilterOperator()),
(selections, orderBys, filters) ->
LogEventsRequest.newBuilder()
.setStartTimeMillis(gqlRequest.timeRange().startTime().toEpochMilli())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hypertrace.core.graphql.common.request.AttributeRequest;
import org.hypertrace.core.graphql.common.schema.attributes.arguments.AttributeExpression;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.utils.BiConverter;
import org.hypertrace.core.graphql.common.utils.Converter;
Expand Down Expand Up @@ -46,7 +47,10 @@ protected void configure() {
requireBinding(
Key.get(
new TypeLiteral<
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter>>() {}));
BiConverter<
Collection<AttributeAssociation<FilterArgument>>,
LogicalFilterOperator,
Filter>>() {}));

requireBinding(
Key.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.results.ResultSet;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperatorArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.LimitArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.OffsetArgument;
Expand Down Expand Up @@ -94,6 +96,11 @@ Single<LogEventRequest> build(
.deserializeObjectList(arguments, orderArgumentClass)
.orElse(Collections.emptyList());

LogicalFilterOperator filterOperator =
this.argumentDeserializer
.deserializePrimitive(arguments, LogicalFilterOperatorArgument.class)
.orElse(LogicalFilterOperator.AND);

List<FilterArgument> requestedFilters =
this.argumentDeserializer
.deserializeObjectList(arguments, FilterArgument.class)
Expand All @@ -115,7 +122,14 @@ context, requestScope, getAttributeQueryableFields(selectionSet))
(attributeRequests, orders, filters) ->
Single.just(
new DefaultLogEventRequest(
context, attributeRequests, timeRange, limit, offset, orders, filters)))
context,
attributeRequests,
timeRange,
limit,
offset,
orders,
filterOperator,
filters)))
.flatMap(single -> single);
}

Expand All @@ -135,6 +149,7 @@ private static class DefaultLogEventRequest implements LogEventRequest {
int limit;
int offset;
List<AttributeAssociation<OrderArgument>> orderArguments;
LogicalFilterOperator logicalFilterOperator;
Collection<AttributeAssociation<FilterArgument>> filterArguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hypertrace.core.graphql.common.request.ContextualRequest;
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;

public interface LogEventRequest extends ContextualRequest {
Expand All @@ -21,5 +22,7 @@ public interface LogEventRequest extends ContextualRequest {

List<AttributeAssociation<OrderArgument>> orderArguments();

LogicalFilterOperator logicalFilterOperator();

Collection<AttributeAssociation<FilterArgument>> filterArguments();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperatorArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.LimitArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.page.OffsetArgument;
Expand All @@ -21,6 +23,8 @@ public interface LogEventSchema {
@GraphQLDataFetcher(LogEventFetcher.class)
LogEventResultSet logEvents(
@GraphQLName(TimeRangeArgument.ARGUMENT_NAME) @GraphQLNonNull TimeRangeArgument between,
@GraphQLName(LogicalFilterOperatorArgument.ARGUMENT_NAME)
LogicalFilterOperator logicalFilterOperator,
@GraphQLName(FilterArgument.ARGUMENT_NAME) List<FilterArgument> filterBy,
@GraphQLName(OrderArgument.ARGUMENT_NAME) List<OrderArgument> orderBy,
@GraphQLName(LimitArgument.ARGUMENT_NAME) int limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hypertrace.core.graphql.common.schema.arguments.TimeRangeArgument;
import org.hypertrace.core.graphql.common.schema.attributes.arguments.AttributeExpression;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.LogicalFilterOperator;
import org.hypertrace.core.graphql.common.schema.results.arguments.order.OrderArgument;
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
import org.hypertrace.core.graphql.log.event.request.LogEventRequest;
Expand All @@ -30,6 +31,7 @@ static class DefaultLogEventRequest implements LogEventRequest {
int limit;
int offset;
List<AttributeAssociation<OrderArgument>> orderArguments;
LogicalFilterOperator logicalFilterOperator;
Collection<AttributeAssociation<FilterArgument>> filterArguments;
}

Expand Down
Loading