Search in sources :

Example 1 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class AggregationDataStoreTransaction method addColumnFilterArguments.

@VisibleForTesting
Query addColumnFilterArguments(Table table, Query query, EntityDictionary dictionary) {
    Query.QueryBuilder queryBuilder = Query.builder();
    query.getColumnProjections().stream().forEach(projection -> {
        Column column = table.getColumn(Column.class, projection.getName());
        FilterExpression requiredFilter = column.getRequiredFilter(dictionary);
        if (requiredFilter != null) {
            Map<String, Argument> allArguments = validateRequiredFilter(requiredFilter, query, column);
            if (projection.getArguments() != null) {
                allArguments.putAll(projection.getArguments());
            }
            queryBuilder.column(projection.withArguments(allArguments));
        } else {
            queryBuilder.column(projection);
        }
    });
    return queryBuilder.arguments(query.getArguments()).havingFilter(query.getHavingFilter()).whereFilter(query.getWhereFilter()).sorting(query.getSorting()).pagination(query.getPagination()).bypassingCache(query.isBypassingCache()).source(query.getSource()).scope(query.getScope()).build();
}
Also used : Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class AggregationDataStoreTransaction method buildQuery.

@VisibleForTesting
Query buildQuery(EntityProjection entityProjection, RequestScope scope) {
    Table table = metaDataStore.getTable(scope.getDictionary().getJsonAliasFor(entityProjection.getType()), scope.getApiVersion());
    String bypassCacheStr = scope.getRequestHeaderByName("bypasscache");
    Boolean bypassCache = "true".equals(bypassCacheStr);
    EntityProjectionTranslator translator = new EntityProjectionTranslator(queryEngine, table, entityProjection, scope, bypassCache);
    Query query = translator.getQuery();
    Query modifiedQuery = addTableFilterArguments(table, query, scope.getDictionary());
    modifiedQuery = addColumnFilterArguments(table, modifiedQuery, scope.getDictionary());
    return modifiedQuery;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) ToString(lombok.ToString) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class AggregationDataStoreTransaction method loadObjects.

@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection entityProjection, RequestScope scope) {
    QueryResult result = null;
    QueryResponse response = null;
    String cacheKey = null;
    try {
        // Convert multivalued map to map.
        Map<String, String> headers = scope.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().stream().collect(Collectors.joining(" "))));
        queryLogger.acceptQuery(scope.getRequestId(), scope.getUser(), headers, scope.getApiVersion(), scope.getQueryParams(), scope.getPath());
        Query query = buildQuery(entityProjection, scope);
        Table table = (Table) query.getSource();
        if (cache != null && !query.isBypassingCache()) {
            String tableVersion = queryEngine.getTableVersion(table, queryEngineTransaction);
            tableVersion = tableVersion == null ? "" : tableVersion;
            cacheKey = tableVersion + ';' + QueryKeyExtractor.extractKey(query);
            result = cache.get(cacheKey);
        }
        boolean isCached = result != null;
        List<String> queryText = queryEngine.explain(query);
        queryLogger.processQuery(scope.getRequestId(), query, queryText, isCached);
        if (result == null) {
            result = queryEngine.executeQuery(query, queryEngineTransaction);
            if (cacheKey != null) {
                // The query result needs to be streamed into an in memory list before caching.
                // TODO - add a cap to how many records can be streamed back.  If this is exceeded, abort caching
                // and return the results.
                QueryResult cacheableResult = QueryResult.builder().data(Lists.newArrayList(result.getData().iterator())).pageTotals(result.getPageTotals()).build();
                cache.put(cacheKey, cacheableResult);
                result = cacheableResult;
            }
        }
        if (entityProjection.getPagination() != null && entityProjection.getPagination().returnPageTotals()) {
            entityProjection.getPagination().setPageTotals(result.getPageTotals());
        }
        response = new QueryResponse(HttpStatus.SC_OK, result.getData(), null);
        return new DataStoreIterableBuilder(result.getData()).build();
    } catch (HttpStatusException e) {
        response = new QueryResponse(e.getStatus(), null, e.getMessage());
        throw e;
    } catch (Exception e) {
        response = new QueryResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, null, e.getMessage());
        throw e;
    } finally {
        queryLogger.completeQuery(scope.getRequestId(), response);
    }
}
Also used : HttpStatus(com.yahoo.elide.core.exceptions.HttpStatus) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Argument(com.yahoo.elide.core.request.Argument) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) MatchesTemplateVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.MatchesTemplateVisitor) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) RequiresFilter(com.yahoo.elide.datastores.aggregation.metadata.models.RequiresFilter) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) Lists(org.apache.commons.compress.utils.Lists) EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryKeyExtractor(com.yahoo.elide.datastores.aggregation.cache.QueryKeyExtractor) Type(com.yahoo.elide.core.type.Type) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) ToString(lombok.ToString) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class SQLQueryEngine method executeQuery.

@Override
public QueryResult executeQuery(Query query, Transaction transaction) {
    SqlTransaction sqlTransaction = (SqlTransaction) transaction;
    ConnectionDetails details = query.getConnectionDetails();
    DataSource dataSource = details.getDataSource();
    SQLDialect dialect = details.getDialect();
    Query expandedQuery = expandMetricQueryPlans(query);
    // Translate the query into SQL.
    NativeQuery sql = toSQL(expandedQuery, dialect);
    String queryString = sql.toString();
    QueryResult.QueryResultBuilder resultBuilder = QueryResult.builder();
    NamedParamPreparedStatement stmt;
    Pagination pagination = query.getPagination();
    if (returnPageTotals(pagination)) {
        resultBuilder.pageTotals(getPageTotal(expandedQuery, sql, query, sqlTransaction));
    }
    log.debug("SQL Query: " + queryString);
    stmt = sqlTransaction.initializeStatement(queryString, dataSource);
    // Supply the query parameters to the query
    supplyFilterQueryParameters(query, stmt, dialect);
    // Run the primary query and log the time spent.
    ResultSet resultSet = runQuery(stmt, queryString, Function.identity());
    resultBuilder.data(new EntityHydrator(resultSet, query, metadataDictionary));
    return resultBuilder.build();
}
Also used : 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) DataSource(javax.sql.DataSource) Pagination(com.yahoo.elide.core.request.Pagination) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) ResultSet(java.sql.ResultSet)

Example 5 with Query

use of com.yahoo.elide.datastores.aggregation.query.Query in project elide by yahoo.

the class SQLQueryEngine method explain.

/**
 * Returns the actual query string(s) that would be executed for the input {@link Query}.
 *
 * @param query The query customized for a particular persistent storage or storage client.
 * @param dialect SQL dialect to use for this storage.
 * @return List of SQL string(s) corresponding to the given query.
 */
public List<String> explain(Query query, SQLDialect dialect) {
    List<String> queries = new ArrayList<>();
    Query expandedQuery = expandMetricQueryPlans(query);
    NativeQuery sql = toSQL(expandedQuery, dialect);
    Pagination pagination = query.getPagination();
    if (returnPageTotals(pagination)) {
        NativeQuery paginationSql = toPageTotalSQL(expandedQuery, sql, dialect);
        if (paginationSql != null) {
            queries.add(paginationSql.toString());
        }
    }
    queries.add(sql.toString());
    return queries;
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) 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) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) ArrayList(java.util.ArrayList)

Aggregations

Query (com.yahoo.elide.datastores.aggregation.query.Query)242 Test (org.junit.jupiter.api.Test)229 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)214 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)46 Argument (com.yahoo.elide.core.request.Argument)43 ArrayList (java.util.ArrayList)42 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)37 Path (com.yahoo.elide.core.Path)35 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)31 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)29 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)29 PlayerStats (example.PlayerStats)29 TreeMap (java.util.TreeMap)29 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)28 HashMap (java.util.HashMap)26 Date (java.util.Date)19 HashSet (java.util.HashSet)17 ToString (lombok.ToString)16 SQLDimensionProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection)14