Search in sources :

Example 1 with AggStar

use of mondrian.rolap.aggmatcher.AggStar in project mondrian by pentaho.

the class SqlTupleReaderTest method testAddLevelMemberSql.

public void testAddLevelMemberSql() throws Exception {
    TupleConstraint constraint = mock(TupleConstraint.class);
    SqlQuery sqlQuery = mock(SqlQuery.class, Answers.RETURNS_MOCKS.get());
    RolapCube baseCube = mock(RolapCube.class);
    RolapLevel targetLevel = mock(RolapLevel.class);
    RolapCubeLevel levelIter = mock(RolapCubeLevel.class, Answers.RETURNS_MOCKS.get());
    RolapProperty rolapProperty = mock(RolapProperty.class, Answers.RETURNS_MOCKS.get());
    String propertyName = "property_1";
    when(rolapProperty.getName()).thenReturn(propertyName);
    RolapProperty[] properties = { rolapProperty };
    when(levelIter.getProperties()).thenReturn(properties);
    when(levelIter.getKeyExp()).thenReturn(mock(MondrianDef.Expression.class));
    when(levelIter.getOrdinalExp()).thenReturn(mock(MondrianDef.Expression.class));
    when(levelIter.getParentExp()).thenReturn(null);
    RolapHierarchy hierarchy = mock(RolapHierarchy.class, Answers.RETURNS_MOCKS.get());
    when(targetLevel.getHierarchy()).thenReturn(hierarchy);
    when(hierarchy.getLevels()).thenReturn(new RolapLevel[] { levelIter });
    SqlTupleReader.WhichSelect whichSelect = SqlTupleReader.WhichSelect.LAST;
    JdbcSchema.Table dbTable = mock(JdbcSchema.Table.class, Answers.RETURNS_MOCKS.get());
    when(dbTable.getColumnUsages(any())).thenReturn(mock(Iterator.class));
    RolapStar star = mock(RolapStar.class);
    when(star.getColumnCount()).thenReturn(1);
    AggStar aggStar = spy(AggStar.makeAggStar(star, dbTable, mock(MessageRecorder.class), 10));
    AggStar.Table.Column column = mock(AggStar.Table.Column.class, Answers.RETURNS_MOCKS.get());
    doReturn(column).when(aggStar).lookupColumn(0);
    RolapStar.Column starColumn = mock(RolapStar.Column.class, Answers.RETURNS_MOCKS.get());
    when(starColumn.getBitPosition()).thenReturn(0);
    doReturn(starColumn).when(levelIter).getStarKeyColumn();
    AggStar.FactTable factTable = (AggStar.FactTable) createInstance("mondrian.rolap.aggmatcher.AggStar$FactTable", new Class[] { mondrian.rolap.aggmatcher.AggStar.class, JdbcSchema.Table.class }, new Object[] { aggStar, dbTable }, AggStar.FactTable.class.getClassLoader());
    factTable = spy(factTable);
    Map<String, MondrianDef.Expression> propertiesAgg = new HashMap<>();
    propertiesAgg.put(propertyName, mock(MondrianDef.Expression.class));
    Class[] constructorArgsClasses = { mondrian.rolap.aggmatcher.AggStar.Table.class, String.class, MondrianDef.Expression.class, int.class, RolapStar.Column.class, boolean.class, MondrianDef.Expression.class, MondrianDef.Expression.class, Map.class };
    Object[] constructorArgs = { factTable, "name", mock(MondrianDef.Expression.class), 0, starColumn, true, mock(MondrianDef.Expression.class), null, propertiesAgg };
    AggStar.Table.Level aggStarLevel = (AggStar.Table.Level) createInstance("mondrian.rolap.aggmatcher.AggStar$Table$Level", constructorArgsClasses, constructorArgs, AggStar.Table.Level.class.getClassLoader());
    when(aggStar.lookupLevel(0)).thenReturn(aggStarLevel);
    doReturn(factTable).when(column).getTable();
    SqlTupleReader reader = new SqlTupleReader(constraint);
    reader.addLevelMemberSql(sqlQuery, targetLevel, baseCube, whichSelect, aggStar);
    verify(factTable).addToFrom(any(), eq(false), eq(true));
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) AggStar(mondrian.rolap.aggmatcher.AggStar) SqlQuery(mondrian.rolap.sql.SqlQuery) JdbcSchema(mondrian.rolap.aggmatcher.JdbcSchema) TupleConstraint(mondrian.rolap.sql.TupleConstraint) MondrianDef(mondrian.olap.MondrianDef)

Example 2 with AggStar

use of mondrian.rolap.aggmatcher.AggStar in project mondrian by pentaho.

the class TestAggregationManager method testAggStarWithUnusedColumnsRequiresRollup.

public void testAggStarWithUnusedColumnsRequiresRollup() {
    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\">" + "<Cube name=\"Sales\" defaultMeasure=\"Unit Sales\">\n" + "  <Table name=\"sales_fact_1997\" />\n" + "  <Dimension name=\"Gender\" foreignKey=\"customer_id\">\n" + "    <Hierarchy hasAll=\"true\" allMemberName=\"All Gender\" primaryKey=\"customer_id\">\n" + "      <Table name=\"customer\"/>\n" + "      <Level name=\"Gender\" column=\"gender\" uniqueMembers=\"true\"/>\n" + "    </Hierarchy>\n" + "  </Dimension>\n" + "  <Measure name=\"Unit Sales\" column=\"unit_sales\" aggregator=\"sum\"\n" + "      formatString=\"Standard\"/>\n" + "</Cube>\n" + "</Schema>");
    RolapStar star = context.getConnection().getSchemaReader().getSchema().getStar("sales_fact_1997");
    AggStar aggStarSpy = spy(getAggStar(star, "agg_c_special_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);
    assertTrue("Rollup should be true since AggStar has ignored columns ", rollup[0]);
    assertEquals(aggStarSpy, returnedStar);
    assertTrue("Unused columns are present, should be marked as " + "having ignored columns.", aggStarSpy.hasIgnoredColumns());
    String sqlOra = "select\n" + "    \"customer\".\"gender\" as \"c0\",\n" + "    sum(\"agg_c_special_sales_fact_1997\".\"unit_sales_sum\") as \"m0\"\n" + "from\n" + "    \"customer\" \"customer\",\n" + "    \"agg_c_special_sales_fact_1997\" \"agg_c_special_sales_fact_1997\"\n" + "where\n" + "    \"agg_c_special_sales_fact_1997\".\"customer_id\" = \"customer\".\"customer_id\"\n" + "group by\n" + "    \"customer\".\"gender\"";
    String sqlMysql = "select\n" + "    `customer`.`gender` as `c0`,\n" + "    sum(`agg_c_special_sales_fact_1997`.`unit_sales_sum`) as `m0`\n" + "from\n" + "    `customer` as `customer`,\n" + "    `agg_c_special_sales_fact_1997` as `agg_c_special_sales_fact_1997`\n" + "where\n" + "    `agg_c_special_sales_fact_1997`.`customer_id` = `customer`.`customer_id`\n" + "group by\n" + "    `customer`.`gender`";
    assertQuerySqlOrNot(context, "select gender.gender.members on 0 from sales", new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysql, sqlMysql.length()), new SqlPattern(Dialect.DatabaseProduct.ORACLE, sqlOra, sqlOra.length()) }, false, false, true);
}
Also used : TestContext(mondrian.test.TestContext) SqlPattern(mondrian.test.SqlPattern) AggStar(mondrian.rolap.aggmatcher.AggStar)

Example 3 with AggStar

use of mondrian.rolap.aggmatcher.AggStar in project mondrian by pentaho.

the class TestAggregationManager method testAggStarWithIgnoredColumnsRequiresRollup.

public void testAggStarWithIgnoredColumnsRequiresRollup() {
    propSaver.set(propSaver.properties.GenerateFormattedSql, true);
    propSaver.set(propSaver.properties.ReadAggregates, true);
    propSaver.set(propSaver.properties.UseAggregates, 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_c_10_sales_fact_1997\">\n" + "        <AggFactCount column=\"FACT_COUNT\"/>\n" + "        <AggIgnoreColumn column=\"Quarter\"/>\n" + "        <AggIgnoreColumn column=\"MONTH_OF_YEAR\"/>\n" + "        <AggMeasure name=\"[Measures].[Unit Sales]\" column=\"unit_sales\" />\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" + "</Cube>\n" + "</Schema>");
    RolapStar star = context.getConnection().getSchemaReader().getSchema().getStar("sales_fact_1997");
    AggStar aggStarSpy = spy(getAggStar(star, "agg_c_10_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);
    assertTrue("Rollup should be true since AggStar has ignored columns ", rollup[0]);
    assertEquals(aggStarSpy, returnedStar);
    assertTrue("Columns marked with AggIgnoreColumn, so AggStar " + ".hasIgnoredColumns() should be true", aggStarSpy.hasIgnoredColumns());
    String sqlMysql = "select\n" + "    `agg_c_10_sales_fact_1997`.`the_year` as `c0`,\n" + "    sum(`agg_c_10_sales_fact_1997`.`unit_sales`) as `m0`\n" + "from\n" + "    `agg_c_10_sales_fact_1997` as `agg_c_10_sales_fact_1997`\n" + "where\n" + "    `agg_c_10_sales_fact_1997`.`the_year` = 1997\n" + "group by\n" + "    `agg_c_10_sales_fact_1997`.`the_year`";
    String sqlOra = "select\n" + "    \"agg_c_10_sales_fact_1997\".\"the_year\" as \"c0\",\n" + "    sum(\"agg_c_10_sales_fact_1997\".\"unit_sales\") as \"m0\"\n" + "from\n" + "    \"agg_c_10_sales_fact_1997\" \"agg_c_10_sales_fact_1997\"\n" + "where\n" + "    \"agg_c_10_sales_fact_1997\".\"the_year\" = 1997\n" + "group by\n" + "    \"agg_c_10_sales_fact_1997\".\"the_year\"";
    assertQuerySqlOrNot(context, "select Time.[1997] on 0 from sales", new SqlPattern[] { new SqlPattern(Dialect.DatabaseProduct.MYSQL, sqlMysql, sqlMysql.length()), new SqlPattern(Dialect.DatabaseProduct.ORACLE, sqlOra, sqlOra.length()) }, false, false, true);
}
Also used : TestContext(mondrian.test.TestContext) SqlPattern(mondrian.test.SqlPattern) AggStar(mondrian.rolap.aggmatcher.AggStar)

Example 4 with AggStar

use of mondrian.rolap.aggmatcher.AggStar in project mondrian by pentaho.

the class RolapStar method reOrderAggStarList.

/**
 * Reorder the list of aggregate stars. This should be called if the
 * algorithm used to order the AggStars has been changed.
 */
public void reOrderAggStarList() {
    List<AggStar> oldList = new ArrayList<AggStar>(aggStars);
    aggStars.clear();
    for (AggStar aggStar : oldList) {
        addAggStar(aggStar);
    }
}
Also used : AggStar(mondrian.rolap.aggmatcher.AggStar)

Example 5 with AggStar

use of mondrian.rolap.aggmatcher.AggStar in project mondrian by pentaho.

the class RolapStar method print.

/**
 * Prints the state of this <code>RolapStar</code>
 *
 * @param pw Writer
 * @param prefix Prefix to print at the start of each line
 * @param structure Whether to print the structure of the star
 */
public void print(PrintWriter pw, String prefix, boolean structure) {
    if (structure) {
        pw.print(prefix);
        pw.println("RolapStar:");
        String subprefix = prefix + "  ";
        factTable.print(pw, subprefix);
        for (AggStar aggStar : getAggStars()) {
            aggStar.print(pw, subprefix);
        }
    }
}
Also used : AggStar(mondrian.rolap.aggmatcher.AggStar)

Aggregations

AggStar (mondrian.rolap.aggmatcher.AggStar)17 Table (mondrian.rolap.RolapStar.Table)6 Column (mondrian.rolap.RolapStar.Column)5 SqlPattern (mondrian.test.SqlPattern)3 TestContext (mondrian.test.TestContext)3 HashMap (java.util.HashMap)2 MondrianDef (mondrian.olap.MondrianDef)2 SqlQuery (mondrian.rolap.sql.SqlQuery)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TupleList (mondrian.calc.TupleList)1 ListColumnPredicate (mondrian.rolap.agg.ListColumnPredicate)1 JdbcSchema (mondrian.rolap.aggmatcher.JdbcSchema)1 TupleConstraint (mondrian.rolap.sql.TupleConstraint)1 Dialect (mondrian.spi.Dialect)1 FilteredIterableList (mondrian.util.FilteredIterableList)1