Search in sources :

Example 16 with Query

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();
}
Also used : Query(mondrian.olap.Query) StringWriter(java.io.StringWriter) CalcWriter(mondrian.calc.CalcWriter) OlapConnection(org.olap4j.OlapConnection) Connection(mondrian.olap.Connection) Calc(mondrian.calc.Calc) Exp(mondrian.olap.Exp) PrintWriter(java.io.PrintWriter)

Example 17 with Query

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;
}
Also used : Query(mondrian.olap.Query) OlapConnection(org.olap4j.OlapConnection) Connection(mondrian.olap.Connection) Result(mondrian.olap.Result)

Example 18 with Query

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);
}
Also used : Query(mondrian.olap.Query) OlapConnection(org.olap4j.OlapConnection) Connection(mondrian.olap.Connection) Exp(mondrian.olap.Exp)

Example 19 with Query

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);
    }
}
Also used : SqlQuery(mondrian.rolap.sql.SqlQuery) Query(mondrian.olap.Query) SqlQuery(mondrian.rolap.sql.SqlQuery) TupleConstraint(mondrian.rolap.sql.TupleConstraint) MemberChildrenConstraint(mondrian.rolap.sql.MemberChildrenConstraint) ListTupleList(mondrian.calc.impl.ListTupleList) ArrayList(java.util.ArrayList) UnaryTupleList(mondrian.calc.impl.UnaryTupleList) ArrayTupleList(mondrian.calc.impl.ArrayTupleList) Sorter.hierarchizeTupleList(mondrian.olap.fun.sort.Sorter.hierarchizeTupleList) TupleList(mondrian.calc.TupleList) List(java.util.List) Member(mondrian.olap.Member)

Example 20 with Query

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());
}
Also used : Query(mondrian.olap.Query)

Aggregations

Query (mondrian.olap.Query)26 Connection (mondrian.olap.Connection)17 OlapConnection (org.olap4j.OlapConnection)14 Result (mondrian.olap.Result)9 RolapConnection (mondrian.rolap.RolapConnection)7 Member (mondrian.olap.Member)6 Exp (mondrian.olap.Exp)5 ArrayList (java.util.ArrayList)3 MemberExpr (mondrian.mdx.MemberExpr)3 MondrianException (mondrian.olap.MondrianException)3 SqlQuery (mondrian.rolap.sql.SqlQuery)3 Execution (mondrian.server.Execution)3 HashSet (java.util.HashSet)2 TupleList (mondrian.calc.TupleList)2 ListTupleList (mondrian.calc.impl.ListTupleList)2 Formula (mondrian.olap.Formula)2 Hierarchy (mondrian.olap.Hierarchy)2 QueryAxis (mondrian.olap.QueryAxis)2 TestMember (mondrian.olap.fun.TestMember)2 Dialect (mondrian.spi.Dialect)2