Search in sources :

Example 61 with SQLTable

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable in project elide by yahoo.

the class SQLQueryEngine method getTableVersion.

@Override
public String getTableVersion(Table table, Transaction transaction) {
    String tableVersion = null;
    SQLTable sqlTable = (SQLTable) table;
    Type<?> tableClass = metadataDictionary.getEntityClass(table.getName(), table.getVersion());
    VersionQuery versionAnnotation = tableClass.getAnnotation(VersionQuery.class);
    if (versionAnnotation != null) {
        String versionQueryString = versionAnnotation.sql();
        SqlTransaction sqlTransaction = (SqlTransaction) transaction;
        ConnectionDetails details = sqlTable.getConnectionDetails();
        DataSource dataSource = details.getDataSource();
        NamedParamPreparedStatement stmt = sqlTransaction.initializeStatement(versionQueryString, dataSource);
        tableVersion = CoerceUtil.coerce(runQuery(stmt, versionQueryString, SINGLE_RESULT_MAPPER), String.class);
    }
    return tableVersion;
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) VersionQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.VersionQuery) DataSource(javax.sql.DataSource)

Example 62 with SQLTable

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable in project elide by yahoo.

the class SQLQueryEngine method verifyMetaData.

@Override
protected void verifyMetaData(MetaDataStore metaDataStore) {
    metaDataStore.getTables().forEach(table -> {
        SQLTable sqlTable = (SQLTable) table;
        checkForCycles(sqlTable);
        TableArgumentValidator tableArgValidator = new TableArgumentValidator(metaDataStore, sqlTable);
        tableArgValidator.validate();
        sqlTable.getAllColumns().forEach(column -> {
            ColumnArgumentValidator colArgValidator = new ColumnArgumentValidator(metaDataStore, sqlTable, column);
            colArgValidator.validate();
        });
    });
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) TableArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.TableArgumentValidator) ColumnArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.ColumnArgumentValidator)

Example 63 with SQLTable

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable in project elide by yahoo.

the class SQLQueryEngine method expandMetricQueryPlans.

/**
 * Transforms a client query into a potentially nested/complex query by expanding each metric into
 * its respective query plan - and then merging the plans together into a consolidated query.
 * @param query The client query.
 * @return A query that reflects each metric's individual query plan.
 */
private Query expandMetricQueryPlans(Query query) {
    // Expand each metric into its own query plan.
    List<QueryPlan> toMerge = query.getMetricProjections().stream().map(projection -> projection.resolve(query)).collect(Collectors.toList());
    // Merge all the queries together.
    List<QueryPlan> mergedPlans = merger.merge(toMerge);
    // TODO - Support joins across plans rather than rejecting plans that don't merge.
    if (mergedPlans.size() != 1) {
        throw new UnsupportedOperationException("Incompatible metrics in client query.  Cannot merge " + "into a single query");
    }
    QueryPlan mergedPlan = mergedPlans.get(0);
    QueryPlanTranslator queryPlanTranslator = new QueryPlanTranslator(query, metaDataStore, merger);
    Query merged = (mergedPlan == null) ? QueryPlanTranslator.addHiddenProjections(metaDataStore, query).build() : queryPlanTranslator.translate(mergedPlan);
    for (Optimizer optimizer : optimizers) {
        SQLTable table = (SQLTable) query.getSource();
        // override table hints.
        if (table.getHints().contains(optimizer.negateHint())) {
            continue;
        }
        if (!table.getHints().contains(optimizer.hint())) {
            continue;
        }
        if (optimizer.canOptimize(merged)) {
            merged = optimizer.optimize(merged);
        }
    }
    return merged;
}
Also used : PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Arrays(java.util.Arrays) Connection(java.sql.Connection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Argument(com.yahoo.elide.core.request.Argument) EnumType(javax.persistence.EnumType) TimedFunction(com.yahoo.elide.core.utils.TimedFunction) Time(com.yahoo.elide.datastores.aggregation.timegrains.Time) NamespacePackage(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) ResultSet(java.sql.ResultSet) Map(java.util.Map) Enumerated(javax.persistence.Enumerated) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) QueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger) Collection(java.util.Collection) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Set(java.util.Set) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnContext.applyQuotes(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext.applyQuotes) QueryValidator(com.yahoo.elide.datastores.aggregation.QueryValidator) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Annotation(java.lang.annotation.Annotation) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Optimizer(com.yahoo.elide.datastores.aggregation.query.Optimizer) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) SQLDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection) VersionQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.VersionQuery) Getter(lombok.Getter) ColumnArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.ColumnArgumentValidator) TableArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.TableArgumentValidator) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) QueryTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryTranslator) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) DataSource(javax.sql.DataSource) SQLTimeDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLTimeDimensionProjection) QueryEngine(com.yahoo.elide.datastores.aggregation.QueryEngine) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan) QueryPlanTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryPlanTranslator) FormulaValidator(com.yahoo.elide.datastores.aggregation.metadata.FormulaValidator) Pagination(com.yahoo.elide.core.request.Pagination) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Type(com.yahoo.elide.core.type.Type) Preconditions(com.google.common.base.Preconditions) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) VersionQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.VersionQuery) QueryPlanTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryPlanTranslator) Optimizer(com.yahoo.elide.datastores.aggregation.query.Optimizer) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan)

Example 64 with SQLTable

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable in project elide by yahoo.

the class DefaultQueryValidator method validateQueryArguments.

@Override
public void validateQueryArguments(Query query) {
    SQLTable table = (SQLTable) query.getSource();
    table.getArgumentDefinitions().forEach(tableArgument -> {
        Argument clientArgument = query.getArguments().get(tableArgument.getName());
        validateArgument(Optional.ofNullable(clientArgument), tableArgument, "table '" + query.getSource().getName() + "'");
    });
}
Also used : Argument(com.yahoo.elide.core.request.Argument) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)

Example 65 with SQLTable

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable in project elide by yahoo.

the class DefaultQueryValidator method validatePredicate.

protected void validatePredicate(Query query, FilterPredicate predicate) {
    SQLTable table = (SQLTable) query.getSource();
    Set<ColumnProjection> projections = extractFilterProjections(query, predicate);
    if (projections.isEmpty()) {
        return;
    }
    ColumnProjection projection = projections.iterator().next();
    validateColumn(query, projection);
    Column column = table.getColumn(Column.class, projection.getName());
    if (column.getValueType().equals(ValueType.ID)) {
        throw new InvalidOperationException("Filtering by ID is not supported on " + query.getSource().getName());
    }
    if (column.getValues() == null || column.getValues().isEmpty()) {
        return;
    }
    if (REGEX_OPERATORS.contains(predicate.getOperator())) {
        return;
    }
    predicate.getValues().forEach(value -> {
        if (!column.getValues().contains(value)) {
            throw new InvalidOperationException(String.format("Column '%s' values must match one of these values: %s", projection.getAlias(), column.getValues()));
        }
    });
}
Also used : Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException)

Aggregations

SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)76 Test (org.junit.jupiter.api.Test)59 Query (com.yahoo.elide.datastores.aggregation.query.Query)50 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)49 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)34 Path (com.yahoo.elide.core.Path)31 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)31 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)29 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)29 Argument (com.yahoo.elide.core.request.Argument)24 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)20 Date (java.util.Date)19 GameRevenue (example.GameRevenue)15 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)12 HashSet (java.util.HashSet)12 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)8 Namespace (com.yahoo.elide.datastores.aggregation.metadata.models.Namespace)8 HashMap (java.util.HashMap)8 TreeMap (java.util.TreeMap)8 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7