Search in sources :

Example 1 with Function

use of org.hypertrace.entity.query.service.v1.Function in project entity-service by hypertrace.

the class AggregationAliasProviderTest method testGetSetAlias.

@Test
void testGetSetAlias() throws ConversionException {
    Function expression = aggregateExpressionBuilder.setFunctionName("DISTINCT").setAlias("total_population_in_Mars").build();
    assertEquals("total_population_in_Mars", aggregationAliasProvider.getAlias(expression));
}
Also used : Function(org.hypertrace.entity.query.service.v1.Function) Test(org.junit.jupiter.api.Test)

Example 2 with Function

use of org.hypertrace.entity.query.service.v1.Function in project entity-service by hypertrace.

the class QueryConverter method getExpressionsForFromClause.

private List<Expression> getExpressionsForFromClause(final EntityQueryRequest request) throws ConversionException {
    final List<Expression> list = new ArrayList<>(request.getGroupByList());
    for (Expression expression : request.getSelectionList()) {
        if (expression.hasFunction()) {
            org.hypertrace.entity.query.service.v1.Function function = expression.getFunction();
            final Expression aggregationColumn = aggregationColumnProvider.getAggregationColumn(function);
            list.add(aggregationColumn);
        }
    }
    return unmodifiableList(list);
}
Also used : FromTypeExpression(org.hypertrace.core.documentstore.expression.type.FromTypeExpression) Expression(org.hypertrace.entity.query.service.v1.Expression) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) ArrayList(java.util.ArrayList)

Example 3 with Function

use of org.hypertrace.entity.query.service.v1.Function in project entity-service by hypertrace.

the class AggregationAliasProvider method getAlias.

@Override
public String getAlias(final Function aggregateExpression) throws ConversionException {
    final Expression expression = aggregationColumnProvider.getAggregationColumn(aggregateExpression);
    final ColumnIdentifier containingIdentifier = expressionAccessor.access(expression, expression.getValueCase(), Set.of(COLUMNIDENTIFIER));
    final String alias = aggregateExpression.getAlias();
    if (StringUtils.isNotBlank(alias)) {
        return alias;
    }
    return aggregateExpression.getFunctionName() + ALIAS_SEPARATOR + identifierAliasProvider.getAlias(containingIdentifier);
}
Also used : Expression(org.hypertrace.entity.query.service.v1.Expression) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier)

Example 4 with Function

use of org.hypertrace.entity.query.service.v1.Function in project entity-service by hypertrace.

the class EntityQueryServiceImpl method buildMetadataForSelections.

private ResultSetMetadata buildMetadataForSelections(List<Expression> selections) throws ConversionException {
    final AliasProvider<ColumnIdentifier> identifierAliasProvider = getIdentifierAliasProvider();
    final AliasProvider<Function> aggregateExpressionAliasProvider = getAggregateExpressionAliasProvider();
    final List<ColumnMetadata> list = new ArrayList<>();
    for (final Expression selection : selections) {
        final String columnName;
        if (selection.hasFunction()) {
            columnName = aggregateExpressionAliasProvider.getAlias(selection.getFunction());
        } else if (selection.hasColumnIdentifier()) {
            columnName = identifierAliasProvider.getAlias(selection.getColumnIdentifier());
        } else {
            throw new ConversionException(String.format("Selection of non-identifier and non-aggregation is not supported. Found: %s", selection));
        }
        ColumnMetadata build = ColumnMetadata.newBuilder().setColumnName(columnName).build();
        list.add(build);
    }
    return ResultSetMetadata.newBuilder().addAllColumnMetadata(list).build();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) Function(org.hypertrace.entity.query.service.v1.Function) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) Expression(org.hypertrace.entity.query.service.v1.Expression) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier)

Example 5 with Function

use of org.hypertrace.entity.query.service.v1.Function in project entity-service by hypertrace.

the class AggregateExpressionConverter method convert.

@Override
public AggregateExpression convert(final Function aggregateExpression, final RequestContext requestContext) throws ConversionException {
    final AggregationOperator operator = OPERATOR_MAP.get().get(aggregateExpression.getFunctionName());
    if (operator == null) {
        throw new ConversionException(String.format("Operator not found for: %s", aggregateExpression));
    }
    final List<Expression> innerExpressions = aggregateExpression.getArgumentsList();
    if (innerExpressions.size() != 1) {
        throw new ConversionException("Aggregation function should have exactly one argument");
    }
    final Expression innerExpression = innerExpressions.get(0);
    final ColumnIdentifier containingIdentifier = expressionAccessor.access(innerExpression, innerExpression.getValueCase(), Set.of(COLUMNIDENTIFIER));
    final IdentifierExpression identifierExpression = identifierExpressionConverter.convert(containingIdentifier, requestContext);
    return AggregateExpression.of(operator, identifierExpression);
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) AggregationOperator(org.hypertrace.core.documentstore.expression.operators.AggregationOperator) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) AggregateExpression(org.hypertrace.core.documentstore.expression.impl.AggregateExpression) Expression(org.hypertrace.entity.query.service.v1.Expression) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)

Aggregations

Expression (org.hypertrace.entity.query.service.v1.Expression)7 Test (org.junit.jupiter.api.Test)7 Function (org.hypertrace.entity.query.service.v1.Function)6 ColumnIdentifier (org.hypertrace.entity.query.service.v1.ColumnIdentifier)4 OrderByExpression (org.hypertrace.entity.query.service.v1.OrderByExpression)3 ArrayList (java.util.ArrayList)2 AggregateExpression (org.hypertrace.core.documentstore.expression.impl.AggregateExpression)2 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)2 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ConstantExpression (org.hypertrace.core.documentstore.expression.impl.ConstantExpression)1 RelationalExpression (org.hypertrace.core.documentstore.expression.impl.RelationalExpression)1 AggregationOperator (org.hypertrace.core.documentstore.expression.operators.AggregationOperator)1 FromTypeExpression (org.hypertrace.core.documentstore.expression.type.FromTypeExpression)1 ColumnMetadata (org.hypertrace.entity.query.service.v1.ColumnMetadata)1