Search in sources :

Example 1 with ValueCalc

use of mondrian.calc.impl.ValueCalc in project mondrian by pentaho.

the class OrderFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final IterCalc listCalc = compiler.compileIter(call.getArg(0));
    List<SortKeySpec> keySpecList = new ArrayList<>();
    buildKeySpecList(keySpecList, call, compiler);
    final int keySpecCount = keySpecList.size();
    // +1 for the listCalc
    Calc[] calcList = new Calc[keySpecCount + 1];
    calcList[0] = listCalc;
    assert keySpecCount >= 1;
    final Calc expCalc = keySpecList.get(0).getKey();
    calcList[1] = expCalc;
    if (keySpecCount == 1) {
        if (expCalc.isWrapperFor(MemberValueCalc.class) || expCalc.isWrapperFor(MemberArrayValueCalc.class)) {
            List<MemberCalc> constantList = new ArrayList<>();
            List<MemberCalc> variableList = new ArrayList<>();
            final MemberCalc[] calcs = (MemberCalc[]) ((AbstractCalc) expCalc).getCalcs();
            for (MemberCalc memberCalc : calcs) {
                if (memberCalc.isWrapperFor(ConstantCalc.class) && !listCalc.dependsOn(memberCalc.getType().getHierarchy())) {
                    constantList.add(memberCalc);
                } else {
                    variableList.add(memberCalc);
                }
            }
            if (constantList.isEmpty()) {
            // All members are non-constant -- cannot optimize
            } else if (variableList.isEmpty()) {
                // All members are constant. Optimize by setting entire
                // context first.
                calcList[1] = new ValueCalc(new DummyExp(expCalc.getType()));
                return new ContextCalc(calcs, new CalcImpl(call, calcList, keySpecList));
            } else {
                // Some members are constant. Evaluate these before
                // evaluating the list expression.
                calcList[1] = MemberValueCalc.create(new DummyExp(expCalc.getType()), variableList.toArray(new MemberCalc[variableList.size()]), compiler.getEvaluator().mightReturnNullForUnrelatedDimension());
                return new ContextCalc(constantList.toArray(new MemberCalc[constantList.size()]), new CalcImpl(call, calcList, keySpecList));
            }
        }
    }
    for (int i = 1; i < keySpecCount; i++) {
        final Calc expCalcs = keySpecList.get(i).getKey();
        calcList[i + 1] = expCalcs;
    }
    return new CalcImpl(call, calcList, keySpecList);
}
Also used : MemberArrayValueCalc(mondrian.calc.impl.MemberArrayValueCalc) MemberValueCalc(mondrian.calc.impl.MemberValueCalc) ValueCalc(mondrian.calc.impl.ValueCalc) SortKeySpec(mondrian.olap.fun.sort.SortKeySpec) IterCalc(mondrian.calc.IterCalc) GenericIterCalc(mondrian.calc.impl.GenericIterCalc) ArrayList(java.util.ArrayList) ConstantCalc(mondrian.calc.impl.ConstantCalc) MemberArrayValueCalc(mondrian.calc.impl.MemberArrayValueCalc) Calc(mondrian.calc.Calc) ConstantCalc(mondrian.calc.impl.ConstantCalc) IterCalc(mondrian.calc.IterCalc) MemberCalc(mondrian.calc.MemberCalc) AbstractListCalc(mondrian.calc.impl.AbstractListCalc) GenericIterCalc(mondrian.calc.impl.GenericIterCalc) MemberValueCalc(mondrian.calc.impl.MemberValueCalc) ValueCalc(mondrian.calc.impl.ValueCalc) AbstractCalc(mondrian.calc.impl.AbstractCalc) MemberArrayValueCalc(mondrian.calc.impl.MemberArrayValueCalc) MemberValueCalc(mondrian.calc.impl.MemberValueCalc) DummyExp(mondrian.calc.DummyExp) MemberCalc(mondrian.calc.MemberCalc)

Example 2 with ValueCalc

use of mondrian.calc.impl.ValueCalc in project mondrian by pentaho.

the class VarPFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final ListCalc listCalc = compiler.compileList(call.getArg(0));
    final Calc calc = call.getArgCount() > 1 ? compiler.compileScalar(call.getArg(1), true) : new ValueCalc(call);
    return new AbstractDoubleCalc(call, new Calc[] { listCalc, calc }) {

        public double evaluateDouble(Evaluator evaluator) {
            TupleList memberList = evaluateCurrentList(listCalc, evaluator);
            final int savepoint = evaluator.savepoint();
            try {
                evaluator.setNonEmpty(false);
                return (Double) var(evaluator, memberList, calc, true);
            } finally {
                evaluator.restore(savepoint);
            }
        }

        public boolean dependsOn(Hierarchy hierarchy) {
            return anyDependsButFirst(getCalcs(), hierarchy);
        }
    };
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc) ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc)

Example 3 with ValueCalc

use of mondrian.calc.impl.ValueCalc in project mondrian by pentaho.

the class MedianFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final ListCalc listCalc = compiler.compileList(call.getArg(0));
    final Calc calc = call.getArgCount() > 1 ? compiler.compileScalar(call.getArg(1), true) : new ValueCalc(call);
    return new AbstractDoubleCalc(call, new Calc[] { listCalc, calc }) {

        public double evaluateDouble(Evaluator evaluator) {
            final int savepoint = evaluator.savepoint();
            try {
                evaluator.setNonEmpty(false);
                TupleList list = evaluateCurrentList(listCalc, evaluator);
                final double percentile = percentile(evaluator, list, calc, 0.5);
                return percentile;
            } finally {
                evaluator.restore(savepoint);
            }
        }

        public boolean dependsOn(Hierarchy hierarchy) {
            return anyDependsButFirst(getCalcs(), hierarchy);
        }
    };
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc) ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc)

Example 4 with ValueCalc

use of mondrian.calc.impl.ValueCalc in project mondrian by pentaho.

the class VarFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final ListCalc listCalc = compiler.compileList(call.getArg(0));
    final Calc calc = call.getArgCount() > 1 ? compiler.compileScalar(call.getArg(1), true) : new ValueCalc(call);
    return new AbstractDoubleCalc(call, new Calc[] { listCalc, calc }) {

        public double evaluateDouble(Evaluator evaluator) {
            final int savepoint = evaluator.savepoint();
            try {
                evaluator.setNonEmpty(false);
                TupleList list = evaluateCurrentList(listCalc, evaluator);
                final double var = (Double) var(evaluator, list, calc, false);
                return var;
            } finally {
                evaluator.restore(savepoint);
            }
        }

        public boolean dependsOn(Hierarchy hierarchy) {
            return anyDependsButFirst(getCalcs(), hierarchy);
        }
    };
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc) ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc)

Example 5 with ValueCalc

use of mondrian.calc.impl.ValueCalc in project mondrian by pentaho.

the class SumFunDef method compileCall.

protected Calc compileCall(final ResolvedFunCall call, ExpCompiler compiler, ResultStyle resultStyle) {
    final Calc ncalc = compiler.compileIter(call.getArg(0));
    if (ncalc == null) {
        return null;
    }
    final Calc calc = call.getArgCount() > 1 ? compiler.compileScalar(call.getArg(1), true) : new ValueCalc(call);
    // we may have asked for one sort of Calc, but here's what we got.
    if (ncalc instanceof ListCalc) {
        return genListCalc(call, (ListCalc) ncalc, calc);
    } else {
        return genIterCalc(call, (IterCalc) ncalc, calc);
    }
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc) ValueCalc(mondrian.calc.impl.ValueCalc)

Aggregations

ValueCalc (mondrian.calc.impl.ValueCalc)15 AbstractDoubleCalc (mondrian.calc.impl.AbstractDoubleCalc)13 ArrayList (java.util.ArrayList)3 Calc (mondrian.calc.Calc)3 AbstractListCalc (mondrian.calc.impl.AbstractListCalc)3 DummyExp (mondrian.calc.DummyExp)2 IntegerCalc (mondrian.calc.IntegerCalc)2 LevelCalc (mondrian.calc.LevelCalc)2 ListCalc (mondrian.calc.ListCalc)2 MemberCalc (mondrian.calc.MemberCalc)2 TupleList (mondrian.calc.TupleList)2 UnaryTupleList (mondrian.calc.impl.UnaryTupleList)2 Evaluator (mondrian.olap.Evaluator)2 Hierarchy (mondrian.olap.Hierarchy)2 Level (mondrian.olap.Level)2 Member (mondrian.olap.Member)2 SchemaReader (mondrian.olap.SchemaReader)2 PrintWriter (java.io.PrintWriter)1 List (java.util.List)1 Locale (java.util.Locale)1