Search in sources :

Example 6 with SQLDialect

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect in project elide by yahoo.

the class MetaDataStoreIntegrationTest method createHarness.

@Override
protected DataStoreTestHarness createHarness() {
    HikariConfig config = new HikariConfig(File.separator + "jpah2db.properties");
    DataSource defaultDataSource = new HikariDataSource(config);
    SQLDialect defaultDialect = SQLDialectFactory.getDefaultDialect();
    ConnectionDetails defaultConnectionDetails = new ConnectionDetails(defaultDataSource, defaultDialect);
    Properties prop = new Properties();
    prop.put("javax.persistence.jdbc.driver", config.getDriverClassName());
    prop.put("javax.persistence.jdbc.url", config.getJdbcUrl());
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("aggregationStore", prop);
    Map<String, ConnectionDetails> connectionDetailsMap = new HashMap<>();
    // Add an entry for "mycon" connection which is not from hjson
    connectionDetailsMap.put("mycon", defaultConnectionDetails);
    // Add connection details fetched from hjson
    VALIDATOR.getElideSQLDBConfig().getDbconfigs().forEach(dbConfig -> connectionDetailsMap.put(dbConfig.getName(), new ConnectionDetails(getDataSource(dbConfig, getDBPasswordExtractor()), SQLDialectFactory.getDialect(dbConfig.getDialect()))));
    return new AggregationDataStoreTestHarness(emf, defaultConnectionDetails, connectionDetailsMap, VALIDATOR);
}
Also used : AggregationDataStoreTestHarness(com.yahoo.elide.datastores.aggregation.framework.AggregationDataStoreTestHarness) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HashMap(java.util.HashMap) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) EntityManagerFactory(javax.persistence.EntityManagerFactory) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) HikariConfig(com.zaxxer.hikari.HikariConfig) Properties(java.util.Properties) DataSource(javax.sql.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) AggregationDataStoreIntegrationTest.getDataSource(com.yahoo.elide.datastores.aggregation.integration.AggregationDataStoreIntegrationTest.getDataSource)

Example 7 with SQLDialect

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect 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 8 with SQLDialect

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect in project elide by yahoo.

the class SQLQueryEngine method getPageTotal.

private long getPageTotal(Query expandedQuery, NativeQuery sql, Query clientQuery, SqlTransaction sqlTransaction) {
    ConnectionDetails details = expandedQuery.getConnectionDetails();
    DataSource dataSource = details.getDataSource();
    SQLDialect dialect = details.getDialect();
    NativeQuery paginationSQL = toPageTotalSQL(expandedQuery, sql, dialect);
    if (paginationSQL == null) {
        // Only 1 record will be returned.
        return 1;
    }
    NamedParamPreparedStatement stmt = sqlTransaction.initializeStatement(paginationSQL.toString(), dataSource);
    // Supply the query parameters to the query
    supplyFilterQueryParameters(clientQuery, stmt, dialect);
    // Run the Pagination query and log the time spent.
    Long result = CoerceUtil.coerce(runQuery(stmt, paginationSQL.toString(), SINGLE_RESULT_MAPPER), Long.class);
    return (result != null) ? result : 0;
}
Also used : NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) DataSource(javax.sql.DataSource)

Example 9 with SQLDialect

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect in project elide by yahoo.

the class SQLColumnProjection method canNest.

@Override
default boolean canNest(Queryable source, MetaDataStore metaDataStore) {
    SQLDialect dialect = source.getConnectionDetails().getDialect();
    String sql = toSQL(source, metaDataStore);
    SyntaxVerifier verifier = new SyntaxVerifier(dialect);
    boolean canNest = verifier.verify(sql);
    if (!canNest) {
        LOGGER.debug("Unable to nest {} because {}", this.getName(), verifier.getLastError());
        return false;
    }
    List<Reference> references = new ExpressionParser(metaDataStore).parse(source, this);
    // because rewriting the SQL in the outer expression will lose the context of the calling $$column.
    return references.stream().map(reference -> reference.accept(new ReferenceExtractor<JoinReference>(JoinReference.class, metaDataStore, ReferenceExtractor.Mode.SAME_QUERY))).flatMap(Set::stream).map(reference -> reference.accept(new ReferenceExtractor<ColumnArgReference>(ColumnArgReference.class, metaDataStore))).flatMap(Set::stream).collect(Collectors.toSet()).isEmpty();
}
Also used : SyntaxVerifier(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier) ReferenceExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) Collectors(java.util.stream.Collectors) PhysicalRefColumnContext(com.yahoo.elide.datastores.aggregation.metadata.PhysicalRefColumnContext) List(java.util.List) Pair(org.apache.commons.lang3.tuple.Pair) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) PhysicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) HasJoinVisitor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.HasJoinVisitor) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) LinkedHashSet(java.util.LinkedHashSet) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) PhysicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) SyntaxVerifier(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)

Aggregations

SQLDialect (com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect)9 DataSource (javax.sql.DataSource)6 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)4 Query (com.yahoo.elide.datastores.aggregation.query.Query)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Argument (com.yahoo.elide.core.request.Argument)3 MetricProjection (com.yahoo.elide.datastores.aggregation.query.MetricProjection)3 ConnectionDetails (com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails)3 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)3 NativeQuery (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery)3 HikariConfig (com.zaxxer.hikari.HikariConfig)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 Properties (java.util.Properties)3 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2