use of org.hypertrace.entity.query.service.converter.ConversionException in project entity-service by hypertrace.
the class EntityQueryServiceImpl method execute.
@Override
public void execute(EntityQueryRequest request, StreamObserver<ResultSetChunk> responseObserver) {
RequestContext requestContext = RequestContext.CURRENT.get();
Optional<String> tenantId = requestContext.getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
final Iterator<Document> documentIterator;
if (queryAggregationEnabled) {
final Converter<EntityQueryRequest, org.hypertrace.core.documentstore.query.Query> queryConverter = getQueryConverter();
final org.hypertrace.core.documentstore.query.Query query;
try {
query = queryConverter.convert(request, requestContext);
documentIterator = entitiesCollection.aggregate(query);
} catch (final Exception e) {
responseObserver.onError(new ServiceException(e));
return;
}
} else {
// TODO: Optimize this later. For now converting to EDS Query and then again to DocStore
// Query.
Query query = entityQueryConverter.convertToEDSQuery(requestContext, request);
/**
* {@link EntityQueryRequest} selections need to treated differently, since they don't
* transform one to one to {@link org.hypertrace.entity.data.service.v1.EntityDataRequest}
* selections
*/
List<String> docStoreSelections = entityQueryConverter.convertSelectionsToDocStoreSelections(requestContext, request.getSelectionList());
documentIterator = entitiesCollection.search(DocStoreConverter.transform(tenantId.get(), query, docStoreSelections));
}
final DocumentConverter rowConverter = injector.getInstance(DocumentConverter.class);
ResultSetMetadata resultSetMetadata;
try {
resultSetMetadata = this.buildMetadataForSelections(request.getSelectionList());
} catch (final ConversionException e) {
responseObserver.onError(new ServiceException(e));
return;
}
if (!documentIterator.hasNext()) {
ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
resultBuilder.setResultSetMetadata(resultSetMetadata);
resultBuilder.setIsLastChunk(true);
resultBuilder.setChunkId(0);
responseObserver.onNext(resultBuilder.build());
responseObserver.onCompleted();
return;
}
boolean isNewChunk = true;
int chunkId = 0, rowCount = 0;
ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
while (documentIterator.hasNext()) {
// Set metadata for new chunk
if (isNewChunk) {
resultBuilder.setResultSetMetadata(resultSetMetadata);
isNewChunk = false;
}
try {
final Row row;
if (queryAggregationEnabled) {
row = rowConverter.convertToRow(documentIterator.next(), resultSetMetadata);
resultBuilder.addRow(row);
rowCount++;
} else {
Optional<Entity> entity = DOCUMENT_PARSER.parseOrLog(documentIterator.next(), Entity.newBuilder());
if (entity.isPresent()) {
row = convertToEntityQueryResult(requestContext, entity.get(), request.getSelectionList());
resultBuilder.addRow(row);
rowCount++;
}
}
} catch (final Exception e) {
responseObserver.onError(new ServiceException(e));
return;
}
// current chunk is complete
if (rowCount >= CHUNK_SIZE || !documentIterator.hasNext()) {
resultBuilder.setChunkId(chunkId++);
resultBuilder.setIsLastChunk(!documentIterator.hasNext());
responseObserver.onNext(resultBuilder.build());
resultBuilder = ResultSetChunk.newBuilder();
isNewChunk = true;
rowCount = 0;
}
}
responseObserver.onCompleted();
}
use of org.hypertrace.entity.query.service.converter.ConversionException 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.entity.query.service.converter.ConversionException 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.entity.query.service.converter.ConversionException in project entity-service by hypertrace.
the class ArrayGetter method getValue.
@Override
public Value getValue(final JsonNode jsonNode) throws ConversionException {
final Iterator<JsonNode> elements = jsonNode.elements();
final List<Value> values = new ArrayList<>();
while (elements.hasNext()) {
final JsonNode node = elements.next();
final Value value;
if (nestedValueGetter.matches(node)) {
value = nestedValueGetter.getValue(node);
} else if (directValueGetter.matches(node)) {
value = directValueGetter.getValue(node);
} else {
throw new ConversionException(String.format("Unexpected node (%s) found", node));
}
values.add(value);
}
final ValueType primitiveType = values.stream().map(Value::getValueType).findFirst().orElse(STRING);
final ValueType type = valueHelper.getArrayValueType(primitiveType);
final Value.Builder valueBuilder = Value.newBuilder().setValueType(type);
switch(type) {
case STRING_ARRAY:
values.stream().map(Value::getString).forEach(valueBuilder::addStringArray);
break;
case INT_ARRAY:
values.stream().map(Value::getInt).forEach(valueBuilder::addIntArray);
break;
case LONG_ARRAY:
values.stream().map(Value::getLong).forEach(valueBuilder::addLongArray);
break;
case FLOAT_ARRAY:
values.stream().map(Value::getFloat).forEach(valueBuilder::addFloatArray);
break;
case DOUBLE_ARRAY:
values.stream().map(Value::getDouble).forEach(valueBuilder::addDoubleArray);
break;
case BYTES_ARRAY:
values.stream().map(Value::getBytes).forEach(valueBuilder::addBytesArray);
break;
case BOOLEAN_ARRAY:
values.stream().map(Value::getBoolean).forEach(valueBuilder::addBooleanArray);
break;
default:
throw new ConversionException(String.format("Unknown array type: %s", type));
}
return valueBuilder.build();
}
use of org.hypertrace.entity.query.service.converter.ConversionException in project entity-service by hypertrace.
the class BytesGetter method getValue.
@Override
public Value getValue(final JsonNode jsonNode) throws ConversionException {
final byte[] binaryValue;
try {
binaryValue = jsonNode.binaryValue();
} catch (final IOException e) {
throw new ConversionException("Unable to convert to bytes", e);
}
final ByteString byteString = ByteString.copyFrom(binaryValue);
return Value.newBuilder().setValueType(BYTES).setBytes(byteString).build();
}
Aggregations