001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap.fun;
011
012import mondrian.calc.*;
013import mondrian.calc.impl.AbstractDoubleCalc;
014import mondrian.calc.impl.ValueCalc;
015import mondrian.mdx.ResolvedFunCall;
016import mondrian.olap.*;
017
018/**
019 * Definition of the <code>StdevP</code> builtin MDX function, and its alias
020 * <code>StddevP</code>.
021 *
022 * @author jhyde
023 * @since Mar 23, 2006
024 */
025class StdevPFunDef extends AbstractAggregateFunDef {
026
027    static final Resolver StdevpResolver =
028        new ReflectiveMultiResolver(
029            "StdevP",
030            "StdevP(<Set>[, <Numeric Expression>])",
031            "Returns the standard deviation of a numeric expression evaluated over a set (biased).",
032            new String[]{"fnx", "fnxn"},
033            StdevPFunDef.class);
034
035    static final Resolver StddevpResolver =
036        new ReflectiveMultiResolver(
037            "StddevP",
038            "StddevP(<Set>[, <Numeric Expression>])",
039            "Alias for StdevP.",
040            new String[]{"fnx", "fnxn"},
041            StdevPFunDef.class);
042
043    public StdevPFunDef(FunDef dummyFunDef) {
044        super(dummyFunDef);
045    }
046
047    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
048        final ListCalc listCalc =
049            compiler.compileList(call.getArg(0));
050        final Calc calc =
051            call.getArgCount() > 1
052            ? compiler.compileScalar(call.getArg(1), true)
053            : new ValueCalc(call);
054        return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
055            public double evaluateDouble(Evaluator evaluator) {
056                final int savepoint = evaluator.savepoint();
057                try {
058                    evaluator.setNonEmpty(false);
059                    TupleList list = evaluateCurrentList(listCalc, evaluator);
060                    final double stdev =
061                        (Double) stdev(
062                            evaluator, list, calc, true);
063                    return stdev;
064                } finally {
065                    evaluator.restore(savepoint);
066                }
067            }
068
069            public boolean dependsOn(Hierarchy hierarchy) {
070                return anyDependsButFirst(getCalcs(), hierarchy);
071            }
072        };
073    }
074}
075
076// End StdevPFunDef.java