Search in sources :

Example 1 with ConstantExpression

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

the class NullFilteringExpressionConverterTest method testEqNull.

@Test
void testEqNull() throws ConversionException {
    ColumnIdentifier columnIdentifier = ColumnIdentifier.newBuilder().setColumnName("column1").build();
    LiteralConstant constant = LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("null").build()).build();
    when(entityAttributeMapping.getDocStorePathByAttributeId(REQUEST_CONTEXT, "column1")).thenReturn(Optional.of("subDocPath1"));
    IdentifierConverter identifierConverter = mock(IdentifierConverter.class);
    when(identifierConverter.convert(any(), eq(REQUEST_CONTEXT))).thenReturn("attributes.subDocPath1");
    when(identifierConverterFactory.getIdentifierConverter("column1", "subDocPath1", ValueType.STRING, REQUEST_CONTEXT)).thenReturn(identifierConverter);
    ConstantExpression constantExpression = ConstantExpression.of("null");
    when(constantExpressionConverter.convert(constant, REQUEST_CONTEXT)).thenReturn(constantExpression);
    FilterTypeExpression filterTypeExpression = nullFilteringExpressionConverter.convert(columnIdentifier, Operator.EQ, constant, REQUEST_CONTEXT);
    IdentifierExpression identifierExpression = IdentifierExpression.of("attributes.subDocPath1");
    assertEquals(LogicalExpression.builder().operator(LogicalOperator.OR).operands(List.of(RelationalExpression.of(identifierExpression, RelationalOperator.NOT_EXISTS, constantExpression), RelationalExpression.of(identifierExpression, RelationalOperator.EQ, constantExpression))).build(), filterTypeExpression);
}
Also used : IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) FilterTypeExpression(org.hypertrace.core.documentstore.expression.type.FilterTypeExpression) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) Test(org.junit.jupiter.api.Test)

Example 2 with ConstantExpression

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

the class NullFilteringExpressionConverterTest method testNeqNull.

@Test
void testNeqNull() throws ConversionException {
    ColumnIdentifier columnIdentifier = ColumnIdentifier.newBuilder().setColumnName("column1").build();
    LiteralConstant constant = LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("null").build()).build();
    when(entityAttributeMapping.getDocStorePathByAttributeId(REQUEST_CONTEXT, "column1")).thenReturn(Optional.of("subDocPath1"));
    IdentifierConverter identifierConverter = mock(IdentifierConverter.class);
    when(identifierConverter.convert(any(), eq(REQUEST_CONTEXT))).thenReturn("attributes.subDocPath1");
    when(identifierConverterFactory.getIdentifierConverter("column1", "subDocPath1", ValueType.STRING, REQUEST_CONTEXT)).thenReturn(identifierConverter);
    ConstantExpression constantExpression = ConstantExpression.of("null");
    when(constantExpressionConverter.convert(constant, REQUEST_CONTEXT)).thenReturn(constantExpression);
    FilterTypeExpression filterTypeExpression = nullFilteringExpressionConverter.convert(columnIdentifier, Operator.NEQ, constant, REQUEST_CONTEXT);
    IdentifierExpression identifierExpression = IdentifierExpression.of("attributes.subDocPath1");
    assertEquals(RelationalExpression.of(identifierExpression, RelationalOperator.NEQ, constantExpression), filterTypeExpression);
}
Also used : IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) FilterTypeExpression(org.hypertrace.core.documentstore.expression.type.FilterTypeExpression) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) Test(org.junit.jupiter.api.Test)

Example 3 with ConstantExpression

use of org.hypertrace.core.documentstore.expression.impl.ConstantExpression 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 4 with ConstantExpression

use of org.hypertrace.core.documentstore.expression.impl.ConstantExpression 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 5 with ConstantExpression

use of org.hypertrace.core.documentstore.expression.impl.ConstantExpression 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)

Aggregations

ConstantExpression (org.hypertrace.core.documentstore.expression.impl.ConstantExpression)7 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)6 IdentifierConverter (org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter)6 RelationalOperator (org.hypertrace.core.documentstore.expression.operators.RelationalOperator)4 IdentifierConversionMetadata (org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)4 Value (org.hypertrace.entity.query.service.v1.Value)4 ValueType (org.hypertrace.entity.query.service.v1.ValueType)4 RelationalExpression (org.hypertrace.core.documentstore.expression.impl.RelationalExpression)3 LiteralConstant (org.hypertrace.entity.query.service.v1.LiteralConstant)3 ArrayList (java.util.ArrayList)2 FilterTypeExpression (org.hypertrace.core.documentstore.expression.type.FilterTypeExpression)2 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)2 ColumnIdentifier (org.hypertrace.entity.query.service.v1.ColumnIdentifier)2 Test (org.junit.jupiter.api.Test)2 TypeLiteral (com.google.inject.TypeLiteral)1 List (java.util.List)1 Map (java.util.Map)1 Query (org.hypertrace.core.documentstore.query.Query)1 Sort (org.hypertrace.core.documentstore.query.Sort)1 AccessorModule (org.hypertrace.entity.query.service.converter.accessor.AccessorModule)1