Search in sources :

Example 1 with ScalarType

use of mondrian.olap.type.ScalarType in project mondrian by pentaho.

the class Sorter method evaluateMembers.

/**
 * For each member in a list, evaluates an expression and creates a map from members to values.
 *
 * <p>If the list contains tuples, use
 * {@link #evaluateTuples(mondrian.olap.Evaluator, mondrian.calc.Calc, mondrian.calc.TupleList)}.
 *
 * @param evaluator  Evaluation context
 * @param exp        Expression to evaluate
 * @param memberIter Iterable over the collection of members
 * @param memberList List to be populated with members, or null
 * @param parentsToo If true, evaluate the expression for all ancestors of the members as well exp != null
 *                   exp.getType() instanceof ScalarType
 */
static Map<Member, Object> evaluateMembers(Evaluator evaluator, Calc exp, Iterable<Member> memberIter, List<Member> memberList, boolean parentsToo) {
    final int savepoint = evaluator.savepoint();
    try {
        assert exp.getType() instanceof ScalarType;
        Map<Member, Object> mapMemberToValue = new HashMap<>();
        for (Member member : memberIter) {
            if (memberList != null) {
                memberList.add(member);
            }
            while (true) {
                evaluator.setContext(member);
                Object result = exp.evaluate(evaluator);
                if (result == null) {
                    result = Util.nullValue;
                }
                mapMemberToValue.put(member, result);
                if (!parentsToo) {
                    break;
                }
                member = member.getParentMember();
                if (member == null) {
                    break;
                }
                if (mapMemberToValue.containsKey(member)) {
                    break;
                }
            }
        }
        return mapMemberToValue;
    } finally {
        evaluator.restore(savepoint);
    }
}
Also used : HashMap(java.util.HashMap) ScalarType(mondrian.olap.type.ScalarType) Member(mondrian.olap.Member)

Example 2 with ScalarType

use of mondrian.olap.type.ScalarType in project mondrian by pentaho.

the class FunUtil method evaluateSet.

/**
 * Evaluates {@code exp} (if defined) over {@code members} to generate a {@link List} of {@link SetWrapper} objects,
 * which contains a {@link Double} value and meta information, unlike {@link #evaluateMembers}, which only produces
 * values.
 *
 * @pre exp != null
 */
static SetWrapper evaluateSet(Evaluator evaluator, TupleIterable members, Calc calc) {
    assert members != null;
    assert calc != null;
    assert calc.getType() instanceof ScalarType;
    // todo: treat constant exps as evaluateMembers() does
    SetWrapper retval = new SetWrapper();
    final TupleCursor cursor = members.tupleCursor();
    int currentIteration = 0;
    Execution execution = evaluator.getQuery().getStatement().getCurrentExecution();
    while (cursor.forward()) {
        CancellationChecker.checkCancelOrTimeout(currentIteration++, execution);
        cursor.setContext(evaluator);
        Object o = calc.evaluate(evaluator);
        if (o == null || o == Util.nullValue) {
            retval.nullCount++;
        } else if (o == RolapUtil.valueNotReadyException) {
            // Carry on summing, so that if we are running in a
            // BatchingCellReader, we find out all the dependent cells we
            // need
            retval.errorCount++;
        } else if (o instanceof Number) {
            retval.v.add(((Number) o).doubleValue());
        } else {
            retval.v.add(o);
        }
    }
    return retval;
}
Also used : Execution(mondrian.server.Execution) TupleCursor(mondrian.calc.TupleCursor) ScalarType(mondrian.olap.type.ScalarType)

Example 3 with ScalarType

use of mondrian.olap.type.ScalarType in project mondrian by pentaho.

the class DrilldownLevelTopBottomFunDef method compileCall.

public Calc compileCall(final ResolvedFunCall call, ExpCompiler compiler) {
    // Compile the member list expression. Ask for a mutable list, because
    // we're going to insert members into it later.
    final ListCalc listCalc = compiler.compileList(call.getArg(0), true);
    final IntegerCalc integerCalc = compiler.compileInteger(call.getArg(1));
    final LevelCalc levelCalc = call.getArgCount() > 2 && call.getArg(2).getCategory() != Category.Empty ? compiler.compileLevel(call.getArg(2)) : null;
    final Calc orderCalc = call.getArgCount() > 3 ? compiler.compileScalar(call.getArg(3), true) : new ValueCalc(new DummyExp(new ScalarType()));
    return new AbstractListCalc(call, new Calc[] { listCalc, integerCalc, orderCalc }) {

        public TupleList evaluateList(Evaluator evaluator) {
            // Use a native evaluator, if more efficient.
            // TODO: Figure this out at compile time.
            SchemaReader schemaReader = evaluator.getSchemaReader();
            NativeEvaluator nativeEvaluator = schemaReader.getNativeSetEvaluator(call.getFunDef(), call.getArgs(), evaluator, this);
            if (nativeEvaluator != null) {
                return (TupleList) nativeEvaluator.execute(ResultStyle.LIST);
            }
            TupleList list = listCalc.evaluateList(evaluator);
            int n = integerCalc.evaluateInteger(evaluator);
            if (n == FunUtil.IntegerNull || n <= 0) {
                return list;
            }
            Level level;
            if (levelCalc == null) {
                level = null;
            } else {
                level = levelCalc.evaluateLevel(evaluator);
            }
            List<Member> result = new ArrayList<Member>();
            assert list.getArity() == 1;
            for (Member member : list.slice(0)) {
                result.add(member);
                if (level != null && member.getLevel() != level) {
                    if (level.getDimension() != member.getDimension()) {
                        throw newEvalException(DrilldownLevelTopBottomFunDef.this, "Level '" + level.getUniqueName() + "' not compatible with member '" + member.getUniqueName() + "'");
                    }
                    continue;
                }
                List<Member> children = schemaReader.getMemberChildren(member);
                final int savepoint = evaluator.savepoint();
                List<Member> sortedChildren;
                try {
                    evaluator.setNonEmpty(false);
                    sortedChildren = Sorter.sortMembers(evaluator, children, children, orderCalc, top, true);
                } finally {
                    evaluator.restore(savepoint);
                }
                int x = Math.min(n, sortedChildren.size());
                for (int i = 0; i < x; i++) {
                    result.add(sortedChildren.get(i));
                }
            }
            return new UnaryTupleList(result);
        }

        public boolean dependsOn(Hierarchy hierarchy) {
            return anyDependsButFirst(getCalcs(), hierarchy);
        }
    };
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) SchemaReader(mondrian.olap.SchemaReader) ScalarType(mondrian.olap.type.ScalarType) ArrayList(java.util.ArrayList) AbstractListCalc(mondrian.calc.impl.AbstractListCalc) Calc(mondrian.calc.Calc) LevelCalc(mondrian.calc.LevelCalc) IntegerCalc(mondrian.calc.IntegerCalc) AbstractListCalc(mondrian.calc.impl.AbstractListCalc) ListCalc(mondrian.calc.ListCalc) ValueCalc(mondrian.calc.impl.ValueCalc) LevelCalc(mondrian.calc.LevelCalc) Evaluator(mondrian.olap.Evaluator) NativeEvaluator(mondrian.olap.NativeEvaluator) UnaryTupleList(mondrian.calc.impl.UnaryTupleList) TupleList(mondrian.calc.TupleList) Hierarchy(mondrian.olap.Hierarchy) UnaryTupleList(mondrian.calc.impl.UnaryTupleList) IntegerCalc(mondrian.calc.IntegerCalc) AbstractListCalc(mondrian.calc.impl.AbstractListCalc) ListCalc(mondrian.calc.ListCalc) DummyExp(mondrian.calc.DummyExp) Level(mondrian.olap.Level) Member(mondrian.olap.Member) NativeEvaluator(mondrian.olap.NativeEvaluator)

Aggregations

ScalarType (mondrian.olap.type.ScalarType)3 Member (mondrian.olap.Member)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Calc (mondrian.calc.Calc)1 DummyExp (mondrian.calc.DummyExp)1 IntegerCalc (mondrian.calc.IntegerCalc)1 LevelCalc (mondrian.calc.LevelCalc)1 ListCalc (mondrian.calc.ListCalc)1 TupleCursor (mondrian.calc.TupleCursor)1 TupleList (mondrian.calc.TupleList)1 AbstractListCalc (mondrian.calc.impl.AbstractListCalc)1 UnaryTupleList (mondrian.calc.impl.UnaryTupleList)1 ValueCalc (mondrian.calc.impl.ValueCalc)1 Evaluator (mondrian.olap.Evaluator)1 Hierarchy (mondrian.olap.Hierarchy)1 Level (mondrian.olap.Level)1 NativeEvaluator (mondrian.olap.NativeEvaluator)1 SchemaReader (mondrian.olap.SchemaReader)1 Execution (mondrian.server.Execution)1