Search in sources :

Example 16 with Cell

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

the class TestContext method assertExprReturns.

/**
 * Executes an expression and asserts that it returns a given result.
 */
public void assertExprReturns(String expression, String expected) {
    final Cell cell = executeExprRaw(expression);
    if (expected == null) {
        // null values are formatted as empty string
        expected = "";
    }
    assertEqualsVerbose(expected, cell.getFormattedValue());
}
Also used : Cell(mondrian.olap.Cell)

Example 17 with Cell

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

the class TestContext method cellIter.

/**
 * Returns an iterator over cells in a result.
 */
static Iterable<Cell> cellIter(final Result result) {
    return new Iterable<Cell>() {

        public Iterator<Cell> iterator() {
            int[] axisDimensions = new int[result.getAxes().length];
            int k = 0;
            for (Axis axis : result.getAxes()) {
                axisDimensions[k++] = axis.getPositions().size();
            }
            final CoordinateIterator coordIter = new CoordinateIterator(axisDimensions);
            return new Iterator<Cell>() {

                public boolean hasNext() {
                    return coordIter.hasNext();
                }

                public Cell next() {
                    final int[] ints = coordIter.next();
                    return result.getCell(ints);
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : CoordinateIterator(org.olap4j.impl.CoordinateIterator) CoordinateIterator(org.olap4j.impl.CoordinateIterator) Iterator(java.util.Iterator) Cell(mondrian.olap.Cell) Axis(mondrian.olap.Axis) CellSetAxis(org.olap4j.CellSetAxis)

Example 18 with Cell

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

the class UdfTest method testScriptUdfInvalid.

/**
 * Unit test that we get a nice error if a script UDF contains an error.
 */
public void testScriptUdfInvalid() {
    TestContext tc = udfTestContext("<UserDefinedFunction name='Factorial'>\n" + "  <Script language='JavaScript'><![CDATA[\n" + "    function getParameterTypes() {\n" + "      return new Array(\n" + "        new mondrian.olap.type.NumericType());\n" + "    }\n" + "    function getReturnType(parameterTypes) {\n" + "      return new mondrian.olap.type.NumericType();\n" + "    }\n" + "    function execute(evaluator, arguments) {\n" + "      var n = arguments[0].evaluateScalar(evaluator);\n" + "      return factorial(n);\n" + "    }\n" + "    function factorial(n) {\n" + "      return n <= 1 ? 1 : n * factorial_xx(n - 1);\n" + "    }\n" + "  ]]>\n" + "  </Script>\n" + "</UserDefinedFunction>\n");
    final Cell cell = tc.executeExprRaw("Factorial(4 + 2)");
    getTestContext().assertMatchesVerbose(Pattern.compile("(?s).*ReferenceError: \"factorial_xx\" is not defined..*"), cell.getValue().toString());
}
Also used : Cell(mondrian.olap.Cell)

Example 19 with Cell

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

the class BasicQueryTest method testGetContext.

/**
 * Unit test for the {@link Cell#getContextMember(mondrian.olap.Hierarchy)} method.
 */
public void testGetContext() {
    if (!MondrianProperties.instance().SsasCompatibleNaming.get()) {
        return;
    }
    Result result = getTestContext().executeQuery("select [Gender].Members on 0,\n" + "[Time].[Weekly].[1997].[6].Children on 1\n" + "from [Sales]\n" + "where [Marital Status].[S]");
    final Cell cell = result.getCell(new int[] { 0, 0 });
    final Map<String, Hierarchy> hierarchyMap = new HashMap<String, Hierarchy>();
    for (Dimension dimension : result.getQuery().getCube().getDimensions()) {
        for (Hierarchy hierarchy : dimension.getHierarchies()) {
            hierarchyMap.put(hierarchy.getUniqueName(), hierarchy);
        }
    }
    assertEquals("[Measures].[Unit Sales]", cell.getContextMember(hierarchyMap.get("[Measures]")).getUniqueName());
    assertEquals("[Time].[1997]", cell.getContextMember(hierarchyMap.get("[Time]")).getUniqueName());
    assertEquals("[Time].[Weekly].[1997].[6].[1]", cell.getContextMember(hierarchyMap.get("[Time].[Weekly]")).getUniqueName());
    assertEquals("[Gender].[All Gender]", cell.getContextMember(hierarchyMap.get("[Gender]")).getUniqueName());
    assertEquals("[Marital Status].[S]", cell.getContextMember(hierarchyMap.get("[Marital Status]")).getUniqueName());
}
Also used : Hierarchy(mondrian.olap.Hierarchy) HashMap(java.util.HashMap) Dimension(mondrian.olap.Dimension) Cell(mondrian.olap.Cell) Result(mondrian.olap.Result)

Example 20 with Cell

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

the class MondrianHelper method outputFlattenedRecurse.

private static void outputFlattenedRecurse(Result result, List<List<Object>> rows, List<Object> rowValues, int[] coords, int axisOrdinal) {
    final Axis[] axes = result.getAxes();
    if (axisOrdinal == axes.length) {
        final Cell cell = result.getCell(coords);
        // Output the raw (unformatted) value of the cell.
        // NOTE: We could output other properties of the cell here, such as its
        // formatted value, too.
        rowValues.add(cell.getValue());
        // Add a copy of the completed row to the list of rows.
        rows.add(new ArrayList<>(rowValues));
    } else {
        final Axis axis = axes[axisOrdinal];
        int k = -1;
        int saveLength = rowValues.size();
        for (Position position : axis.getPositions()) {
            coords[axisOrdinal] = ++k;
            for (Member member : position) {
                rowValues.add(member.getUniqueName());
            }
            outputFlattenedRecurse(result, rows, rowValues, coords, axisOrdinal + 1);
            while (rowValues.size() > saveLength) {
                rowValues.remove(rowValues.size() - 1);
            }
        }
    }
}
Also used : Position(mondrian.olap.Position) Cell(mondrian.olap.Cell) Member(mondrian.olap.Member) Axis(mondrian.olap.Axis)

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