001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/VarFunDef.java#4 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2007 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap.fun;
011    
012    import mondrian.olap.FunDef;
013    import mondrian.olap.Evaluator;
014    import mondrian.olap.Dimension;
015    import mondrian.calc.Calc;
016    import mondrian.calc.ExpCompiler;
017    import mondrian.calc.ListCalc;
018    import mondrian.calc.impl.ValueCalc;
019    import mondrian.calc.impl.AbstractDoubleCalc;
020    import mondrian.mdx.ResolvedFunCall;
021    
022    import java.util.List;
023    
024    /**
025     * Definition of the <code>Var</code> MDX builtin function
026     * (and its synonym <code>Variance</code>).
027     *
028     * @author jhyde
029     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/VarFunDef.java#4 $
030     * @since Mar 23, 2006
031     */
032    class VarFunDef extends AbstractAggregateFunDef {
033        static final Resolver VarResolver = new ReflectiveMultiResolver(
034                "Var",
035                "Var(<Set>[, <Numeric Expression>])",
036                "Returns the variance of a numeric expression evaluated over a set (unbiased).",
037                new String[]{"fnx", "fnxn"},
038                VarFunDef.class);
039    
040        static final Resolver VarianceResolver = new ReflectiveMultiResolver(
041                "Variance", "Variance(<Set>[, <Numeric Expression>])",
042                "Alias for Var.",
043                new String[]{"fnx", "fnxn"},
044                VarFunDef.class);
045    
046        public VarFunDef(FunDef dummyFunDef) {
047            super(dummyFunDef);
048        }
049    
050        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
051            final ListCalc listCalc =
052                    compiler.compileList(call.getArg(0));
053            final Calc calc = call.getArgCount() > 1 ?
054                    compiler.compileScalar(call.getArg(1), true) :
055                    new ValueCalc(call);
056            return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
057                public double evaluateDouble(Evaluator evaluator) {
058                    List memberList = evaluateCurrentList(listCalc, evaluator);
059                    return (Double)var(evaluator.push(), memberList, calc, false);
060                }
061    
062                public boolean dependsOn(Dimension dimension) {
063                    return anyDependsButFirst(getCalcs(), dimension);
064                }
065            };
066        }
067    }
068    
069    // End VarFunDef.java