use of mondrian.test.SqlPattern in project mondrian by pentaho.
the class TestAggregationManager method testAggStarWithIgnoredColumnsAndCountDistinct.
public void testAggStarWithIgnoredColumnsAndCountDistinct() {
propSaver.set(propSaver.properties.ReadAggregates, true);
propSaver.set(propSaver.properties.UseAggregates, true);
propSaver.set(propSaver.properties.GenerateFormattedSql, true);
final TestContext context = TestContext.instance().withSchema("<Schema name=\"FoodMart\">" + " <Dimension name=\"Time\" type=\"TimeDimension\">\n" + " <Hierarchy hasAll=\"false\" primaryKey=\"time_id\">\n" + " <Table name=\"time_by_day\"/>\n" + " <Level name=\"Year\" column=\"the_year\" type=\"Numeric\" uniqueMembers=\"true\"\n" + " levelType=\"TimeYears\"/>\n" + " <Level name=\"Quarter\" column=\"quarter\" uniqueMembers=\"false\"\n" + " levelType=\"TimeQuarters\"/>\n" + " </Hierarchy>\n" + " </Dimension>\n" + "<Cube name=\"Sales\" defaultMeasure=\"Unit Sales\">\n" + " <Table name=\"sales_fact_1997\">\n" + " <AggExclude name=\"agg_c_special_sales_fact_1997\" />\n" + " <AggExclude name=\"agg_lc_100_sales_fact_1997\" />\n" + " <AggExclude name=\"agg_lc_10_sales_fact_1997\" />\n" + " <AggExclude name=\"agg_pc_10_sales_fact_1997\" />\n" + " <AggName name=\"agg_g_ms_pcat_sales_fact_1997\">\n" + " <AggFactCount column=\"FACT_COUNT\"/>\n" + " <AggIgnoreColumn column=\"Quarter\"/>\n" + " <AggIgnoreColumn column=\"MONTH_OF_YEAR\"/>\n" + " <AggMeasure name=\"[Measures].[Customer Count]\" column=\"customer_count\" />\n" + " <AggLevel name=\"[Time].[Year]\" column=\"the_year\" />\n" + " </AggName>\n" + " </Table>\n" + " <DimensionUsage name=\"Time\" source=\"Time\" foreignKey=\"time_id\"/>\n" + " <Measure name=\"Unit Sales\" column=\"unit_sales\" aggregator=\"sum\"\n" + " formatString=\"Standard\"/>\n" + " <Measure name=\"Customer Count\" column=\"customer_id\" aggregator=\"distinct-count\"\n" + " formatString=\"Standard\"/>\n" + "</Cube>\n" + "</Schema>");
RolapStar star = context.getConnection().getSchemaReader().getSchema().getStar("sales_fact_1997");
AggStar aggStarSpy = spy(getAggStar(star, "agg_g_ms_pcat_sales_fact_1997"));
// make sure the test AggStar will be prioritized first
when(aggStarSpy.getSize()).thenReturn(0l);
context.getConnection().getSchemaReader().getSchema().getStar("sales_fact_1997").addAggStar(aggStarSpy);
boolean[] rollup = { false };
AggStar returnedStar = AggregationManager.findAgg(star, aggStarSpy.getLevelBitKey(), aggStarSpy.getMeasureBitKey(), rollup);
assertNull("Should not find an agg star given that ignored or unused " + "columns are present, and loading distinct count measure", returnedStar);
String sqlOra = "select\n" + " \"time_by_day\".\"the_year\" as \"c0\",\n" + " count(distinct \"sales_fact_1997\".\"customer_id\") as \"m0\"\n" + "from\n" + " \"time_by_day\" \"time_by_day\",\n" + " \"sales_fact_1997\" \"sales_fact_1997\"\n" + "where\n" + " \"sales_fact_1997\".\"time_id\" = \"time_by_day\".\"time_id\"\n" + "and\n" + " \"time_by_day\".\"the_year\" = 1997\n" + "group by\n" + " \"time_by_day\".\"the_year\"";
String sqlMysql = "select\n" + " `time_by_day`.`the_year` as `c0`,\n" + " count(distinct `sales_fact_1997`.`customer_id`) as `m0`\n" + "from\n" + " `time_by_day` as `time_by_day`,\n" + " `sales_fact_1997` as `sales_fact_1997`\n" + "where\n" + " `sales_fact_1997`.`time_id` = `time_by_day`.`time_id`\n" + "and\n" + " `time_by_day`.`the_year` = 1997\n" + "group by\n" + " `time_by_day`.`the_year`";
assertQuerySqlOrNot(context, "select Time.[1997] on 0 from sales where " + "measures.[Customer Count]", new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysql, sqlMysql.length()), new SqlPattern(Dialect.DatabaseProduct.ORACLE, sqlOra, sqlOra.length()) }, false, false, true);
}
use of mondrian.test.SqlPattern in project mondrian by pentaho.
the class TestAggregationManager method testDisabledReadAggregatesIgnoresDefaultRules.
public void testDisabledReadAggregatesIgnoresDefaultRules() throws Exception {
propSaver.set(propSaver.properties.ReadAggregates, false);
propSaver.set(propSaver.properties.UseAggregates, true);
String sql = "select count(*) as `c0` from `agg_c_10_sales_fact_1997` as `agg_c_10_sales_fact_1997`";
assertQuerySqlOrNot(getTestContext(), "select from sales", new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sql, sql.length()) }, true, true, true);
}
use of mondrian.test.SqlPattern in project mondrian by pentaho.
the class TestAggregationManager method testNonCollapsedAggregate.
public void testNonCollapsedAggregate() throws Exception {
propSaver.set(MondrianProperties.instance().UseAggregates, true);
propSaver.set(MondrianProperties.instance().ReadAggregates, true);
final String cube = "<Cube name=\"Foo\" defaultMeasure=\"Unit Sales\">\n" + " <Table name=\"sales_fact_1997\">\n" + " <AggExclude name=\"agg_g_ms_pcat_sales_fact_1997\"/>" + " <AggExclude name=\"agg_c_14_sales_fact_1997\"/>" + " <AggExclude name=\"agg_pl_01_sales_fact_1997\"/>" + " <AggExclude name=\"agg_ll_01_sales_fact_1997\"/>" + " <AggName name=\"agg_l_05_sales_fact_1997\">" + " <AggFactCount column=\"fact_count\"/>\n" + " <AggIgnoreColumn column=\"customer_id\"/>\n" + " <AggIgnoreColumn column=\"store_id\"/>\n" + " <AggIgnoreColumn column=\"promotion_id\"/>\n" + " <AggIgnoreColumn column=\"store_sales\"/>\n" + " <AggIgnoreColumn column=\"store_cost\"/>\n" + " <AggMeasure name=\"[Measures].[Unit Sales]\" column=\"unit_sales\" />\n" + " <AggLevel name=\"[Product].[Product Id]\" column=\"product_id\" collapsed=\"false\"/>\n" + " </AggName>\n" + "</Table>\n" + "<Dimension foreignKey=\"product_id\" name=\"Product\">\n" + "<Hierarchy hasAll=\"true\" primaryKey=\"product_id\" primaryKeyTable=\"product\">\n" + " <Join leftKey=\"product_class_id\" rightKey=\"product_class_id\">\n" + " <Table name=\"product\"/>\n" + " <Table name=\"product_class\"/>\n" + " </Join>\n" + " <Level name=\"Product Family\" table=\"product_class\" column=\"product_family\"\n" + " uniqueMembers=\"true\"/>\n" + " <Level name=\"Product Department\" table=\"product_class\" column=\"product_department\"\n" + " uniqueMembers=\"false\"/>\n" + " <Level name=\"Product Category\" table=\"product_class\" column=\"product_category\"\n" + " uniqueMembers=\"false\"/>\n" + " <Level name=\"Product Subcategory\" table=\"product_class\" column=\"product_subcategory\"\n" + " uniqueMembers=\"false\"/>\n" + " <Level name=\"Brand Name\" table=\"product\" column=\"brand_name\" uniqueMembers=\"false\"/>\n" + " <Level name=\"Product Name\" table=\"product\" column=\"product_name\"\n" + " uniqueMembers=\"true\"/>\n" + " <Level name=\"Product Id\" table=\"product\" column=\"product_id\"\n" + " uniqueMembers=\"true\"/>\n" + "</Hierarchy>\n" + "</Dimension>\n" + "<Measure name=\"Unit Sales\" column=\"unit_sales\" aggregator=\"sum\"\n" + " formatString=\"Standard\"/>\n" + "</Cube>\n";
final TestContext context = TestContext.instance().create(null, cube, null, null, null, null);
final String mdx = "select {[Product].[Product Family].Members} on rows, {[Measures].[Unit Sales]} on columns from [Foo]";
final String sqlOracle = "select \"product_class\".\"product_family\" as \"c0\", sum(\"agg_l_05_sales_fact_1997\".\"unit_sales\") as \"m0\" from \"product_class\" \"product_class\", \"product\" \"product\", \"agg_l_05_sales_fact_1997\" \"agg_l_05_sales_fact_1997\" where \"agg_l_05_sales_fact_1997\".\"product_id\" = \"product\".\"product_id\" and \"product\".\"product_class_id\" = \"product_class\".\"product_class_id\" group by \"product_class\".\"product_family\"";
final String sqlMysql = "select `product_class`.`product_family` as `c0`, sum(`agg_l_05_sales_fact_1997`.`unit_sales`) as `m0` from `product_class` as `product_class`, `product` as `product`, `agg_l_05_sales_fact_1997` as `agg_l_05_sales_fact_1997` where `agg_l_05_sales_fact_1997`.`product_id` = `product`.`product_id` and `product`.`product_class_id` = `product_class`.`product_class_id` group by `product_class`.`product_family`";
assertQuerySqlOrNot(context, mdx, new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.ORACLE, sqlOracle, sqlOracle.length()), new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysql, sqlMysql.length()) }, false, false, true);
}
use of mondrian.test.SqlPattern in project mondrian by pentaho.
the class TestAggregationManager method testMondrian1271.
/**
* This is a test for
* <a href="http://jira.pentaho.com/browse/MONDRIAN-1271">MONDRIAN-1271</a>
*
* When a non-collapsed AggLevel was used, Mondrian would join on the
* key column of the lowest level instead of the one it should have.
*/
public void testMondrian1271() {
if (!propSaver.properties.EnableNativeCrossJoin.get()) {
return;
}
propSaver.set(MondrianProperties.instance().UseAggregates, true);
propSaver.set(MondrianProperties.instance().ReadAggregates, true);
propSaver.set(propSaver.properties.GenerateFormattedSql, true);
final String schema = "<?xml version=\"1.0\"?>\n" + "<Schema name=\"custom\">\n" + " <Dimension name=\"Store\">\n" + " <Hierarchy hasAll=\"true\" primaryKey=\"store_id\">\n" + " <Table name=\"store\"/>\n" + " <Level name=\"Store Country\" column=\"store_country\" uniqueMembers=\"true\"/>\n" + " <Level name=\"Store State\" column=\"store_state\" uniqueMembers=\"true\"/>\n" + " <Level name=\"Store City\" column=\"store_city\" uniqueMembers=\"false\"/>\n" + " <Level name=\"Store Name\" column=\"store_name\" uniqueMembers=\"true\">\n" + " </Level>\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <Dimension name=\"Time\" type=\"TimeDimension\">\n" + " <Hierarchy hasAll=\"false\" primaryKey=\"time_id\">\n" + " <Table name=\"time_by_day\"/>\n" + " <Level name=\"Year\" column=\"the_year\" type=\"Numeric\" uniqueMembers=\"true\"\n" + " levelType=\"TimeYears\"/>\n" + " <Level name=\"Quarter\" column=\"quarter\" uniqueMembers=\"false\"\n" + " levelType=\"TimeQuarters\"/>\n" + " <Level name=\"Month\" column=\"month_of_year\" uniqueMembers=\"true\" type=\"Numeric\"\n" + " levelType=\"TimeMonths\"/>\n" + " <Level name=\"Day\" column=\"day_of_month\" uniqueMembers=\"false\" type=\"Numeric\"\n" + " levelType=\"TimeDays\"/>\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <Cube name=\"Sales1\" defaultMeasure=\"Unit Sales\">\n" + " <Table name=\"sales_fact_1997\">\n" + " <AggExclude name=\"agg_c_special_sales_fact_1997\"/>" + " <AggExclude name=\"agg_c_10_sales_fact_1997\"/>" + " <AggExclude name=\"agg_l_04_sales_fact_1997\"/>" + " <AggExclude name=\"agg_g_ms_pcat_sales_fact_1997\"/>" + " <AggExclude name=\"agg_lc_06_sales_fact_1997\"/>" + " <AggExclude name=\"agg_l_03_sales_fact_1997\"/>" + " <AggExclude name=\"agg_lc_100_sales_fact_1997\"/>" + " <AggExclude name=\"agg_pl_01_sales_fact_1997\"/>" + " <AggExclude name=\"agg_ll_01_sales_fact_1997\"/>" + " <AggExclude name=\"agg_l_05_sales_fact_1997\"/>" + " <AggName name=\"agg_c_14_sales_fact_1997\">\n" + " <AggFactCount column=\"fact_count\"/>\n" + " <AggIgnoreColumn column=\"product_id\" />\n" + " <AggIgnoreColumn column=\"customer_id\" />\n" + " <AggIgnoreColumn column=\"promotion_id\" />\n" + " <AggIgnoreColumn column=\"the_year\" />\n" + " <AggIgnoreColumn column=\"quarter\" />\n" + " <AggForeignKey factColumn=\"store_id\" aggColumn=\"store_id\" />\n" + " <AggMeasure name=\"[Measures].[Unit Sales]\" column=\"unit_sales\" />\n" + " <AggMeasure name=\"[Measures].[Store Cost]\" column=\"store_cost\" />\n" + " <AggMeasure name=\"[Measures].[Store Sales]\" column=\"store_sales\" />\n" + " <AggLevel name=\"[Time].[Month]\" column=\"month_of_year\" collapsed=\"false\" />\n" + " </AggName>\n" + " </Table>\n" + " <DimensionUsage name=\"Store\" source=\"Store\" foreignKey=\"store_id\"/>\n" + " <DimensionUsage name=\"Time\" source=\"Time\" foreignKey=\"time_id\"/>\n" + " <Measure name=\"Unit Sales\" column=\"unit_sales\" aggregator=\"sum\"\n" + " formatString=\"Standard\"/>\n" + " <Measure name=\"Store Cost\" column=\"store_cost\" aggregator=\"sum\"\n" + " formatString=\"#,###.00\"/>\n" + " <Measure name=\"Store Sales\" column=\"store_sales\" aggregator=\"sum\"\n" + " formatString=\"#,###.00\"/>\n" + " </Cube>\n" + "</Schema>\n";
final String mdx = "select {NonEmptyCrossJoin([Time].[Year].Members, [Store].[Store Country].Members)} on rows," + "{[Measures].[Unit Sales]} on columns " + "from [Sales1]";
final String mdxTooLowForAgg = "select {NonEmptyCrossJoin([Time].[Day].Members, [Store].[Store Country].Members)} on rows," + "{[Measures].[Unit Sales]} on columns " + "from [Sales1]";
final String sqlMysqlTupleQuery = "select\n" + " `time_by_day`.`the_year` as `c0`,\n" + " `store`.`store_country` as `c1`\n" + "from\n" + " `time_by_day` as `time_by_day`,\n" + " `agg_c_14_sales_fact_1997` as `agg_c_14_sales_fact_1997`,\n" + " `store` as `store`\n" + "where\n" + " `agg_c_14_sales_fact_1997`.`month_of_year` = `time_by_day`.`month_of_year`\n" + "and\n" + " `agg_c_14_sales_fact_1997`.`store_id` = `store`.`store_id`\n" + "group by\n" + " `time_by_day`.`the_year`,\n" + " `store`.`store_country`\n" + "order by\n" + (TestContext.instance().getDialect().requiresOrderByAlias() ? " ISNULL(`c0`) ASC, `c0` ASC,\n" + " ISNULL(`c1`) ASC, `c1` ASC" : " ISNULL(`time_by_day`.`the_year`) ASC, `time_by_day`.`the_year` ASC,\n" + " ISNULL(`store`.`store_country`) ASC, `store`.`store_country` ASC");
final String sqlMysqlSegmentQuery = "select\n" + " `store`.`store_country` as `c0`,\n" + " `time_by_day`.`the_year` as `c1`,\n" + " sum(`agg_c_14_sales_fact_1997`.`unit_sales`) as `m0`\n" + "from\n" + " `store` as `store`,\n" + " `agg_c_14_sales_fact_1997` as `agg_c_14_sales_fact_1997`,\n" + " `time_by_day` as `time_by_day`\n" + "where\n" + " `agg_c_14_sales_fact_1997`.`store_id` = `store`.`store_id`\n" + "and\n" + " `store`.`store_country` = 'USA'\n" + "and\n" + " `agg_c_14_sales_fact_1997`.`month_of_year` = `time_by_day`.`month_of_year`\n" + "group by\n" + " `store`.`store_country`,\n" + " `time_by_day`.`the_year`";
final String sqlMysqlTooLowTupleQuery = "select\n" + " `time_by_day`.`the_year` as `c0`,\n" + " `time_by_day`.`quarter` as `c1`,\n" + " `time_by_day`.`month_of_year` as `c2`,\n" + " `time_by_day`.`day_of_month` as `c3`,\n" + " `store`.`store_country` as `c4`\n" + "from\n" + " `time_by_day` as `time_by_day`,\n" + " `sales_fact_1997` as `sales_fact_1997`,\n" + " `store` as `store`\n" + "where\n" + " `sales_fact_1997`.`time_id` = `time_by_day`.`time_id`\n" + "and\n" + " `sales_fact_1997`.`store_id` = `store`.`store_id`\n" + "group by\n" + " `time_by_day`.`the_year`,\n" + " `time_by_day`.`quarter`,\n" + " `time_by_day`.`month_of_year`,\n" + " `time_by_day`.`day_of_month`,\n" + " `store`.`store_country`\n" + "order by\n" + (TestContext.instance().getDialect().requiresOrderByAlias() ? " ISNULL(`c0`) ASC, `c0` ASC,\n" + " ISNULL(`c1`) ASC, `c1` ASC,\n" + " ISNULL(`c2`) ASC, `c2` ASC,\n" + " ISNULL(`c3`) ASC, `c3` ASC,\n" + " ISNULL(`c4`) ASC, `c4` ASC" : " ISNULL(`time_by_day`.`the_year`) ASC, `time_by_day`.`the_year` ASC,\n" + " ISNULL(`time_by_day`.`quarter`) ASC, `time_by_day`.`quarter` ASC,\n" + " ISNULL(`time_by_day`.`month_of_year`) ASC, `time_by_day`.`month_of_year` ASC,\n" + " ISNULL(`time_by_day`.`day_of_month`) ASC, `time_by_day`.`day_of_month` ASC,\n" + " ISNULL(`store`.`store_country`) ASC, `store`.`store_country` ASC");
final String sqlMysqlTooLowSegmentQuery = "select\n" + " `store`.`store_country` as `c0`,\n" + " `time_by_day`.`month_of_year` as `c1`,\n" + " `time_by_day`.`day_of_month` as `c2`,\n" + " sum(`sales_fact_1997`.`unit_sales`) as `m0`\n" + "from\n" + " `store` as `store`,\n" + " `sales_fact_1997` as `sales_fact_1997`,\n" + " `time_by_day` as `time_by_day`\n" + "where\n" + " `sales_fact_1997`.`store_id` = `store`.`store_id`\n" + "and\n" + " `store`.`store_country` = 'USA'\n" + "and\n" + " `sales_fact_1997`.`time_id` = `time_by_day`.`time_id`\n" + "group by\n" + " `store`.`store_country`,\n" + " `time_by_day`.`month_of_year`,\n" + " `time_by_day`.`day_of_month`";
final TestContext context = TestContext.instance().withSchema(schema);
assertQuerySqlOrNot(context, mdx, new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysqlTupleQuery, sqlMysqlTupleQuery.length()) }, false, false, true);
assertQuerySqlOrNot(context, mdx, new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysqlSegmentQuery, sqlMysqlSegmentQuery.length()) }, false, false, true);
// Because we have caused a many-to-many relation between the agg table
// and the dim table, we expect retarded numbers here.
context.assertQueryReturns(mdx, "Axis #0:\n" + "{}\n" + "Axis #1:\n" + "{[Measures].[Unit Sales]}\n" + "Axis #2:\n" + "{[Time].[1997], [Store].[USA]}\n" + "{[Time].[1998], [Store].[USA]}\n" + "Row #0: 8,119,905\n" + "Row #1: 8,119,905\n");
// Make sure that queries on lower levels don't trigger a
// false positive with the agg matcher.
assertQuerySqlOrNot(context, mdxTooLowForAgg, new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysqlTooLowTupleQuery, sqlMysqlTooLowTupleQuery.length()) }, false, false, true);
assertQuerySqlOrNot(context, mdxTooLowForAgg, new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysqlTooLowSegmentQuery, sqlMysqlTooLowSegmentQuery.length()) }, false, false, true);
}
use of mondrian.test.SqlPattern in project mondrian by pentaho.
the class TestAggregationManager method testColumnCadinalityCache.
/**
* Test that once fetched, column cardinality can be shared between
* different queries using the same connection.
*
* <p>Test also that expressions with only table alias difference do not
* share cardinality result.
*/
public void testColumnCadinalityCache() {
String query1 = "select " + "NonEmptyCrossJoin(" + "[Product].[Product Family].Members, " + "[Gender].[Gender].Members) on columns " + "from [Sales]";
String query2 = "select " + "NonEmptyCrossJoin(" + "[Store].[Store Country].Members, " + "[Product].[Product Family].Members) on columns " + "from [Warehouse]";
String cardinalitySqlDerby = "select " + "count(distinct \"product_class\".\"product_family\") " + "from \"product_class\" as \"product_class\"";
String cardinalitySqlMySql = "select " + "count(distinct `product_class`.`product_family`) as `c0` " + "from `product_class` as `product_class`";
SqlPattern[] patterns = new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.DERBY, cardinalitySqlDerby, cardinalitySqlDerby), new SqlPattern(Dialect.DatabaseProduct.MYSQL, cardinalitySqlMySql, cardinalitySqlMySql) };
final TestContext context = getTestContext().withFreshConnection();
try {
// This MDX gets the [Product].[Product Family] cardinality
// from the DB.
context.executeQuery(query1);
// This MDX should be able to reuse the cardinality for
// [Product].[Product Family]; and should not issue a SQL to fetch
// that from DB again.
assertQuerySqlOrNot(context, query2, patterns, true, false, false);
} finally {
context.close();
}
}
Aggregations