1 10 package mondrian.olap.fun; 11 12 import mondrian.olap.FunDef; 13 import mondrian.olap.Evaluator; 14 import mondrian.calc.Calc; 15 import mondrian.calc.ExpCompiler; 16 import mondrian.calc.ListCalc; 17 import mondrian.calc.impl.AbstractListCalc; 18 import mondrian.mdx.ResolvedFunCall; 19 20 import java.util.List ; 21 import java.util.Set ; 22 import java.util.HashSet ; 23 import java.util.ArrayList ; 24 25 32 class UnionFunDef extends FunDefBase { 33 static final String [] ReservedWords = new String [] {"ALL", "DISTINCT"}; 34 35 static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver( 36 "Union", 37 "Union(<Set1>, <Set2>[, ALL])", 38 "Returns the union of two sets, optionally retaining duplicates.", 39 new String [] {"fxxx", "fxxxy"}, 40 UnionFunDef.class, 41 ReservedWords); 42 43 public UnionFunDef(FunDef dummyFunDef) { 44 super(dummyFunDef); 45 } 46 47 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 48 String allString = getLiteralArg(call, 2, "DISTINCT", ReservedWords); 49 final boolean all = allString.equalsIgnoreCase("ALL"); 50 checkCompatible(call.getArg(0), call.getArg(1), null); final ListCalc listCalc0 = 52 compiler.compileList(call.getArg(0)); 53 final ListCalc listCalc1 = 54 compiler.compileList(call.getArg(1)); 55 return new AbstractListCalc(call, new Calc[] {listCalc0, listCalc1}) { 56 public List evaluateList(Evaluator evaluator) { 57 List list0 = listCalc0.evaluateList(evaluator); 58 List list1 = listCalc1.evaluateList(evaluator); 59 return union(list0, list1, all); 60 } 61 }; 62 } 63 64 <T> List <T> union(List <T> list0, List <T> list1, final boolean all) { 65 assert list0 != null; 66 assert list1 != null; 67 if (all) { 68 if (list0.isEmpty()) { 69 return list1; 70 } 71 if (list1.isEmpty()) { 72 return list0; 73 } 74 List <T> result = new ArrayList <T>(); 75 result.addAll(list0); 76 result.addAll(list1); 77 return result; 78 } else { 79 Set added = new HashSet (); 80 List <T> result = new ArrayList <T>(); 81 FunUtil.addUnique(result, list0, added); 82 FunUtil.addUnique(result, list1, added); 83 return result; 84 } 85 } 86 } 87 88 | Popular Tags |