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) 2005-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.udf;
011
012import mondrian.olap.*;
013import mondrian.olap.type.*;
014import mondrian.rolap.RolapUtil;
015import mondrian.spi.UserDefinedFunction;
016
017import java.util.List;
018
019/**
020 * Definition of the user-defined function "LastNonEmpty".
021 *
022 * @author jhyde
023 */
024public class LastNonEmptyUdf implements UserDefinedFunction {
025
026    public String getName() {
027        return "LastNonEmpty";
028    }
029
030    public String getDescription() {
031        return "Returns the last member of a set whose value is not empty";
032    }
033
034    public Syntax getSyntax() {
035        return Syntax.Function;
036    }
037
038    public Type getReturnType(Type[] parameterTypes) {
039        // Return type is the same as the elements of the first parameter.
040        // For example,
041        //    LastNonEmpty({[Time].[1997], [Time].[1997].[Q1]},
042        //                 [Measures].[Unit Sales])
043        // will return a member of the [Time] dimension.
044        SetType setType = (SetType) parameterTypes[0];
045        MemberType memberType = (MemberType) setType.getElementType();
046        return memberType;
047    }
048
049    public Type[] getParameterTypes() {
050        return new Type[] {
051            // The first argument must be a set of members (of any hierarchy).
052            new SetType(MemberType.Unknown),
053            // The second argument must be a member.
054            MemberType.Unknown,
055        };
056    }
057
058    public Object execute(Evaluator evaluator, Argument[] arguments) {
059        final Argument memberListExp = arguments[0];
060        final List memberList = (List) memberListExp.evaluate(evaluator);
061        final Argument exp = arguments[1];
062        int nullCount = 0;
063        int missCount = 0;
064        for (int i = memberList.size() - 1; i >= 0; --i) {
065            Member member = (Member) memberList.get(i);
066            // Create an evaluator with the member as its context.
067            Evaluator subEvaluator = evaluator.push();
068            subEvaluator.setContext(member);
069            int missCountBefore = subEvaluator.getMissCount();
070            final Object o = exp.evaluateScalar(subEvaluator);
071            int missCountAfter = subEvaluator.getMissCount();
072            if (Util.isNull(o)) {
073                ++nullCount;
074                continue;
075            }
076            if (missCountAfter > missCountBefore) {
077                // There was a cache miss while evaluating the expression, so
078                // the result is bogus. It would be a mistake to give up after
079                // one cache miss, because then it would take us N
080                // evaluate/fetch passes to move back through N members, which
081                // is way too many.
082                //
083                // Carry on until we have seen as many misses as we have seen
084                // null cells. The effect of this policy is that each pass
085                // examines twice as many cells as the previous pass. Thus
086                // we can move back through N members in log2(N) passes.
087                ++missCount;
088                if (missCount < 2 * nullCount + 1) {
089                    continue;
090                }
091            }
092            if (o == RolapUtil.valueNotReadyException) {
093                // Value is not in the cache yet, so we don't know whether
094                // it will be empty. Carry on...
095                continue;
096            }
097            if (o instanceof RuntimeException) {
098                RuntimeException runtimeException = (RuntimeException) o;
099                if (o == RolapUtil.valueNotReadyException) {
100                    // Value is not in the cache yet, so we don't know whether
101                    // it will be empty. Carry on...
102                    continue;
103                }
104                return runtimeException;
105            }
106            return member;
107        }
108        // Not found. Return the hierarchy's 'null member'.
109        // It is possible that a MemberType has a Dimension but
110        // no hierarchy, so we have to just get the type's Dimension's
111        // default hierarchy and return it's null member.
112        final Hierarchy hierarchy = memberListExp.getType().getHierarchy();
113        return (hierarchy == null)
114            ? memberListExp.getType().getDimension()
115                .getHierarchies()[0].getNullMember()
116            : hierarchy.getNullMember();
117    }
118
119    public String[] getReservedWords() {
120        // This function does not require any reserved words.
121        return null;
122    }
123}
124
125// End LastNonEmptyUdf.java