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);
}
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());
}
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());
}
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());
}
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);
}
}
}
}
Aggregations