Search in sources :

Example 1 with StatisticsProvider

use of mondrian.spi.StatisticsProvider in project mondrian by pentaho.

the class JdbcDialectImpl method computeStatisticsProviders.

protected List<StatisticsProvider> computeStatisticsProviders() {
    List<String> names = getStatisticsProviderNames();
    if (names == null) {
        return Collections.<StatisticsProvider>singletonList(new SqlStatisticsProvider());
    }
    final List<StatisticsProvider> providerList = new ArrayList<StatisticsProvider>();
    for (String name : names) {
        try {
            StatisticsProvider provider = ClassResolver.INSTANCE.instantiateSafe(name);
            providerList.add(provider);
        } catch (Exception e) {
            LOGGER.info("Error instantiating statistics provider (class=" + name + ")", e);
        }
    }
    return providerList;
}
Also used : ArrayList(java.util.ArrayList) StatisticsProvider(mondrian.spi.StatisticsProvider) SQLException(java.sql.SQLException)

Example 2 with StatisticsProvider

use of mondrian.spi.StatisticsProvider in project mondrian by pentaho.

the class RolapStatisticsCache method getQueryCardinality.

private long getQueryCardinality(String sql) {
    long rowCount = -1;
    if (queryMap.containsKey(sql)) {
        rowCount = queryMap.get(sql);
    } else {
        final Dialect dialect = star.getSqlQueryDialect();
        final List<StatisticsProvider> statisticsProviders = dialect.getStatisticsProviders();
        final Execution execution = new Execution(star.getSchema().getInternalConnection().getInternalStatement(), 0);
        for (StatisticsProvider statisticsProvider : statisticsProviders) {
            rowCount = statisticsProvider.getQueryCardinality(dialect, star.getDataSource(), sql, execution);
            if (rowCount >= 0) {
                break;
            }
        }
        // Note: If all providers fail, we put -1 into the cache, to ensure
        // that we won't try again.
        queryMap.put(sql, rowCount);
    }
    return rowCount;
}
Also used : Execution(mondrian.server.Execution) Dialect(mondrian.spi.Dialect) StatisticsProvider(mondrian.spi.StatisticsProvider)

Example 3 with StatisticsProvider

use of mondrian.spi.StatisticsProvider in project mondrian by pentaho.

the class RolapStatisticsCache method getTableCardinality.

private long getTableCardinality(String catalog, String schema, String table) {
    final List<String> key = Arrays.asList(catalog, schema, table);
    long rowCount = -1;
    if (tableMap.containsKey(key)) {
        rowCount = tableMap.get(key);
    } else {
        final Dialect dialect = star.getSqlQueryDialect();
        final List<StatisticsProvider> statisticsProviders = dialect.getStatisticsProviders();
        final Execution execution = new Execution(star.getSchema().getInternalConnection().getInternalStatement(), 0);
        for (StatisticsProvider statisticsProvider : statisticsProviders) {
            rowCount = statisticsProvider.getTableCardinality(dialect, star.getDataSource(), catalog, schema, table, execution);
            if (rowCount >= 0) {
                break;
            }
        }
        // Note: If all providers fail, we put -1 into the cache, to ensure
        // that we won't try again.
        tableMap.put(key, rowCount);
    }
    return rowCount;
}
Also used : Execution(mondrian.server.Execution) Dialect(mondrian.spi.Dialect) StatisticsProvider(mondrian.spi.StatisticsProvider)

Example 4 with StatisticsProvider

use of mondrian.spi.StatisticsProvider in project mondrian by pentaho.

the class BasicQueryTest method testStatistics.

/**
 * Unit test for {@link StatisticsProvider} and implementations {@link JdbcStatisticsProvider} and
 * {@link SqlStatisticsProvider}.
 */
public void testStatistics() {
    final String product = getTestContext().getDialect().getDatabaseProduct().name();
    final String dialectClassName = getTestContext().getDialect().getClass().getName();
    propSaver.set(new StringProperty(MondrianProperties.instance(), MondrianProperties.instance().StatisticsProviders.getPath() + "." + product, null), MyJdbcStatisticsProvider.class.getName() + "," + SqlStatisticsProvider.class.getName());
    final TestContext testContext = getTestContext().withFreshConnection();
    try {
        testContext.assertSimpleQuery();
        // bypass dialect cache and always get a fresh dialect instance
        // with our custom providers
        Dialect dialect = DialectManager.createDialect(testContext.getConnection().getDataSource(), null, dialectClassName);
        final List<StatisticsProvider> statisticsProviders = dialect.getStatisticsProviders();
        assertEquals(2, statisticsProviders.size());
        assertTrue(statisticsProviders.get(0) instanceof MyJdbcStatisticsProvider);
        assertTrue(statisticsProviders.get(1) instanceof SqlStatisticsProvider);
        for (StatisticsProvider statisticsProvider : statisticsProviders) {
            long rowCount = statisticsProvider.getTableCardinality(dialect, testContext.getConnection().getDataSource(), null, null, "customer", new Execution(((RolapSchema) testContext.getConnection().getSchema()).getInternalConnection().getInternalStatement(), 0));
            if (statisticsProvider instanceof SqlStatisticsProvider) {
                assertTrue("Row count estimate: " + rowCount + " (actual 10281)", rowCount > 10000 && rowCount < 15000);
            }
            long valueCount = statisticsProvider.getColumnCardinality(dialect, testContext.getConnection().getDataSource(), null, null, "customer", "gender", new Execution(((RolapSchema) testContext.getConnection().getSchema()).getInternalConnection().getInternalStatement(), 0));
            assertTrue("Value count estimate: " + valueCount + " (actual 2)", statisticsProvider instanceof JdbcStatisticsProvider ? valueCount == -1 : valueCount == 2);
        }
    } finally {
        testContext.close();
    }
}
Also used : SqlStatisticsProvider(mondrian.spi.impl.SqlStatisticsProvider) Execution(mondrian.server.Execution) JdbcStatisticsProvider(mondrian.spi.impl.JdbcStatisticsProvider) Dialect(mondrian.spi.Dialect) StringProperty(org.eigenbase.util.property.StringProperty) JdbcStatisticsProvider(mondrian.spi.impl.JdbcStatisticsProvider) StatisticsProvider(mondrian.spi.StatisticsProvider) SqlStatisticsProvider(mondrian.spi.impl.SqlStatisticsProvider)

Example 5 with StatisticsProvider

use of mondrian.spi.StatisticsProvider in project mondrian by pentaho.

the class RolapStatisticsCache method getColumnCardinality.

private long getColumnCardinality(String catalog, String schema, String table, String column) {
    final List<String> key = Arrays.asList(catalog, schema, table, column);
    long rowCount = -1;
    if (columnMap.containsKey(key)) {
        rowCount = columnMap.get(key);
    } else {
        final Dialect dialect = star.getSqlQueryDialect();
        final List<StatisticsProvider> statisticsProviders = dialect.getStatisticsProviders();
        final Execution execution = new Execution(star.getSchema().getInternalConnection().getInternalStatement(), 0);
        for (StatisticsProvider statisticsProvider : statisticsProviders) {
            rowCount = statisticsProvider.getColumnCardinality(dialect, star.getDataSource(), catalog, schema, table, column, execution);
            if (rowCount >= 0) {
                break;
            }
        }
        // Note: If all providers fail, we put -1 into the cache, to ensure
        // that we won't try again.
        columnMap.put(key, rowCount);
    }
    return rowCount;
}
Also used : Execution(mondrian.server.Execution) Dialect(mondrian.spi.Dialect) StatisticsProvider(mondrian.spi.StatisticsProvider)

Aggregations

StatisticsProvider (mondrian.spi.StatisticsProvider)5 Execution (mondrian.server.Execution)4 Dialect (mondrian.spi.Dialect)4 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 JdbcStatisticsProvider (mondrian.spi.impl.JdbcStatisticsProvider)1 SqlStatisticsProvider (mondrian.spi.impl.SqlStatisticsProvider)1 StringProperty (org.eigenbase.util.property.StringProperty)1