Search in sources :

Example 1 with RelationalExpression

use of org.hypertrace.core.documentstore.expression.impl.RelationalExpression in project entity-service by hypertrace.

the class RelationalExpressionConverterTest method testConvert.

@Test
void testConvert() throws ConversionException {
    RelationalExpression expected = RelationalExpression.of(identifierExpression, RelationalOperator.NOT_IN, constantExpression);
    when(filteringExpressionConverter.convert(columnIdentifier, Operator.NOT_IN, literalConstant, requestContext)).thenReturn(expected);
    assertEquals(expected, relationalExpressionConverter.convert(filter, requestContext));
}
Also used : RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) Test(org.junit.jupiter.api.Test)

Example 2 with RelationalExpression

use of org.hypertrace.core.documentstore.expression.impl.RelationalExpression in project entity-service by hypertrace.

the class ArrayFilteringExpressionConverter method convert.

@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
    if (ARRAY_OPERATORS.contains(operator)) {
        return primitiveFilteringExpressionConverter.convert(columnIdentifier, operator, constant, requestContext);
    }
    final String id = columnIdentifier.getColumnName();
    final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
    final Value value = constant.getValue();
    final ValueType valueType = value.getValueType();
    final List<?> list = valueOneOfAccessor.access(value, valueType);
    if (list.isEmpty()) {
        throw new ConversionException("Conversion of empty-list is unsupported");
    }
    final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
    final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
    final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
    final List<RelationalExpression> expressions = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        final IdentifierExpression lhs = IdentifierExpression.of(String.format(suffixedSubDocPath, i));
        final RelationalOperator relationalOperator = convertOperator(operator);
        final ConstantExpression rhs = valueHelper.convertToConstantExpression(value, i);
        final RelationalExpression expression = RelationalExpression.of(lhs, relationalOperator, rhs);
        expressions.add(expression);
    }
    if (expressions.size() == 1) {
        return expressions.get(0);
    }
    return LogicalExpression.builder().operator(LogicalOperator.AND).operands(expressions).build();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) ArrayList(java.util.ArrayList) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) Value(org.hypertrace.entity.query.service.v1.Value) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)

Example 3 with RelationalExpression

use of org.hypertrace.core.documentstore.expression.impl.RelationalExpression in project entity-service by hypertrace.

the class MapFilteringExpressionConverter method convert.

@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
    if (!SUPPORTED_OPERATORS.contains(operator)) {
        throw new ConversionException(String.format("Operator %s is not supported", operator));
    }
    final String id = columnIdentifier.getColumnName();
    final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
    final Value value = constant.getValue();
    final ValueType valueType = value.getValueType();
    final Map<?, ?> map = valueOneOfAccessor.access(value, valueType);
    if (map.isEmpty()) {
        throw new ConversionException("Conversion of empty-map is unsupported");
    }
    final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
    final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
    final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
    final List<RelationalExpression> expressions = new ArrayList<>();
    for (final Map.Entry<?, ?> entry : map.entrySet()) {
        final IdentifierExpression lhs = IdentifierExpression.of(String.format(suffixedSubDocPath, entry.getKey()));
        final RelationalOperator relationalOperator = convertOperator(operator);
        final ConstantExpression rhs = valueHelper.convertToConstantExpression(value, entry.getKey());
        final RelationalExpression expression = RelationalExpression.of(lhs, relationalOperator, rhs);
        expressions.add(expression);
    }
    if (expressions.size() == 1) {
        return expressions.get(0);
    }
    return LogicalExpression.builder().operator(LogicalOperator.AND).operands(expressions).build();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) ArrayList(java.util.ArrayList) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) Value(org.hypertrace.entity.query.service.v1.Value) Map(java.util.Map) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)

Example 4 with RelationalExpression

use of org.hypertrace.core.documentstore.expression.impl.RelationalExpression in project entity-service by hypertrace.

the class NullFilteringExpressionConverter method convert.

@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
    final String id = columnIdentifier.getColumnName();
    final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
    final Value value = constant.getValue();
    final ValueType valueType = value.getValueType();
    final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
    final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
    final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
    final IdentifierExpression identifierExpression = IdentifierExpression.of(suffixedSubDocPath);
    final RelationalOperator relationalOperator = convertOperator(operator);
    final ConstantExpression constantExpression = constantExpressionConverter.convert(constant, requestContext);
    RelationalExpression relationalExpression = RelationalExpression.of(identifierExpression, relationalOperator, constantExpression);
    switch(operator) {
        // 'field' EQ 'null' -> 'field' EQ 'null' || 'field' NOT_EXISTS
        case EQ:
            RelationalExpression nonExistenceRelationalExpression = RelationalExpression.of(identifierExpression, convertOperator(Operator.NOT_EXISTS), constantExpression);
            return LogicalExpression.builder().operator(LogicalOperator.OR).operands(List.of(nonExistenceRelationalExpression, relationalExpression)).build();
        default:
            return relationalExpression;
    }
}
Also used : RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) Value(org.hypertrace.entity.query.service.v1.Value) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)

Example 5 with RelationalExpression

use of org.hypertrace.core.documentstore.expression.impl.RelationalExpression in project document-store by hypertrace.

the class MongoQueryExecutorIntegrationTest method testFilterAndUnnest.

@Test
public void testFilterAndUnnest() throws IOException {
    RelationalExpression relationalExpression = RelationalExpression.of(IdentifierExpression.of("sales.city"), EQ, ConstantExpression.of("delhi"));
    org.hypertrace.core.documentstore.query.Query query = org.hypertrace.core.documentstore.query.Query.builder().addSelection(IdentifierExpression.of("item")).addSelection(IdentifierExpression.of("sales.city")).addSelection(IdentifierExpression.of("sales.medium")).addFromClause(UnnestExpression.builder().identifierExpression(IdentifierExpression.of("sales")).preserveNullAndEmptyArrays(true).filterTypeExpression(relationalExpression).build()).addFromClause(UnnestExpression.of(IdentifierExpression.of("sales.medium"), true)).addSort(IdentifierExpression.of("item"), DESC).addSort(IdentifierExpression.of("sales.city"), DESC).addSort(IdentifierExpression.of("sales.medium.volume"), DESC).addSort(IdentifierExpression.of("sales.medium.type"), DESC).build();
    Iterator<Document> iterator = collection.aggregate(query);
    assertDocsEqual(iterator, "mongo/unwind_filter_response.json");
}
Also used : RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) Query(org.hypertrace.core.documentstore.query.Query) Document(org.hypertrace.core.documentstore.Document) Test(org.junit.jupiter.api.Test)

Aggregations

RelationalExpression (org.hypertrace.core.documentstore.expression.impl.RelationalExpression)7 ConstantExpression (org.hypertrace.core.documentstore.expression.impl.ConstantExpression)3 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)3 RelationalOperator (org.hypertrace.core.documentstore.expression.operators.RelationalOperator)3 IdentifierConversionMetadata (org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)3 IdentifierConverter (org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter)3 Value (org.hypertrace.entity.query.service.v1.Value)3 ValueType (org.hypertrace.entity.query.service.v1.ValueType)3 ArrayList (java.util.ArrayList)2 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)2 Test (org.junit.jupiter.api.Test)2 Map (java.util.Map)1 Document (org.hypertrace.core.documentstore.Document)1 Query (org.hypertrace.core.documentstore.query.Query)1