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>Var</code> MDX builtin function
020 * (and its synonym <code>Variance</code>).
021 *
022 * @author jhyde
023 * @since Mar 23, 2006
024 */
025class VarFunDef extends AbstractAggregateFunDef {
026    static final Resolver VarResolver =
027        new ReflectiveMultiResolver(
028            "Var",
029            "Var(<Set>[, <Numeric Expression>])",
030            "Returns the variance of a numeric expression evaluated over a set (unbiased).",
031            new String[]{"fnx", "fnxn"},
032            VarFunDef.class);
033
034    static final Resolver VarianceResolver =
035        new ReflectiveMultiResolver(
036            "Variance",
037            "Variance(<Set>[, <Numeric Expression>])",
038            "Alias for Var.",
039            new String[]{"fnx", "fnxn"},
040            VarFunDef.class);
041
042    public VarFunDef(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                final int savepoint = evaluator.savepoint();
056                try {
057                    evaluator.setNonEmpty(false);
058                    TupleList list = evaluateCurrentList(listCalc, evaluator);
059                    final double var =
060                        (Double) var(
061                            evaluator, list, calc, false);
062                    return var;
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 VarFunDef.java