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>Stdev</code> builtin MDX function, and its alias
020 * <code>Stddev</code>.
021 *
022 * @author jhyde
023 * @since Mar 23, 2006
024 */
025class StdevFunDef extends AbstractAggregateFunDef {
026    static final ReflectiveMultiResolver StdevResolver =
027        new ReflectiveMultiResolver(
028            "Stdev",
029            "Stdev(<Set>[, <Numeric Expression>])",
030            "Returns the standard deviation of a numeric expression evaluated over a set (unbiased).",
031            new String[]{"fnx", "fnxn"},
032            StdevFunDef.class);
033
034    static final ReflectiveMultiResolver StddevResolver =
035        new ReflectiveMultiResolver(
036            "Stddev",
037            "Stddev(<Set>[, <Numeric Expression>])",
038            "Alias for Stdev.",
039            new String[]{"fnx", "fnxn"},
040            StdevFunDef.class);
041
042    public StdevFunDef(FunDef dummyFunDef) {
043        super(dummyFunDef);
044    }
045
046    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
047        final ListCalc listCalc =
048            compiler.compileList(call.getArg(0));
049        final Calc calc =
050            call.getArgCount() > 1
051            ? compiler.compileScalar(call.getArg(1), true)
052            : new ValueCalc(call);
053        return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
054            public double evaluateDouble(Evaluator evaluator) {
055                TupleList memberList = evaluateCurrentList(listCalc, evaluator);
056                final int savepoint = evaluator.savepoint();
057                try {
058                    evaluator.setNonEmpty(false);
059                    final double stdev =
060                        (Double) stdev(
061                            evaluator, memberList, calc, false);
062                    return stdev;
063                } finally {
064                    evaluator.restore(savepoint);
065                }
066            }
067
068            public boolean dependsOn(Hierarchy hierarchy) {
069                return anyDependsButFirst(getCalcs(), hierarchy);
070            }
071        };
072    }
073}
074
075// End StdevFunDef.java