001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/PercentileFunDef.java#1 $
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.*;
016    import mondrian.calc.impl.ValueCalc;
017    import mondrian.calc.impl.AbstractDoubleCalc;
018    import mondrian.mdx.ResolvedFunCall;
019    
020    import java.util.List;
021    
022    /**
023     * Definition of the <code>Percentile</code> MDX function.
024     *
025     * @author jhyde
026     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/PercentileFunDef.java#1 $
027     * @since Jan 16, 2008
028     */
029    class PercentileFunDef extends AbstractAggregateFunDef {
030        static final ReflectiveMultiResolver Resolver =
031            new ReflectiveMultiResolver(
032                "Percentile",
033                "Percentile(<Set>, <Numeric Expression>, <Percent>)",
034                "Returns the value of the tuple that is at a given percentile of a set.",
035                new String[] {"fnxnn"},
036                PercentileFunDef.class);
037    
038        public PercentileFunDef(FunDef dummyFunDef) {
039            super(dummyFunDef);
040        }
041    
042        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
043            final ListCalc listCalc =
044                compiler.compileList(call.getArg(0));
045            final Calc calc =
046                compiler.compileScalar(call.getArg(1), true);
047            final DoubleCalc percentCalc =
048                compiler.compileDouble(call.getArg(2));
049            return new AbstractDoubleCalc(
050                call, new Calc[] {listCalc, calc, percentCalc})
051            {
052                public double evaluateDouble(Evaluator evaluator) {
053                    List memberList = evaluateCurrentList(listCalc, evaluator);
054                    double percent = percentCalc.evaluateDouble(evaluator) * 0.01;
055                    return percentile(evaluator.push(), memberList, calc, percent);
056                }
057    
058                public boolean dependsOn(Dimension dimension) {
059                    return anyDependsButFirst(getCalcs(), dimension);
060                }
061            };
062        }
063    }
064    
065    // End PercentileFunDef.java