001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/VarPFunDef.java#5 $
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-2008 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>VarP</code> MDX builtin function
026     * (and its synonym <code>VarianceP</code>).
027     *
028     * @author jhyde
029     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/VarPFunDef.java#5 $
030     * @since Mar 23, 2006
031     */
032    class VarPFunDef extends AbstractAggregateFunDef {
033        static final Resolver VariancePResolver = new ReflectiveMultiResolver(
034                "VarianceP",
035                "VarianceP(<Set>[, <Numeric Expression>])",
036                "Alias for VarP.",
037                new String[]{"fnx", "fnxn"},
038                VarPFunDef.class);
039    
040        static final Resolver VarPResolver = new ReflectiveMultiResolver(
041                "VarP",
042                "VarP(<Set>[, <Numeric Expression>])",
043                "Returns the variance of a numeric expression evaluated over a set (biased).",
044                new String[]{"fnx", "fnxn"},
045                VarPFunDef.class);
046    
047        public VarPFunDef(FunDef dummyFunDef) {
048            super(dummyFunDef);
049        }
050    
051        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
052            final ListCalc listCalc =
053                    compiler.compileList(call.getArg(0));
054            final Calc calc = call.getArgCount() > 1 ?
055                    compiler.compileScalar(call.getArg(1), true) :
056                    new ValueCalc(call);
057            return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
058                public double evaluateDouble(Evaluator evaluator) {
059                    List memberList = evaluateCurrentList(listCalc, evaluator);
060                    return (Double)var(evaluator.push(), memberList, calc, true);
061                }
062    
063                public boolean dependsOn(Dimension dimension) {
064                    return anyDependsButFirst(getCalcs(), dimension);
065                }
066            };
067        }
068    }
069    
070    // End VarPFunDef.java