Search in sources :

Example 1 with Cell

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

the class MondrianHelper method createRectangularOutput.

/**
 * Outputs one row per tuple on the rows axis.
 *
 * @throws KettleDatabaseException
 *           in case some or other error occurs
 */
public void createRectangularOutput() throws KettleDatabaseException {
    final Axis[] axes = result.getAxes();
    if (axes.length != 2) {
        throw new KettleDatabaseException(BaseMessages.getString(PKG, "MondrianInputErrorOnlyTabular"));
    }
    headings = new ArrayList<>();
    rows = new ArrayList<>();
    final Axis rowsAxis = axes[1];
    final Axis columnsAxis = axes[0];
    int rowOrdinal = -1;
    int[] coords = { 0, 0 };
    for (Position rowPos : rowsAxis.getPositions()) {
        ++rowOrdinal;
        coords[1] = rowOrdinal;
        if (rowOrdinal == 0) {
            // First headings are for the members on the rows axis.
            for (Member rowMember : rowPos) {
                headings.add(rowMember.getHierarchy().getUniqueName());
            }
            // concatenate the unique names.
            for (Position columnPos : columnsAxis.getPositions()) {
                String heading = "";
                for (Member columnMember : columnPos) {
                    if (!heading.equals("")) {
                        heading += ", ";
                    }
                    heading += columnMember.getUniqueName();
                }
                headings.add(heading);
            }
        }
        List<Object> rowValues = new ArrayList<>();
        // The first row values describe the members on the rows axis.
        for (Member rowMember : rowPos) {
            rowValues.add(rowMember.getUniqueName());
        }
        // NOTE: Could also output all properties of each cell.
        for (int columnOrdinal = 0; columnOrdinal < columnsAxis.getPositions().size(); ++columnOrdinal) {
            coords[0] = columnOrdinal;
            final Cell cell = result.getCell(coords);
            rowValues.add(cell.getValue());
        }
        rows.add(rowValues);
    }
    outputRowMeta = new RowMeta();
    // column, keep scanning until we find one line that has an actual value
    if (rows.size() > 0) {
        int columnCount = rows.get(0).size();
        HashMap<Integer, ValueMetaInterface> valueMetaHash = new HashMap<>();
        for (int i = 0; i < rows.size(); i++) {
            List<Object> rowValues = rows.get(i);
            for (int c = 0; c < rowValues.size(); c++) {
                if (valueMetaHash.containsKey(new Integer(c))) {
                    // we have this value already
                    continue;
                }
                Object valueData = rowValues.get(c);
                if (valueData == null) {
                    // skip this row and look for the metadata in a new one
                    continue;
                }
                String valueName = headings.get(c);
                ValueMetaInterface valueMeta;
                if (valueData instanceof String) {
                    valueMeta = new ValueMetaString(valueName);
                } else if (valueData instanceof Date) {
                    valueMeta = new ValueMetaDate(valueName);
                } else if (valueData instanceof Boolean) {
                    valueMeta = new ValueMetaBoolean(valueName);
                } else if (valueData instanceof Integer) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Integer) valueData).longValue());
                } else if (valueData instanceof Short) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Short) valueData).longValue());
                } else if (valueData instanceof Byte) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Byte) valueData).longValue());
                } else if (valueData instanceof Long) {
                    valueMeta = new ValueMetaInteger(valueName);
                } else if (valueData instanceof Double) {
                    valueMeta = new ValueMetaNumber(valueName);
                } else if (valueData instanceof Float) {
                    valueMeta = new ValueMetaNumber(valueName);
                    valueData = Double.valueOf(((Float) valueData).doubleValue());
                } else if (valueData instanceof BigDecimal) {
                    valueMeta = new ValueMetaBigNumber(valueName);
                } else {
                    throw new KettleDatabaseException(BaseMessages.getString(PKG, "MondrianInputErrorUnhandledType", valueData.getClass().toString()));
                }
                valueMetaHash.put(c, valueMeta);
            }
            if (valueMetaHash.size() == columnCount) {
                // we're done
                break;
            }
        }
        // Build the list of valueMetas
        List<ValueMetaInterface> valueMetaList = new ArrayList<>();
        for (int c = 0; c < columnCount; c++) {
            if (valueMetaHash.containsKey(new Integer(c))) {
                valueMetaList.add(valueMetaHash.get(new Integer(c)));
            } else {
                // If the entire column is null, assume the missing data as String.
                // Irrelevant, anyway
                ValueMetaInterface valueMeta = new ValueMetaString(headings.get(c));
                valueMetaList.add(valueMeta);
            }
        }
        outputRowMeta.setValueMetaList(valueMetaList);
    }
    // Now that we painstakingly found the meta data 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) HashMap(java.util.HashMap) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ArrayList(java.util.ArrayList) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) Member(mondrian.olap.Member) Cell(mondrian.olap.Cell) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) Axis(mondrian.olap.Axis) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) 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) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 2 with Cell

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

the class FunctionTest method testPropertyInCalculatedMember.

public void testPropertyInCalculatedMember() {
    Result result = executeQuery("WITH MEMBER [Measures].[Store Sales per Sqft]\n" + "AS '[Measures].[Store Sales] / " + "  [Store].CurrentMember.Properties(\"Store Sqft\")'\n" + "SELECT \n" + "  {[Measures].[Unit Sales], [Measures].[Store Sales per Sqft]} ON COLUMNS,\n" + "  {[Store].[Store Name].members} ON ROWS\n" + "FROM Sales");
    Member member;
    Cell cell;
    member = result.getAxes()[1].getPositions().get(18).get(0);
    Assert.assertEquals("[Store].[USA].[WA].[Bellingham].[Store 2]", member.getUniqueName());
    cell = result.getCell(new int[] { 0, 18 });
    Assert.assertEquals("2,237", cell.getFormattedValue());
    cell = result.getCell(new int[] { 1, 18 });
    Assert.assertEquals(".17", cell.getFormattedValue());
    member = result.getAxes()[1].getPositions().get(3).get(0);
    Assert.assertEquals("[Store].[Mexico].[DF].[San Andres].[Store 21]", member.getUniqueName());
    cell = result.getCell(new int[] { 0, 3 });
    Assert.assertEquals("", cell.getFormattedValue());
    cell = result.getCell(new int[] { 1, 3 });
    Assert.assertEquals("", cell.getFormattedValue());
}
Also used : Member(mondrian.olap.Member) Cell(mondrian.olap.Cell) Result(mondrian.olap.Result)

Example 3 with Cell

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

the class FunctionTest method testOrdinal.

public void testOrdinal() {
    final TestContext testContext = getTestContext().withCube("Sales Ragged");
    Cell cell = testContext.executeExprRaw("[Store].[All Stores].[Vatican].ordinal");
    assertEquals("Vatican is at level 1.", 1, ((Number) cell.getValue()).intValue());
    cell = testContext.executeExprRaw("[Store].[All Stores].[USA].[Washington].ordinal");
    assertEquals("Washington is at level 3.", 3, ((Number) cell.getValue()).intValue());
}
Also used : TestContext(mondrian.test.TestContext) Cell(mondrian.olap.Cell)

Example 4 with Cell

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

the class FunctionTest method testFilterWithSlicer.

/**
 * Make sure that slicer is in force when expression is applied on axis, E.g. select filter([Customers].members, [Unit
 * Sales] > 100) from sales where ([Time].[1998])
 */
public void testFilterWithSlicer() {
    Result result = executeQuery("select {[Measures].[Unit Sales]} on columns,\n" + " filter([Customers].[USA].children,\n" + "        [Measures].[Unit Sales] > 20000) on rows\n" + "from Sales\n" + "where ([Time].[1997].[Q1])");
    Axis rows = result.getAxes()[1];
    // if slicer were ignored, there would be 3 rows
    Assert.assertEquals(1, rows.getPositions().size());
    Cell cell = result.getCell(new int[] { 0, 0 });
    Assert.assertEquals("30,114", cell.getFormattedValue());
}
Also used : Cell(mondrian.olap.Cell) Axis(mondrian.olap.Axis) Result(mondrian.olap.Result)

Example 5 with Cell

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

the class TestContext method assertResultValid.

/**
 * Checks that a {@link Result} is valid.
 *
 * @param result Query result
 */
private void assertResultValid(Result result) {
    for (Cell cell : cellIter(result)) {
        final Object value = cell.getValue();
        // Check that the dummy value used to represent null cells never
        // leaks into the outside world.
        Assert.assertNotSame(value, Util.nullValue);
        Assert.assertFalse(value instanceof Number && ((Number) value).doubleValue() == FunUtil.DoubleNull);
        // Similarly empty values.
        Assert.assertNotSame(value, Util.EmptyValue);
        Assert.assertFalse(value instanceof Number && ((Number) value).doubleValue() == FunUtil.DoubleEmpty);
        // Cells should be null if and only if they are null or empty.
        if (cell.getValue() == null) {
            Assert.assertTrue(cell.isNull());
        } else {
            Assert.assertFalse(cell.isNull());
        }
    }
    // There should be no null members.
    for (Axis axis : result.getAxes()) {
        for (Position position : axis.getPositions()) {
            for (Member member : position) {
                Assert.assertNotNull(member);
            }
        }
    }
}
Also used : Position(mondrian.olap.Position) Cell(mondrian.olap.Cell) Member(mondrian.olap.Member) Axis(mondrian.olap.Axis) CellSetAxis(org.olap4j.CellSetAxis)

Aggregations

Cell (mondrian.olap.Cell)20 Result (mondrian.olap.Result)9 Axis (mondrian.olap.Axis)8 Member (mondrian.olap.Member)5 Position (mondrian.olap.Position)3 CellSetAxis (org.olap4j.CellSetAxis)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 CoordinateIterator (org.olap4j.impl.CoordinateIterator)2 BigDecimal (java.math.BigDecimal)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Connection (mondrian.olap.Connection)1 Dimension (mondrian.olap.Dimension)1 Hierarchy (mondrian.olap.Hierarchy)1 Query (mondrian.olap.Query)1 NonEmptyResult (mondrian.rolap.RolapConnection.NonEmptyResult)1 Dialect (mondrian.spi.Dialect)1 TestContext (mondrian.test.TestContext)1