Search in sources :

Example 1 with Hierarchy

use of mondrian.olap.Hierarchy in project pentaho-kettle by pentaho.

the class MondrianHelper method createFlattenedOutput.

/**
 * Retrieve the rows from the opened query. Also create a description of the flattened output of the query. This call
 * populated rowMetaInterface and rows The query needs to be opened beforehand.
 *
 * @throws KettleDatabaseException
 *           in case something goes wrong
 *
 *           TODO: this is not quite working for our purposes.
 */
public void createFlattenedOutput() throws KettleDatabaseException {
    final Axis[] axes = result.getAxes();
    rows = new ArrayList<>();
    headings = new ArrayList<>();
    // 
    for (Axis axis : axes) {
        final List<Position> positions = axis.getPositions();
        if (positions.isEmpty()) {
            // even deduce column headings.
            return;
        }
        for (Member member : positions.get(0)) {
            Hierarchy hierarchy = member.getHierarchy();
            headings.add(hierarchy.getUniqueName());
        }
    }
    int[] coords = new int[axes.length];
    outputFlattenedRecurse(result, rows, new ArrayList<>(), coords, 0);
    outputRowMeta = new RowMeta();
    // 
    for (int i = 0; i < rows.size() && i < 1; i++) {
        List<Object> rowValues = rows.get(i);
        for (int c = 0; c < rowValues.size(); c++) {
            Object valueData = rowValues.get(c);
            int valueType;
            if (valueData instanceof String) {
                valueType = ValueMetaInterface.TYPE_STRING;
            } else if (valueData instanceof Date) {
                valueType = ValueMetaInterface.TYPE_DATE;
            } else if (valueData instanceof Boolean) {
                valueType = ValueMetaInterface.TYPE_BOOLEAN;
            } else if (valueData instanceof Integer) {
                valueType = ValueMetaInterface.TYPE_INTEGER;
                valueData = Long.valueOf(((Integer) valueData).longValue());
            } else if (valueData instanceof Short) {
                valueType = ValueMetaInterface.TYPE_INTEGER;
                valueData = Long.valueOf(((Short) valueData).longValue());
            } else if (valueData instanceof Byte) {
                valueType = ValueMetaInterface.TYPE_INTEGER;
                valueData = Long.valueOf(((Byte) valueData).longValue());
            } else if (valueData instanceof Long) {
                valueType = ValueMetaInterface.TYPE_INTEGER;
            } else if (valueData instanceof Double) {
                valueType = ValueMetaInterface.TYPE_NUMBER;
            } else if (valueData instanceof Float) {
                valueType = ValueMetaInterface.TYPE_NUMBER;
                valueData = Double.valueOf(((Float) valueData).doubleValue());
            } else if (valueData instanceof BigDecimal) {
                valueType = ValueMetaInterface.TYPE_BIGNUMBER;
            } else {
                throw new KettleDatabaseException(BaseMessages.getString(PKG, "MondrianInputErrorUnhandledType", valueData.getClass().toString()));
            }
            try {
                ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta(headings.get(c), valueType);
                outputRowMeta.addValueMeta(valueMeta);
                rowValues.set(i, valueData);
            } catch (Exception e) {
                throw new KettleDatabaseException(e);
            }
        }
    }
    // Now that we painstakingly found the metadata that comes out of the Mondrian database, cache it please...
    // 
    DBCacheEntry cacheEntry = new DBCacheEntry(databaseMeta.getName(), queryString);
    DBCache.getInstance().put(cacheEntry, outputRowMeta);
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Hierarchy(mondrian.olap.Hierarchy) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) Member(mondrian.olap.Member) Axis(mondrian.olap.Axis) Position(mondrian.olap.Position) DBCacheEntry(org.pentaho.di.core.DBCacheEntry) Date(java.util.Date) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) BigDecimal(java.math.BigDecimal) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 2 with Hierarchy

use of mondrian.olap.Hierarchy in project mondrian by pentaho.

the class SchemaTest method testHierarchyVisibility.

public void testHierarchyVisibility() throws Exception {
    for (Boolean testValue : new Boolean[] { true, false }) {
        String cubeDef = "<Cube name=\"Foo\">\n" + "  <Table name=\"store\"/>\n" + "  <Dimension name=\"Bar\">\n" + "    <Hierarchy name=\"Bacon\" hasAll=\"true\" visible=\"@REPLACE_ME@\">\n" + "      <Level name=\"Store Type\" column=\"store_type\" uniqueMembers=\"true\"/>\n" + "    </Hierarchy>\n" + "  </Dimension>\n" + "  <Measure name=\"Store Sqft\" column=\"store_sqft\" aggregator=\"sum\"\n" + "      formatString=\"#,###\"/>\n" + "</Cube>\n";
        cubeDef = cubeDef.replace("@REPLACE_ME@", String.valueOf(testValue));
        final TestContext context = TestContext.instance().create(null, cubeDef, null, null, null, null);
        final Cube cube = context.getConnection().getSchema().lookupCube("Foo", true);
        Dimension dim = null;
        for (Dimension dimCheck : cube.getDimensions()) {
            if (dimCheck.getName().equals("Bar")) {
                dim = dimCheck;
            }
        }
        assertNotNull(dim);
        final Hierarchy hier = dim.getHierarchy();
        assertNotNull(hier);
        assertEquals(MondrianProperties.instance().SsasCompatibleNaming.get() ? "Bacon" : "Bar.Bacon", hier.getName());
        assertTrue(testValue.equals(hier.isVisible()));
    }
}
Also used : Hierarchy(mondrian.olap.Hierarchy) RolapCube(mondrian.rolap.RolapCube)

Example 3 with Hierarchy

use of mondrian.olap.Hierarchy in project mondrian by pentaho.

the class CmdRunner method parseParameter.

// this is taken from JPivot
public Expr parseParameter(String value) {
    // is it a String (enclose in double or single quotes ?
    String trimmed = value.trim();
    int len = trimmed.length();
    if (trimmed.charAt(0) == '"' && trimmed.charAt(len - 1) == '"') {
        debug("parseParameter. STRING_TYPE: " + trimmed);
        return new Expr(trimmed.substring(1, trimmed.length() - 1), Expr.Type.STRING);
    }
    if (trimmed.charAt(0) == '\'' && trimmed.charAt(len - 1) == '\'') {
        debug("parseParameter. STRING_TYPE: " + trimmed);
        return new Expr(trimmed.substring(1, trimmed.length() - 1), Expr.Type.STRING);
    }
    // is it a Number ?
    Number number = null;
    try {
        number = nf.parse(trimmed);
    } catch (ParseException pex) {
    // nothing to do, should be member
    }
    if (number != null) {
        debug("parseParameter. NUMERIC_TYPE: " + number);
        return new Expr(number, Expr.Type.NUMERIC);
    }
    debug("parseParameter. MEMBER_TYPE: " + trimmed);
    Query query = this.connection.parseQuery(this.mdxCmd);
    // dont have to execute
    // this.connection.execute(query);
    // assume member, dimension, hierarchy, level
    OlapElement element = Util.lookup(query, Util.parseIdentifier(trimmed));
    debug("parseParameter. exp=" + ((element == null) ? "null" : element.getClass().getName()));
    if (element instanceof Member) {
        Member member = (Member) element;
        return new Expr(member, Expr.Type.MEMBER);
    } else if (element instanceof mondrian.olap.Level) {
        mondrian.olap.Level level = (mondrian.olap.Level) element;
        return new Expr(level, Expr.Type.MEMBER);
    } else if (element instanceof Hierarchy) {
        Hierarchy hier = (Hierarchy) element;
        return new Expr(hier, Expr.Type.MEMBER);
    } else if (element instanceof Dimension) {
        Dimension dim = (Dimension) element;
        return new Expr(dim, Expr.Type.MEMBER);
    }
    return null;
}
Also used : Hierarchy(mondrian.olap.Hierarchy) mondrian.olap(mondrian.olap) Level(org.apache.log4j.Level) ParseException(java.text.ParseException)

Example 4 with Hierarchy

use of mondrian.olap.Hierarchy in project mondrian by pentaho.

the class MemberCacheControlTest method testFlushHierarchy.

/**
 * Test case for bug
 * <a href="http://jira.pentaho.com/browse/MONDRIAN-1076">MONDRIAN-1076,
 * "Add CacheControl API to flush members from dimension cache"</a>.
 */
public void testFlushHierarchy() {
    final TestContext testContext = getTestContext();
    CacheControlTest.flushCache(testContext);
    final CacheControl cacheControl = testContext.getConnection().getCacheControl(null);
    final Cube salesCube = testContext.getConnection().getSchema().lookupCube("Sales", true);
    final Logger logger = RolapUtil.SQL_LOGGER;
    final Level level = logger.getLevel();
    final StringWriter sw = new StringWriter();
    final WriterAppender appender = new WriterAppender(new SimpleLayout(), sw);
    try {
        logger.setLevel(Level.DEBUG);
        logger.addAppender(appender);
        final Hierarchy storeHierarchy = salesCube.getDimensions()[1].getHierarchies()[0];
        assertEquals("Store", storeHierarchy.getName());
        final CacheControl.MemberSet storeMemberSet = cacheControl.createMemberSet(storeHierarchy.getAllMember(), true);
        final Runnable storeFlusher = new Runnable() {

            public void run() {
                cacheControl.flush(storeMemberSet);
            }
        };
        final Result result = testContext.executeQuery("select [Store].[Mexico].[Yucatan] on 0 from [Sales]");
        final Member storeYucatanMember = result.getAxes()[0].getPositions().get(0).get(0);
        final CacheControl.MemberSet storeYucatanMemberSet = cacheControl.createMemberSet(storeYucatanMember, true);
        final Runnable storeYucatanFlusher = new Runnable() {

            public void run() {
                cacheControl.flush(storeYucatanMemberSet);
            }
        };
        checkFlushHierarchy(sw, true, storeFlusher, new Runnable() {

            public void run() {
                // Check that <Member>.Children uses cache when applied
                // to an 'all' member.
                testContext.assertAxisReturns("[Store].Children", "[Store].[Canada]\n" + "[Store].[Mexico]\n" + "[Store].[USA]");
            }
        });
        checkFlushHierarchy(sw, true, storeFlusher, new Runnable() {

            public void run() {
                // Check that <Member>.Children uses cache when applied
                // to regular member.
                testContext.assertAxisReturns("[Store].[USA].[CA].Children", "[Store].[USA].[CA].[Alameda]\n" + "[Store].[USA].[CA].[Beverly Hills]\n" + "[Store].[USA].[CA].[Los Angeles]\n" + "[Store].[USA].[CA].[San Diego]\n" + "[Store].[USA].[CA].[San Francisco]");
            }
        });
        // In contrast to preceding, flushing Yucatan should not affect
        // California.
        checkFlushHierarchy(sw, false, storeYucatanFlusher, new Runnable() {

            public void run() {
                // Check that <Member>.Children uses cache when applied
                // to regular member.
                testContext.assertAxisReturns("[Store].[USA].[CA].Children", "[Store].[USA].[CA].[Alameda]\n" + "[Store].[USA].[CA].[Beverly Hills]\n" + "[Store].[USA].[CA].[Los Angeles]\n" + "[Store].[USA].[CA].[San Diego]\n" + "[Store].[USA].[CA].[San Francisco]");
            }
        });
        checkFlushHierarchy(sw, true, storeFlusher, new Runnable() {

            public void run() {
                // Check that <Hierarchy>.Members uses cache.
                testContext.assertExprReturns("Count([Store].Members)", "63");
            }
        });
        checkFlushHierarchy(sw, true, storeFlusher, new Runnable() {

            public void run() {
                // Check that <Level>.Members uses cache.
                testContext.assertExprReturns("Count([Store].[Store Name].Members)", "25");
            }
        });
        // Time hierarchy is interesting because it has public 'all' member.
        // But you can still use the private all member for purposes like
        // flushing.
        final Hierarchy timeHierarchy = salesCube.getDimensions()[4].getHierarchies()[0];
        assertEquals("Time", timeHierarchy.getName());
        final CacheControl.MemberSet timeMemberSet = cacheControl.createMemberSet(timeHierarchy.getAllMember(), true);
        final Runnable timeFlusher = new Runnable() {

            public void run() {
                cacheControl.flush(timeMemberSet);
            }
        };
        checkFlushHierarchy(sw, true, timeFlusher, new Runnable() {

            public void run() {
                // Check that <Level>.Members uses cache.
                testContext.assertExprReturns("Count([Time].[Month].Members)", "24");
            }
        });
        checkFlushHierarchy(sw, true, timeFlusher, new Runnable() {

            public void run() {
                // Check that <Level>.Members uses cache.
                testContext.assertAxisReturns("[Time].[1997].[Q2].Children", "[Time].[1997].[Q2].[4]\n" + "[Time].[1997].[Q2].[5]\n" + "[Time].[1997].[Q2].[6]");
            }
        });
    } finally {
        logger.setLevel(level);
        logger.removeAppender(appender);
    }
}
Also used : Hierarchy(mondrian.olap.Hierarchy) StringWriter(java.io.StringWriter) Level(org.apache.log4j.Level)

Example 5 with Hierarchy

use of mondrian.olap.Hierarchy in project mondrian by pentaho.

the class RestrictedMemberReaderTest method testGetHierarchy_allAccess.

public void testGetHierarchy_allAccess() {
    Schema schema = Mockito.mock(Schema.class);
    Dimension dimension = Mockito.mock(Dimension.class);
    RolapHierarchy hierarchy = Mockito.mock(RolapHierarchy.class);
    Level[] hierarchyAccessLevels = new Level[] { null };
    MemberReader delegateMemberReader = Mockito.mock(MemberReader.class);
    HierarchyAccess roleAccess = null;
    Role role = Mockito.mock(Role.class);
    Mockito.doReturn(schema).when(dimension).getSchema();
    Mockito.doReturn(dimension).when(hierarchy).getDimension();
    Mockito.doReturn(hierarchyAccessLevels).when(hierarchy).getLevels();
    Mockito.doReturn(true).when(hierarchy).isRagged();
    Mockito.doReturn(roleAccess).when(role).getAccessDetails(Mockito.any(Hierarchy.class));
    Mockito.doReturn(hierarchy).when(delegateMemberReader).getHierarchy();
    rmr = new RestrictedMemberReader(delegateMemberReader, role);
    Assert.assertSame(hierarchy, rmr.getHierarchy());
}
Also used : Role(mondrian.olap.Role) Hierarchy(mondrian.olap.Hierarchy) Schema(mondrian.olap.Schema) Level(mondrian.olap.Level) Dimension(mondrian.olap.Dimension) HierarchyAccess(mondrian.olap.Role.HierarchyAccess)

Aggregations

Hierarchy (mondrian.olap.Hierarchy)27 Level (mondrian.olap.Level)9 Member (mondrian.olap.Member)8 Role (mondrian.olap.Role)8 HierarchyAccess (mondrian.olap.Role.HierarchyAccess)7 MultiCardinalityDefaultMember (mondrian.rolap.RestrictedMemberReader.MultiCardinalityDefaultMember)6 MondrianDef (mondrian.olap.MondrianDef)5 DataSource (javax.sql.DataSource)4 Dimension (mondrian.olap.Dimension)4 mondrian.olap (mondrian.olap)3 Schema (mondrian.olap.Schema)3 RolapCube (mondrian.rolap.RolapCube)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Axis (mondrian.olap.Axis)2 RelationOrJoin (mondrian.olap.MondrianDef.RelationOrJoin)2 LimitedRollupMember (mondrian.rolap.RolapHierarchy.LimitedRollupMember)2 Level (org.apache.log4j.Level)2