use of mondrian.olap.Query in project mondrian by pentaho.
the class TestContext method compileExpression.
/**
* Compiles a scalar expression in the context of the default cube.
*
* @param expression The expression to evaluate
* @param scalar Whether the expression is scalar
* @return String form of the program
*/
public String compileExpression(String expression, final boolean scalar) {
String cubeName = getDefaultCubeName();
if (cubeName.indexOf(' ') >= 0) {
cubeName = Util.quoteMdxIdentifier(cubeName);
}
final String queryString;
if (scalar) {
queryString = "with member [Measures].[Foo] as " + Util.singleQuoteString(expression) + " select {[Measures].[Foo]} on columns from " + cubeName;
} else {
queryString = "SELECT {" + expression + "} ON COLUMNS FROM " + cubeName;
}
Connection connection = getConnection();
Query query = connection.parseQuery(queryString);
final Exp exp;
if (scalar) {
exp = query.getFormulas()[0].getExpression();
} else {
exp = query.getAxes()[0].getSet();
}
final Calc calc = query.compileExpression(exp, scalar, null);
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final CalcWriter calcWriter = new CalcWriter(pw, false);
calc.accept(calcWriter);
pw.flush();
return sw.toString();
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class TestContext method executeQuery.
/**
* Executes a query.
*
* @param queryString Query string
*/
public Result executeQuery(String queryString) {
Connection connection = getConnection();
queryString = upgradeQuery(queryString);
Query query = connection.parseQuery(queryString);
final Result result = connection.execute(query);
// switch to enable this, but it will do for now.
if (MondrianProperties.instance().TestExpDependencies.booleanValue()) {
assertResultValid(result);
}
return result;
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class TestContext method assertSetExprDependsOn.
/**
* Asserts that an MDX set-valued expression depends upon a given list of dimensions.
*/
public void assertSetExprDependsOn(String expr, String dimList) {
// Construct a query, and mine it for a parsed expression.
// Use a fresh connection, because some tests define their own dims.
final Connection connection = getConnection();
final String queryString = "SELECT {" + expr + "} ON COLUMNS FROM [Sales]";
final Query query = connection.parseQuery(queryString);
query.resolve();
final Exp expression = query.getAxes()[0].getSet();
// Build a list of the dimensions which the expression depends upon,
// and check that it is as expected.
checkDependsOn(query, expression, dimList, false);
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class SqlTupleReader method makeLevelMembersSql.
Pair<String, List<SqlStatement.Type>> makeLevelMembersSql(DataSource dataSource, List<TargetBase> targetGroup) {
// In the case of a virtual cube, if we need to join to the fact
// table, we do not necessarily have a single underlying fact table,
// as the underlying base cubes in the virtual cube may all reference
// different fact tables.
//
// Therefore, we need to gather the underlying fact tables by going
// through the list of measures referenced in the query. And then
// we generate one sub-select per fact table, joining against each
// underlying fact table, unioning the sub-selects.
RolapCube cube = null;
boolean virtualCube = false;
if (constraint instanceof SqlContextConstraint) {
SqlContextConstraint sqlConstraint = (SqlContextConstraint) constraint;
Query query = constraint.getEvaluator().getQuery();
cube = (RolapCube) query.getCube();
if (sqlConstraint.isJoinRequired()) {
virtualCube = cube.isVirtual();
}
}
if (virtualCube) {
Query query = constraint.getEvaluator().getQuery();
// Make fact table appear in fixed sequence
final Collection<RolapCube> baseCubes = getBaseCubeCollection(query);
Collection<RolapCube> fullyJoiningBaseCubes = getFullyJoiningBaseCubes(baseCubes, targetGroup);
if (fullyJoiningBaseCubes.size() == 0) {
return sqlForEmptyTuple(dataSource, baseCubes);
}
// generate sub-selects, each one joining with one of
// the fact table referenced
String prependString = "";
final StringBuilder selectString = new StringBuilder();
List<SqlStatement.Type> types = null;
final int savepoint = getEvaluator(constraint).savepoint();
SqlQuery unionQuery = SqlQuery.newQuery(dataSource, "");
try {
for (RolapCube baseCube : fullyJoiningBaseCubes) {
// Use the measure from the corresponding base cube in the
// context to find the correct join path to the base fact
// table.
//
// The first non-calculated measure is fine since the
// constraint logic only uses it
// to find the correct fact table to join to.
Member measureInCurrentbaseCube = null;
for (Member currMember : baseCube.getMeasures()) {
if (!currMember.isCalculated()) {
measureInCurrentbaseCube = currMember;
break;
}
}
if (measureInCurrentbaseCube == null) {
// the fact table.
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("No non-calculated member found in cube " + baseCube.getName());
}
measureInCurrentbaseCube = baseCube.getMeasures().get(0);
}
// Force the constraint evaluator's measure
// to the one in the base cube.
getEvaluator(constraint).setContext(measureInCurrentbaseCube);
selectString.append(prependString);
// Generate the select statement for the current base cube.
// Make sure to pass WhichSelect.NOT_LAST if there are more
// than one base cube and it isn't the last one so that
// the order by clause is not added to unionized queries
// (that would be illegal SQL)
final Pair<String, List<SqlStatement.Type>> pair = generateSelectForLevels(dataSource, baseCube, fullyJoiningBaseCubes.size() == 1 ? WhichSelect.ONLY : WhichSelect.NOT_LAST, targetGroup);
selectString.append(pair.left);
types = pair.right;
prependString = MondrianProperties.instance().GenerateFormattedSql.get() ? Util.nl + UNION + Util.nl : " " + UNION + " ";
}
} finally {
// Restore the original measure member
getEvaluator(constraint).restore(savepoint);
}
if (fullyJoiningBaseCubes.size() == 1) {
// the original one.
return Pair.of(selectString.toString(), types);
} else {
// Add the subquery to the wrapper query.
unionQuery.addFromQuery(selectString.toString(), "unionQuery", true);
// Dont forget to select all columns.
unionQuery.addSelect("*", null, null);
// not column name strings or expressions.
if (fullyJoiningBaseCubes.size() > 1) {
for (int i = 0; i < types.size(); i++) {
unionQuery.addOrderBy(i + 1 + "", null, true, false, // has lost its meaning in the process.
unionQuery.getDialect().requiresUnionOrderByOrdinal(), true);
}
}
return Pair.of(unionQuery.toSqlAndTypes().left, types);
}
} else {
// cubes.
return generateSelectForLevels(dataSource, cube, WhichSelect.ONLY, targetGroup);
}
}
use of mondrian.olap.Query in project mondrian by pentaho.
the class NonEmptyPropertyForAllAxisTest method testSlicerAxisDoesNotGetNonEmptyApplied.
public void testSlicerAxisDoesNotGetNonEmptyApplied() {
propSaver.set(MondrianProperties.instance().EnableNonEmptyOnAllAxis, true);
String mdxQuery = "select from [Sales]\n" + "where [Time].[1997]\n";
Query query = getConnection().parseQuery(mdxQuery);
TestContext.assertEqualsVerbose(mdxQuery, query.toString());
}
Aggregations