Search in sources :

Example 11 with ValueCalc

use of mondrian.calc.impl.ValueCalc 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)

Example 12 with ValueCalc

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

the class AvgFunDef 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) {
            evaluator.getTiming().markStart(TIMING_NAME);
            final int savepoint = evaluator.savepoint();
            try {
                TupleList memberList = evaluateCurrentList(listCalc, evaluator);
                evaluator.setNonEmpty(false);
                return (Double) avg(evaluator, memberList, calc);
            } finally {
                evaluator.restore(savepoint);
                evaluator.getTiming().markEnd(TIMING_NAME);
            }
        }

        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 13 with ValueCalc

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

the class MinMaxFunDef 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) {
            evaluator.getTiming().markStart(TIMING_NAME);
            final int savepoint = evaluator.savepoint();
            try {
                TupleList memberList = evaluateCurrentList(listCalc, evaluator);
                evaluator.setNonEmpty(false);
                return (Double) (max ? max(evaluator, memberList, calc) : min(evaluator, memberList, calc));
            } finally {
                evaluator.restore(savepoint);
                evaluator.getTiming().markEnd(TIMING_NAME);
            }
        }

        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 14 with ValueCalc

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

the class StdevFunDef 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);
                final double stdev = (Double) stdev(evaluator, memberList, calc, false);
                return stdev;
            } 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 15 with ValueCalc

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

the class LinReg method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final ListCalc listCalc = compiler.compileList(call.getArg(0));
    final DoubleCalc yCalc = compiler.compileDouble(call.getArg(1));
    final DoubleCalc xCalc = call.getArgCount() > 2 ? compiler.compileDouble(call.getArg(2)) : new ValueCalc(call);
    return new LinRegCalc(call, listCalc, yCalc, xCalc, regType);
}
Also used : ValueCalc(mondrian.calc.impl.ValueCalc) AbstractDoubleCalc(mondrian.calc.impl.AbstractDoubleCalc)

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