Search in sources :

Example 1 with ConversionException

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();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ServiceException(com.google.protobuf.ServiceException) Row(org.hypertrace.entity.query.service.v1.Row) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) DocumentConverter(org.hypertrace.entity.query.service.converter.response.DocumentConverter) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 2 with ConversionException

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

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

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();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ValueType(org.hypertrace.entity.query.service.v1.ValueType) Value(org.hypertrace.entity.query.service.v1.Value) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with ConversionException

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();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Aggregations

ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ArrayList (java.util.ArrayList)5 Value (org.hypertrace.entity.query.service.v1.Value)5 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)4 ValueType (org.hypertrace.entity.query.service.v1.ValueType)4 ConstantExpression (org.hypertrace.core.documentstore.expression.impl.ConstantExpression)3 RelationalExpression (org.hypertrace.core.documentstore.expression.impl.RelationalExpression)3 Entry (java.util.Map.Entry)2 RelationalOperator (org.hypertrace.core.documentstore.expression.operators.RelationalOperator)2 IdentifierConversionMetadata (org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)2 IdentifierConverter (org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter)2 ColumnIdentifier (org.hypertrace.entity.query.service.v1.ColumnIdentifier)2 Expression (org.hypertrace.entity.query.service.v1.Expression)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ByteString (com.google.protobuf.ByteString)1 ServiceException (com.google.protobuf.ServiceException)1 IOException (java.io.IOException)1 Map (java.util.Map)1