1 10 package mondrian.udf; 11 12 import mondrian.olap.*; 13 import mondrian.olap.type.*; 14 import mondrian.rolap.RolapUtil; 15 import mondrian.spi.UserDefinedFunction; 16 17 import java.util.List ; 18 19 25 public class LastNonEmptyUdf implements UserDefinedFunction { 26 27 public String getName() { 28 return "LastNonEmpty"; 29 } 30 31 public String getDescription() { 32 return "Returns the last member of a set whose value is not empty"; 33 } 34 35 public Syntax getSyntax() { 36 return Syntax.Function; 37 } 38 39 public Type getReturnType(Type[] parameterTypes) { 40 SetType setType = (SetType) parameterTypes[0]; 46 MemberType memberType = (MemberType) setType.getElementType(); 47 return memberType; 48 } 49 50 public Type[] getParameterTypes() { 51 return new Type[] { 52 new SetType(MemberType.Unknown), 54 MemberType.Unknown, 56 }; 57 } 58 59 public Object execute(Evaluator evaluator, Argument[] arguments) { 60 final Argument memberListExp = arguments[0]; 61 final List memberList = (List ) memberListExp.evaluate(evaluator); 62 final Argument exp = arguments[1]; 63 int nullCount = 0; 64 int missCount = 0; 65 for (int i = memberList.size() - 1; i >= 0; --i) { 66 Member member = (Member) memberList.get(i); 67 Evaluator subEvaluator = evaluator.push(member); 69 int missCountBefore = subEvaluator.getMissCount(); 70 final Object o = exp.evaluateScalar(subEvaluator); 71 int missCountAfter = subEvaluator.getMissCount(); 72 if (Util.isNull(o)) { 73 ++nullCount; 74 continue; 75 } 76 if (missCountAfter > missCountBefore) { 77 ++missCount; 88 if (missCount < 2*nullCount+1) { 89 continue; 90 } 91 } 92 if (o == RolapUtil.valueNotReadyException) { 93 continue; 96 } 97 if (o instanceof RuntimeException ) { 98 RuntimeException runtimeException = (RuntimeException ) o; 99 if (o == RolapUtil.valueNotReadyException) { 100 continue; 103 } 104 return runtimeException; 105 } 106 return member; 107 } 108 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 return null; 122 } 123 } 124 125 | Popular Tags |