Search in sources :

Example 1 with ValueType

use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.

the class TableType method getArgumentDefinitions.

private static ArgumentDefinition[] getArgumentDefinitions(List<Argument> arguments) {
    int numArguments = arguments == null ? 0 : arguments.size();
    ArgumentDefinition[] definitions = new ArgumentDefinition[numArguments];
    for (int idx = 0; idx < numArguments; idx++) {
        Argument argument = arguments.get(idx);
        definitions[idx] = new ArgumentDefinition() {

            @Override
            public String name() {
                return argument.getName();
            }

            @Override
            public String description() {
                return argument.getDescription();
            }

            @Override
            public ValueType type() {
                return ValueType.valueOf(argument.getType().toUpperCase(Locale.ROOT));
            }

            @Override
            public TableSource tableSource() {
                return buildTableSource(argument.getTableSource());
            }

            @Override
            public String[] values() {
                return argument.getValues().toArray(new String[0]);
            }

            @Override
            public String defaultValue() {
                Object value = argument.getDefaultValue();
                return value == null ? null : value.toString();
            }

            @Override
            public Class<? extends Annotation> annotationType() {
                return ArgumentDefinition.class;
            }
        };
    }
    return definitions;
}
Also used : TableSource(com.yahoo.elide.datastores.aggregation.annotation.TableSource) Argument(com.yahoo.elide.modelconfig.model.Argument) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.annotation.ArgumentDefinition) Annotation(java.lang.annotation.Annotation)

Example 2 with ValueType

use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.

the class SQLMetricProjection method nest.

@Override
public Pair<ColumnProjection, Set<ColumnProjection>> nest(Queryable source, MetaDataStore metaDataStore, boolean joinInOuter) {
    SQLDialect dialect = source.getConnectionDetails().getDialect();
    String sql = toSQL(source, metaDataStore);
    SqlParser sqlParser = SqlParser.create(sql, CalciteUtils.constructParserConfig(dialect));
    SqlNode node;
    try {
        node = sqlParser.parseExpression();
    } catch (SqlParseException e) {
        throw new IllegalStateException(e);
    }
    CalciteInnerAggregationExtractor innerExtractor = new CalciteInnerAggregationExtractor(dialect);
    List<List<String>> innerAggExpressions = node.accept(innerExtractor);
    List<List<String>> innerAggLabels = innerAggExpressions.stream().map(list -> list.stream().map((expression) -> getAggregationLabel(dialect.getCalciteDialect(), expression)).collect(Collectors.toList())).collect(Collectors.toList());
    Set<ColumnProjection> innerAggProjections = new LinkedHashSet<>();
    Iterator<String> labelIt = innerAggLabels.stream().flatMap(List::stream).iterator();
    Iterator<String> expressionIt = innerAggExpressions.stream().flatMap(List::stream).iterator();
    while (labelIt.hasNext() && expressionIt.hasNext()) {
        String innerAggExpression = expressionIt.next();
        String innerAggLabel = labelIt.next();
        innerAggProjections.add(SQLMetricProjection.builder().projected(true).name(innerAggLabel).alias(innerAggLabel).expression(innerAggExpression).columnType(columnType).valueType(valueType).arguments(arguments).build());
    }
    CalciteOuterAggregationExtractor outerExtractor = new CalciteOuterAggregationExtractor(dialect, innerAggLabels);
    SqlNode transformedParseTree = node.accept(outerExtractor);
    String outerAggExpression = transformedParseTree.toSqlString(dialect.getCalciteDialect()).getSql();
    // replace INNER_AGG_... with {{$INNER_AGG...}}
    outerAggExpression = outerAggExpression.replaceAll(dialect.getBeginQuote() + "?(" + getAggregationLabelPrefix(dialect.getCalciteDialect()) + "\\w+)" + dialect.getEndQuote() + "?", "{{\\$" + "$1" + "}}");
    String columnId = source.isRoot() ? getName() : getAlias();
    boolean inProjection = source.getColumnProjection(columnId, arguments, true) != null;
    ColumnProjection outerProjection = SQLMetricProjection.builder().projected(inProjection).expression(outerAggExpression).name(name).alias(alias).valueType(valueType).columnType(columnType).arguments(arguments).build();
    return Pair.of(outerProjection, innerAggProjections);
}
Also used : CalciteOuterAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteOuterAggregationExtractor) Argument(com.yahoo.elide.core.request.Argument) CalciteInnerAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteInnerAggregationExtractor) SqlNode(org.apache.calcite.sql.SqlNode) Pair(org.apache.commons.lang3.tuple.Pair) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Map(java.util.Map) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) LinkedHashSet(java.util.LinkedHashSet) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) Iterator(java.util.Iterator) ColumnType(com.yahoo.elide.datastores.aggregation.metadata.enums.ColumnType) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Casing(org.apache.calcite.avatica.util.Casing) Set(java.util.Set) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan) Collectors(java.util.stream.Collectors) SqlDialect(org.apache.calcite.sql.SqlDialect) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) Builder(lombok.Builder) CalciteUtils(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteUtils) SqlParser(org.apache.calcite.sql.parser.SqlParser) Data(lombok.Data) Pattern(java.util.regex.Pattern) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) LinkedHashSet(java.util.LinkedHashSet) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) CalciteInnerAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteInnerAggregationExtractor) SqlParser(org.apache.calcite.sql.parser.SqlParser) CalciteOuterAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteOuterAggregationExtractor) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) List(java.util.List) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with ValueType

use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.

the class EntityHydrator method coerceObjectToEntity.

/**
 * Coerces results from a {@link Query} into an Object.
 *
 * @param result a fieldName-value map
 * @param counter Monotonically increasing number to generate IDs.
 * @return A hydrated entity object.
 */
protected Object coerceObjectToEntity(Map<String, Object> result, MutableInt counter) {
    Table table = getBaseTable(query);
    Type<?> entityClass = entityDictionary.getEntityClass(table.getName(), table.getVersion());
    // Construct the object.
    Object entityInstance;
    try {
        entityInstance = entityClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    result.forEach((fieldName, value) -> {
        ColumnProjection columnProjection = query.getColumnProjection(fieldName);
        Column column = table.getColumn(Column.class, columnProjection.getName());
        Type<?> fieldType = getType(entityClass, columnProjection);
        Attribute attribute = projectionToAttribute(columnProjection, fieldType);
        ValueType valueType = column.getValueType();
        if (entityInstance instanceof ParameterizedModel) {
            // This is an ENUM_TEXT or ENUM_ORDINAL type.
            if (// Java enums can be coerced directly via CoerceUtil - so skip them.
            !fieldType.isEnum() && valueType == ValueType.TEXT && column.getValues() != null && !column.getValues().isEmpty()) {
                value = convertToEnumValue(value, column.getValues());
            }
            ((ParameterizedModel) entityInstance).addAttributeValue(attribute, CoerceUtil.coerce(value, fieldType));
        } else {
            getEntityDictionary().setValue(entityInstance, fieldName, value);
        }
    });
    // Set the ID (it must be coerced from an integer)
    getEntityDictionary().setValue(entityInstance, getEntityDictionary().getIdFieldName(entityClass), counter.getAndIncrement());
    return entityInstance;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) Attribute(com.yahoo.elide.core.request.Attribute) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ParameterizedModel(com.yahoo.elide.core.type.ParameterizedModel) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 4 with ValueType

use of com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType in project elide by yahoo.

the class Table method constructColumns.

/**
 * Construct all columns of this table.
 *
 * @param cls table class
 * @param dictionary dictionary contains the table class
 * @return all resolved column metadata
 */
private Set<Column> constructColumns(Type<?> cls, EntityDictionary dictionary) {
    EntityBinding binding = dictionary.getEntityBinding(cls);
    Set<Column> columns = new HashSet<>();
    // TODO - refactor entity binding to have classes for attributes & relationships.
    binding.fieldsToValues.forEach((name, field) -> {
        if (EntityBinding.isIdField(field)) {
            return;
        }
        ValueType valueType = getValueType(cls, name, dictionary);
        if (valueType == ValueType.UNKNOWN) {
            return;
        }
        if (isMetricField(dictionary, cls, name)) {
            columns.add(constructMetric(name, dictionary));
        } else if (dictionary.attributeOrRelationAnnotationExists(cls, name, Temporal.class)) {
            columns.add(constructTimeDimension(name, dictionary));
        } else {
            columns.add(constructDimension(name, dictionary));
        }
    });
    // add id field if exists
    if (dictionary.getIdFieldName(cls) != null) {
        String idFieldName = dictionary.getIdFieldName(cls);
        if (AggregationDataStore.isAggregationStoreModel(cls)) {
            columns.add(constructMetric(idFieldName, dictionary));
        } else {
            columns.add(constructDimension(idFieldName, dictionary));
        }
    }
    return columns;
}
Also used : Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) Column.getValueType(com.yahoo.elide.datastores.aggregation.metadata.models.Column.getValueType) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) ToString(lombok.ToString) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)4 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)2 LinkedHashSet (java.util.LinkedHashSet)2 EntityBinding (com.yahoo.elide.core.dictionary.EntityBinding)1 Argument (com.yahoo.elide.core.request.Argument)1 Attribute (com.yahoo.elide.core.request.Attribute)1 ParameterizedModel (com.yahoo.elide.core.type.ParameterizedModel)1 ArgumentDefinition (com.yahoo.elide.datastores.aggregation.annotation.ArgumentDefinition)1 TableSource (com.yahoo.elide.datastores.aggregation.annotation.TableSource)1 Temporal (com.yahoo.elide.datastores.aggregation.annotation.Temporal)1 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)1 ColumnType (com.yahoo.elide.datastores.aggregation.metadata.enums.ColumnType)1 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)1 Column.getValueType (com.yahoo.elide.datastores.aggregation.metadata.models.Column.getValueType)1 Metric (com.yahoo.elide.datastores.aggregation.metadata.models.Metric)1 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)1 MetricProjection (com.yahoo.elide.datastores.aggregation.query.MetricProjection)1 Query (com.yahoo.elide.datastores.aggregation.query.Query)1 QueryPlan (com.yahoo.elide.datastores.aggregation.query.QueryPlan)1 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)1