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));
}
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();
}
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();
}
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;
}
}
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");
}
Aggregations