Search in sources :

Example 1 with Position

use of mondrian.olap.Position 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 Position

use of mondrian.olap.Position 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 3 with Position

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

the class FunctionTest method testVisualTotalsLevel.

public void testVisualTotalsLevel() {
    Result result = getTestContext().executeQuery("select {[Measures].[Unit Sales]} on columns,\n" + "{[Product].[All Products],\n" + " [Product].[All Products].[Food].[Baked Goods].[Bread],\n" + " VisualTotals(\n" + "    {[Product].[All Products].[Food].[Baked Goods].[Bread],\n" + "     [Product].[All Products].[Food].[Baked Goods].[Bread].[Bagels],\n" + "     [Product].[All Products].[Food].[Baked Goods].[Bread].[Muffins]},\n" + "     \"**Subtotal - *\")} on rows\n" + "from [Sales]");
    final List<Position> rowPos = result.getAxes()[1].getPositions();
    final Member member0 = rowPos.get(0).get(0);
    assertEquals("All Products", member0.getName());
    assertEquals("(All)", member0.getLevel().getName());
    final Member member1 = rowPos.get(1).get(0);
    assertEquals("Bread", member1.getName());
    assertEquals("Product Category", member1.getLevel().getName());
    final Member member2 = rowPos.get(2).get(0);
    assertEquals("*Subtotal - Bread", member2.getName());
    assertEquals("Product Category", member2.getLevel().getName());
    final Member member3 = rowPos.get(3).get(0);
    assertEquals("Bagels", member3.getName());
    assertEquals("Product Subcategory", member3.getLevel().getName());
    final Member member4 = rowPos.get(4).get(0);
    assertEquals("Muffins", member4.getName());
    assertEquals("Product Subcategory", member4.getLevel().getName());
}
Also used : Position(mondrian.olap.Position) Member(mondrian.olap.Member) Result(mondrian.olap.Result)

Example 4 with Position

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

the class FunctionTest method testVisualTotalsAll.

/**
 * Test case for bug
 * <a href="http://jira.pentaho.com/browse/MONDRIAN-615">MONDRIAN-615</a>,
 * "VisualTotals doesn't work for the all member".
 */
public void testVisualTotalsAll() {
    final String query = "SELECT \n" + "  {[Measures].[Unit Sales]} ON 0, \n" + "  VisualTotals(\n" + "    {[Customers].[All Customers],\n" + "     [Customers].[USA],\n" + "     [Customers].[USA].[CA],\n" + "     [Customers].[USA].[OR]}) ON 1\n" + "FROM [Sales]";
    assertQueryReturns(query, "Axis #0:\n" + "{}\n" + "Axis #1:\n" + "{[Measures].[Unit Sales]}\n" + "Axis #2:\n" + "{[Customers].[All Customers]}\n" + "{[Customers].[USA]}\n" + "{[Customers].[USA].[CA]}\n" + "{[Customers].[USA].[OR]}\n" + "Row #0: 142,407\n" + "Row #1: 142,407\n" + "Row #2: 74,748\n" + "Row #3: 67,659\n");
    // Check captions
    final Result result = getTestContext().executeQuery(query);
    final List<Position> positionList = result.getAxes()[1].getPositions();
    assertEquals("All Customers", positionList.get(0).get(0).getCaption());
    assertEquals("USA", positionList.get(1).get(0).getCaption());
    assertEquals("CA", positionList.get(2).get(0).getCaption());
}
Also used : Position(mondrian.olap.Position) Result(mondrian.olap.Result)

Example 5 with Position

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

the class VirtualCubeTest method assertVisibility.

private void assertVisibility(Result result, int ordinal, String expectedName, boolean expectedVisibility) {
    List<Position> columnPositions = result.getAxes()[0].getPositions();
    Member measure = columnPositions.get(ordinal).get(0);
    assertEquals(expectedName, measure.getName());
    assertEquals(expectedVisibility, measure.getPropertyValue(Property.VISIBLE.name));
}
Also used : Position(mondrian.olap.Position) Member(mondrian.olap.Member)

Aggregations

Position (mondrian.olap.Position)17 Member (mondrian.olap.Member)13 Axis (mondrian.olap.Axis)10 Result (mondrian.olap.Result)8 Cell (mondrian.olap.Cell)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CellSetAxis (org.olap4j.CellSetAxis)2 DBCacheEntry (org.pentaho.di.core.DBCacheEntry)2 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 RowMeta (org.pentaho.di.core.row.RowMeta)2 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)2 ValueMetaBoolean (org.pentaho.di.core.row.value.ValueMetaBoolean)2 ValueMetaDate (org.pentaho.di.core.row.value.ValueMetaDate)2 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)2 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)2 HashMap (java.util.HashMap)1 TupleList (mondrian.calc.TupleList)1 Hierarchy (mondrian.olap.Hierarchy)1