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>Correlation</code> MDX function.
020 *
021 * @author jhyde
022 * @since Mar 23, 2006
023 */
024class CorrelationFunDef extends AbstractAggregateFunDef {
025    static final ReflectiveMultiResolver Resolver =
026        new ReflectiveMultiResolver(
027            "Correlation",
028            "Correlation(<Set>, <Numeric Expression>[, <Numeric Expression>])",
029            "Returns the correlation of two series evaluated over a set.",
030            new String[]{"fnxn", "fnxnn"},
031            CorrelationFunDef.class);
032
033    public CorrelationFunDef(FunDef dummyFunDef) {
034        super(dummyFunDef);
035    }
036
037    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
038        final ListCalc listCalc =
039            compiler.compileList(call.getArg(0));
040        final Calc calc1 =
041            compiler.compileScalar(call.getArg(1), true);
042        final Calc calc2 =
043            call.getArgCount() > 2
044            ? compiler.compileScalar(call.getArg(2), true)
045            : new ValueCalc(call);
046        return new AbstractDoubleCalc(
047            call, new Calc[] {listCalc, calc1, calc2})
048        {
049            public double evaluateDouble(Evaluator evaluator) {
050                final int savepoint = evaluator.savepoint();
051                try {
052                    evaluator.setNonEmpty(false);
053                    TupleList list = evaluateCurrentList(listCalc, evaluator);
054                    final double correlation =
055                        correlation(
056                            evaluator, list, calc1, calc2);
057                    return correlation;
058                } finally {
059                    evaluator.restore(savepoint);
060                }
061            }
062
063            public boolean dependsOn(Hierarchy hierarchy) {
064                return anyDependsButFirst(getCalcs(), hierarchy);
065            }
066        };
067    }
068}
069
070// End CorrelationFunDef.java