<commit_before>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 <NAME>
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.udf;

import mondrian.olap.*;
import mondrian.olap.type.*;
import mondrian.rolap.RolapUtil;
import mondrian.spi.UserDefinedFunction;

import java.util.List;

/**
 * Definition of the user-defined function "LastNonEmpty".
 *
 * @author jhyde
 * @version $Id$
 */
public class LastNonEmptyUdf implements UserDefinedFunction {

    public String getName() {
        return "LastNonEmpty";
    }

    public String getDescription() {
        return "Returns the last member of a set whose value is not empty";
    }

    public Syntax getSyntax() {
        return Syntax.Function;
    }

    public Type getReturnType(Type[] parameterTypes) {
        // Return type is the same as the elements of the first parameter.
        // For example,
        //    LastNonEmpty({[Time].[1997], [Time].[1997].[Q1]},
        //                 [Measures].[Unit Sales])
        // will return a member of the [Time] dimension.
        SetType setType = (SetType) parameterTypes[0];
        MemberType memberType = (MemberType) setType.getElementType();
        return memberType;
    }

    public Type[] getParameterTypes() {
        return new Type[] {
            // The first argument must be a set of members (of any hierarchy).
            new SetType(MemberType.Unknown),
            // The second argument must be a member.
            MemberType.Unknown,
        };
    }

    public Object execute(Evaluator evaluator, Argument[] arguments) {
        final Argument memberListExp = arguments[0];
        final List memberList = (List) memberListExp.evaluate(evaluator);
        final Argument exp = arguments[1];
        for (int i = memberList.size() - 1; i >= 0; --i) {
            Member member = (Member) memberList.get(i);
            // Create an evaluator with the member as its context.
            Evaluator subEvaluator = evaluator.push(member);
            final Object o = exp.evaluateScalar(subEvaluator);
            if (o == Util.nullValue) {
                continue;
            }
            if (o instanceof RuntimeException) {
                RuntimeException runtimeException = (RuntimeException) o;
                if (o == RolapUtil.valueNotReadyException) {
                    // Value is not in the cache yet, so we don't know whether
                    // it will be empty. Carry on...
                    continue;
                }
                return runtimeException;
            }
            return member;
        }
        // Not found. Return the hierarchy's 'null member'.
        final Hierarchy hierarchy = memberListExp.getType().getHierarchy();
        return hierarchy.getNullMember();<commit_msg>MONDRIAN        When there are no members and the type is MemberType with only        a Dimension, then the hierarchy variable is 'null' so we        have to have another way of returning a null memeber.<commit_after>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 <NAME>
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.udf;

import mondrian.olap.*;
import mondrian.olap.type.*;
import mondrian.rolap.RolapUtil;
import mondrian.spi.UserDefinedFunction;

import java.util.List;

/**
 * Definition of the user-defined function "LastNonEmpty".
 *
 * @author jhyde
 * @version $Id$
 */
public class LastNonEmptyUdf implements UserDefinedFunction {

    public String getName() {
        return "LastNonEmpty";
    }

    public String getDescription() {
        return "Returns the last member of a set whose value is not empty";
    }

    public Syntax getSyntax() {
        return Syntax.Function;
    }

    public Type getReturnType(Type[] parameterTypes) {
        // Return type is the same as the elements of the first parameter.
        // For example,
        //    LastNonEmpty({[Time].[1997], [Time].[1997].[Q1]},
        //                 [Measures].[Unit Sales])
        // will return a member of the [Time] dimension.
        SetType setType = (SetType) parameterTypes[0];
        MemberType memberType = (MemberType) setType.getElementType();
        return memberType;
    }

    public Type[] getParameterTypes() {
        return new Type[] {
            // The first argument must be a set of members (of any hierarchy).
            new SetType(MemberType.Unknown),
            // The second argument must be a member.
            MemberType.Unknown,
        };
    }

    public Object execute(Evaluator evaluator, Argument[] arguments) {
        final Argument memberListExp = arguments[0];
        final List memberList = (List) memberListExp.evaluate(evaluator);
        final Argument exp = arguments[1];
        for (int i = memberList.size() - 1; i >= 0; --i) {
            Member member = (Member) memberList.get(i);
            // Create an evaluator with the member as its context.
            Evaluator subEvaluator = evaluator.push(member);
            final Object o = exp.evaluateScalar(subEvaluator);
            if (o == Util.nullValue) {
                continue;
            }
            if (o instanceof RuntimeException) {
                RuntimeException runtimeException = (RuntimeException) o;
                if (o == RolapUtil.valueNotReadyException) {
                    // Value is not in the cache yet, so we don't know whether
                    // it will be empty. Carry on...
                    continue;
                }
                return runtimeException;
            }
            return member;
        }
        // Not found. Return the hierarchy's 'null member'.
        // It is possible that a MemberType has a Dimension but
        // no hierarchy, so we have to just get the type's Dimension's
        // default hierarchy and return it's null member.
        final Hierarchy hierarchy = memberListExp.getType().getHierarchy();
        return (hierarchy == null)
            ? memberListExp.getType().getDimension().
                getHierarchies()[0].getNullMember()
            : hierarchy.getNullMember();
{'fraction_non_alphanumeric': 0.09627569154690156, 'fraction_numerical': 0.006954102920723227, 'mean_word_length': 2.105566218809981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '121760', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1654, 'n_words': 581}