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>VarP</code> MDX builtin function
020 * (and its synonym <code>VarianceP</code>).
021 *
022 * @author jhyde
023 * @since Mar 23, 2006
024 */
025class VarPFunDef extends AbstractAggregateFunDef {
026    static final Resolver VariancePResolver =
027        new ReflectiveMultiResolver(
028            "VarianceP",
029            "VarianceP(<Set>[, <Numeric Expression>])",
030            "Alias for VarP.",
031            new String[]{"fnx", "fnxn"},
032            VarPFunDef.class);
033
034    static final Resolver VarPResolver =
035        new ReflectiveMultiResolver(
036            "VarP",
037            "VarP(<Set>[, <Numeric Expression>])",
038            "Returns the variance of a numeric expression evaluated over a set (biased).",
039            new String[]{"fnx", "fnxn"},
040            VarPFunDef.class);
041
042    public VarPFunDef(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                    return
060                        (Double) var(evaluator, memberList, calc, true);
061                } finally {
062                    evaluator.restore(savepoint);
063                }
064            }
065
066            public boolean dependsOn(Hierarchy hierarchy) {
067                return anyDependsButFirst(getCalcs(), hierarchy);
068            }
069        };
070    }
071}
072
073// End VarPFunDef.java