/
FunctionTest.java
9703 lines (8775 loc) · 442 KB
/
FunctionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// $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) 2003-2009 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.olap.fun;
import junit.framework.Assert;
import junit.framework.ComparisonFailure;
import mondrian.olap.*;
import mondrian.test.*;
import mondrian.resource.MondrianResource;
import mondrian.udf.CurrentDateMemberExactUdf;
import mondrian.udf.CurrentDateMemberUdf;
import mondrian.udf.CurrentDateStringUdf;
import mondrian.util.Bug;
import org.eigenbase.xom.StringEscaper;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
/**
* <code>FunctionTest</code> tests the functions defined in
* {@link BuiltinFunTable}.
*
* @author gjohnson
* @version $Id$
*/
public class FunctionTest extends FoodMartTestCase {
private static final Logger LOGGER = Logger.getLogger(FunctionTest.class);
private static final String months =
fold("[Time].[1997].[Q1].[1]\n" +
"[Time].[1997].[Q1].[2]\n" +
"[Time].[1997].[Q1].[3]\n" +
"[Time].[1997].[Q2].[4]\n" +
"[Time].[1997].[Q2].[5]\n" +
"[Time].[1997].[Q2].[6]\n" +
"[Time].[1997].[Q3].[7]\n" +
"[Time].[1997].[Q3].[8]\n" +
"[Time].[1997].[Q3].[9]\n" +
"[Time].[1997].[Q4].[10]\n" +
"[Time].[1997].[Q4].[11]\n" +
"[Time].[1997].[Q4].[12]");
private static final String quarters =
fold("[Time].[1997].[Q1]\n" +
"[Time].[1997].[Q2]\n" +
"[Time].[1997].[Q3]\n" +
"[Time].[1997].[Q4]");
private static final String year1997 = "[Time].[1997]";
private static final String hierarchized1997 =
fold(year1997 + "\n" +
"[Time].[1997].[Q1]\n" +
"[Time].[1997].[Q1].[1]\n" +
"[Time].[1997].[Q1].[2]\n" +
"[Time].[1997].[Q1].[3]\n" +
"[Time].[1997].[Q2]\n" +
"[Time].[1997].[Q2].[4]\n" +
"[Time].[1997].[Q2].[5]\n" +
"[Time].[1997].[Q2].[6]\n" +
"[Time].[1997].[Q3]\n" +
"[Time].[1997].[Q3].[7]\n" +
"[Time].[1997].[Q3].[8]\n" +
"[Time].[1997].[Q3].[9]\n" +
"[Time].[1997].[Q4]\n" +
"[Time].[1997].[Q4].[10]\n" +
"[Time].[1997].[Q4].[11]\n" +
"[Time].[1997].[Q4].[12]");
private static final String NullNumericExpr = (" ([Measures].[Unit Sales]," +
" [Customers].[All Customers].[USA].[CA].[Bellflower], " +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Good].[Good Imported Beer])");
// ~ Constructors ----------------------------------------------------------
/**
* Creates a FunctionTest.
*/
public FunctionTest() {
}
/**
* Creates a FucntionTest.
*
* @param s Test name
*/
public FunctionTest(String s) {
super(s);
}
// ~ Methods ---------------------------------------------------------------
// ~ Test methods ----------------------------------------------------------
/**
* Tests that Integeer.MIN_VALUE(-2147483648) does not cause NPE.
*/
public void testParallelPeriodMinValue() {
String query =
"with " +
"member [measures].[foo] as " +
"'([Measures].[unit sales],ParallelPeriod([Time].[Quarter], -2147483648))' " +
"select " +
"[measures].[foo] on columns, " +
"[time].[1997].children on rows " +
"from [sales]";
executeQuery(query);
}
/**
* Tests that Integeer.MIN_VALUE(-2147483648) in Lag is handled correctly.
*/
public void testLagMinValue() {
String query =
"with " +
"member [measures].[foo] as " +
"'([Measures].[unit sales], [Time].[1997].[Q1].Lag(-2147483648))' " +
"select " +
"[measures].[foo] on columns, " +
"[time].[1997].children on rows " +
"from [sales]";
executeQuery(query);
}
/*
* Tests that ParallelPeriod with Aggregate function works
*/
public void testParallelPeriodWithSlicer() {
String query =
"With " +
"Set [*NATIVE_CJ_SET] as 'NonEmptyCrossJoin([*BASE_MEMBERS_Time],[*BASE_MEMBERS_Product])' " +
"Set [*BASE_MEMBERS_Measures] as '{[Measures].[*FORMATTED_MEASURE_0], [Measures].[*FORMATTED_MEASURE_1]}' " +
"Set [*BASE_MEMBERS_Time] as '{[Time].[1997].[Q2].[6]}' " +
"Set [*NATIVE_MEMBERS_Time] as 'Generate([*NATIVE_CJ_SET], {[Time].CurrentMember})' " +
"Set [*BASE_MEMBERS_Product] as '{[Product].[All Products].[Drink],[Product].[All Products].[Food]}' " +
"Set [*NATIVE_MEMBERS_Product] as 'Generate([*NATIVE_CJ_SET], {[Product].CurrentMember})' " +
"Member [Measures].[*FORMATTED_MEASURE_0] as '[Measures].[Customer Count]', FORMAT_STRING = '#,##0', SOLVE_ORDER=400 " +
"Member [Measures].[*FORMATTED_MEASURE_1] as " +
"'([Measures].[Customer Count], ParallelPeriod([Time].[Quarter], 1, [Time].currentMember))', FORMAT_STRING = '#,##0', SOLVE_ORDER=-200 " +
"Member [Product].[*FILTER_MEMBER] as 'Aggregate ([*NATIVE_MEMBERS_Product])', SOLVE_ORDER=-300 " +
"Select " +
"[*BASE_MEMBERS_Measures] on columns, Non Empty Generate([*NATIVE_CJ_SET], {([Time].CurrentMember)}) on rows " +
"From [Sales] " +
"Where ([Product].[*FILTER_MEMBER])";
String result =
"Axis #0:\n" +
"{[Product].[*FILTER_MEMBER]}\n" +
"Axis #1:\n" +
"{[Measures].[*FORMATTED_MEASURE_0]}\n" +
"{[Measures].[*FORMATTED_MEASURE_1]}\n" +
"Axis #2:\n" +
"{[Time].[1997].[Q2].[6]}\n" +
"Row #0: 1,314\n" +
"Row #0: 1,447\n";
assertQueryReturns(query, fold(result));
}
public void testParallelperiodOnLevelsString() {
assertQueryReturns(
"with member Measures.[Prev Unit Sales] as 'parallelperiod(Levels(\"[Time].[Month]\"))'\n" +
"select {[Measures].[Unit Sales], Measures.[Prev Unit Sales]} ON COLUMNS,\n" +
"[Gender].members ON ROWS\n" +
"from [Sales]\n" +
"where [Time].[1997].[Q2].[5]",
fold("Axis #0:\n" +
"{[Time].[1997].[Q2].[5]}\n" +
"Axis #1:\n" +
"{[Measures].[Unit Sales]}\n" +
"{[Measures].[Prev Unit Sales]}\n" +
"Axis #2:\n" +
"{[Gender].[All Gender]}\n" +
"{[Gender].[All Gender].[F]}\n" +
"{[Gender].[All Gender].[M]}\n" +
"Row #0: 21,081\n" +
"Row #0: 20,179\n" +
"Row #1: 10,536\n" +
"Row #1: 9,990\n" +
"Row #2: 10,545\n" +
"Row #2: 10,189\n"));
}
public void testParallelperiodOnStrToMember() {
assertQueryReturns(
"with member Measures.[Prev Unit Sales] as 'parallelperiod(strToMember(\"[Time].[1997].[Q2]\"))'\n" +
"select {[Measures].[Unit Sales], Measures.[Prev Unit Sales]} ON COLUMNS,\n" +
"[Gender].members ON ROWS\n" +
"from [Sales]\n" +
"where [Time].[1997].[Q2].[5]",
fold("Axis #0:\n" +
"{[Time].[1997].[Q2].[5]}\n" +
"Axis #1:\n" +
"{[Measures].[Unit Sales]}\n" +
"{[Measures].[Prev Unit Sales]}\n" +
"Axis #2:\n" +
"{[Gender].[All Gender]}\n" +
"{[Gender].[All Gender].[F]}\n" +
"{[Gender].[All Gender].[M]}\n" +
"Row #0: 21,081\n" +
"Row #0: 20,957\n" +
"Row #1: 10,536\n" +
"Row #1: 10,266\n" +
"Row #2: 10,545\n" +
"Row #2: 10,691\n"));
assertThrows(
"with member Measures.[Prev Unit Sales] as 'parallelperiod(strToMember(\"[Time].[Quarter]\"))'\n" +
"select {[Measures].[Unit Sales], Measures.[Prev Unit Sales]} ON COLUMNS,\n" +
"[Gender].members ON ROWS\n" +
"from [Sales]\n" +
"where [Time].[1997].[Q2].[5]",
"Cannot find MDX member '[Time].[Quarter]'. Make sure it is indeed a member and not a level or a hierarchy.");
}
public void testNumericLiteral() {
assertExprReturns("2", "2");
if (false) {
// The test is currently broken because the value 2.5 is formatted
// as "2". TODO: better default format string
assertExprReturns("2.5", "2.5");
}
assertExprReturns("-10.0", "-10");
getTestContext().assertExprDependsOn("1.5", "{}");
}
public void testStringLiteral() {
// single-quoted string
if (false) {
// TODO: enhance parser so that you can include a quoted string
// inside a WITH MEMBER clause
assertExprReturns("'foobar'", "foobar");
}
// double-quoted string
assertExprReturns("\"foobar\"", "foobar");
// literals don't depend on any dimensions
getTestContext().assertExprDependsOn("\"foobar\"", "{}");
}
public void testDimensionHierarchy() {
assertExprReturns("[Time].Dimension.Name", "Time");
}
public void testLevelDimension() {
assertExprReturns("[Time].[Year].Dimension.UniqueName", "[Time]");
}
public void testMemberDimension() {
assertExprReturns("[Time].[1997].[Q2].Dimension.UniqueName", "[Time]");
}
public void testDimensionsNumeric() {
getTestContext().assertExprDependsOn("Dimensions(2).Name", "{}");
getTestContext().assertMemberExprDependsOn("Dimensions(3).CurrentMember",
TestContext.allDims());
assertExprReturns("Dimensions(2).Name", "Store Size in SQFT");
// bug 1426134 -- Dimensions(0) throws 'Index '0' out of bounds'
assertExprReturns("Dimensions(0).Name", "Measures");
assertExprThrows("Dimensions(-1).Name", "Index '-1' out of bounds");
assertExprThrows("Dimensions(100).Name", "Index '100' out of bounds");
}
public void testDimensionsString() {
getTestContext().assertExprDependsOn("Dimensions(\"foo\").UniqueName",
"{}");
getTestContext().assertMemberExprDependsOn(
"Dimensions(\"foo\").CurrentMember", TestContext.allDims());
assertExprReturns("Dimensions(\"Store\").UniqueName", "[Store]");
}
public void testDimensionsDepends() {
final String expression =
"Crossjoin(" +
"{Dimensions(\"Measures\").CurrentMember.Hierarchy.CurrentMember}, " +
"{Dimensions(\"Product\")})";
assertAxisReturns(
expression,
"{[Measures].[Unit Sales], [Product].[All Products]}");
getTestContext().assertSetExprDependsOn(expression, TestContext.allDims());
}
public void testTime() {
assertExprReturns("[Time].[1997].[Q1].[1].Hierarchy.UniqueName", "[Time]");
}
public void testBasic9() {
assertExprReturns("[Gender].[All Gender].[F].Hierarchy.UniqueName", "[Gender]");
}
public void testFirstInLevel9() {
assertExprReturns("[Education Level].[All Education Levels].[Bachelors Degree].Hierarchy.UniqueName", "[Education Level]");
}
public void testHierarchyAll() {
assertExprReturns("[Gender].[All Gender].Hierarchy.UniqueName", "[Gender]");
}
public void testNullMember() {
// MSAS fails here, but Mondrian doesn't.
assertExprReturns("[Gender].[All Gender].Parent.Level.UniqueName",
"[Gender].[(All)]");
// MSAS fails here, but Mondrian doesn't.
assertExprReturns("[Gender].[All Gender].Parent.Hierarchy.UniqueName",
"[Gender]");
// MSAS fails here, but Mondrian doesn't.
assertExprReturns("[Gender].[All Gender].Parent.Dimension.UniqueName",
"[Gender]");
// MSAS succeeds too
assertExprReturns("[Gender].[All Gender].Parent.Children.Count",
"0");
if (isDefaultNullMemberRepresentation()) {
// MSAS returns "" here.
assertExprReturns("[Gender].[All Gender].Parent.UniqueName",
"[Gender].[#null]");
// MSAS returns "" here.
assertExprReturns("[Gender].[All Gender].Parent.Name",
"#null");
}
}
/**
* Tests use of NULL literal to generate a null cell value.
* Testcase is from bug 1440344.
*/
public void testNullValue() {
assertQueryReturns("with member [Measures].[X] as 'IIF([Measures].[Store Sales]>10000,[Measures].[Store Sales],Null)'\n" +
"select\n" +
"{[Measures].[X]} on columns,\n" +
"{[Product].[Product Department].members} on rows\n" +
"from Sales",
fold("Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[X]}\n" +
"Axis #2:\n" +
"{[Product].[All Products].[Drink].[Alcoholic Beverages]}\n" +
"{[Product].[All Products].[Drink].[Beverages]}\n" +
"{[Product].[All Products].[Drink].[Dairy]}\n" +
"{[Product].[All Products].[Food].[Baked Goods]}\n" +
"{[Product].[All Products].[Food].[Baking Goods]}\n" +
"{[Product].[All Products].[Food].[Breakfast Foods]}\n" +
"{[Product].[All Products].[Food].[Canned Foods]}\n" +
"{[Product].[All Products].[Food].[Canned Products]}\n" +
"{[Product].[All Products].[Food].[Dairy]}\n" +
"{[Product].[All Products].[Food].[Deli]}\n" +
"{[Product].[All Products].[Food].[Eggs]}\n" +
"{[Product].[All Products].[Food].[Frozen Foods]}\n" +
"{[Product].[All Products].[Food].[Meat]}\n" +
"{[Product].[All Products].[Food].[Produce]}\n" +
"{[Product].[All Products].[Food].[Seafood]}\n" +
"{[Product].[All Products].[Food].[Snack Foods]}\n" +
"{[Product].[All Products].[Food].[Snacks]}\n" +
"{[Product].[All Products].[Food].[Starchy Foods]}\n" +
"{[Product].[All Products].[Non-Consumable].[Carousel]}\n" +
"{[Product].[All Products].[Non-Consumable].[Checkout]}\n" +
"{[Product].[All Products].[Non-Consumable].[Health and Hygiene]}\n" +
"{[Product].[All Products].[Non-Consumable].[Household]}\n" +
"{[Product].[All Products].[Non-Consumable].[Periodicals]}\n" +
"Row #0: 14,029.08\n" +
"Row #1: 27,748.53\n" +
"Row #2: \n" +
"Row #3: 16,455.43\n" +
"Row #4: 38,670.41\n" +
"Row #5: \n" +
"Row #6: 39,774.34\n" +
"Row #7: \n" +
"Row #8: 30,508.85\n" +
"Row #9: 25,318.93\n" +
"Row #10: \n" +
"Row #11: 55,207.50\n" +
"Row #12: \n" +
"Row #13: 82,248.42\n" +
"Row #14: \n" +
"Row #15: 67,609.82\n" +
"Row #16: 14,550.05\n" +
"Row #17: 11,756.07\n" +
"Row #18: \n" +
"Row #19: \n" +
"Row #20: 32,571.86\n" +
"Row #21: 60,469.89\n" +
"Row #22: \n"));
}
public void testNullInMultiplication() {
assertExprReturns("NULL*1", "");
assertExprReturns("1*NULL", "");
assertExprReturns("NULL*NULL", "");
}
public void testNullInAddition() {
assertExprReturns("1+NULL", "1");
assertExprReturns("NULL+1", "1");
}
public void testNullInSubtraction() {
assertExprReturns("1-NULL", "1");
assertExprReturns("NULL-1", "-1");
}
public void testMemberLevel() {
assertExprReturns("[Time].[1997].[Q1].[1].Level.UniqueName", "[Time].[Month]");
}
public void testLevelsNumeric() {
assertExprReturns("[Time].Levels(2).Name", "Month");
assertExprReturns("[Time].Levels(0).Name", "Year");
assertExprReturns("[Product].Levels(0).Name", "(All)");
}
public void testLevelsTooSmall() {
assertExprThrows("[Time].Levels(-1).Name",
"Index '-1' out of bounds");
}
public void testLevelsTooLarge() {
assertExprThrows("[Time].Levels(8).Name",
"Index '8' out of bounds");
}
public void testHierarchyLevelsString() {
assertExprReturns("[Time].Levels(\"Year\").UniqueName", "[Time].[Year]");
}
public void testHierarchyLevelsStringFail() {
assertExprThrows("[Time].Levels(\"nonexistent\").UniqueName",
"Level 'nonexistent' not found in hierarchy '[Time]'");
}
public void testLevelsString() {
assertExprReturns("Levels(\"[Time].[Year]\").UniqueName", "[Time].[Year]");
}
public void testLevelsStringFail() {
assertExprThrows("Levels(\"nonexistent\").UniqueName",
"Level 'nonexistent' not found");
}
public void testIsEmptyQuery() {
String desiredResult = fold("Axis #0:\n" +
"{[Time].[1997].[Q4].[12], [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Portsmouth].[Portsmouth Imported Beer], [Measures].[Foo]}\n" +
"Axis #1:\n" +
"{[Store].[All Stores].[USA].[WA].[Bellingham]}\n" +
"{[Store].[All Stores].[USA].[WA].[Bremerton]}\n" +
"{[Store].[All Stores].[USA].[WA].[Seattle]}\n" +
"{[Store].[All Stores].[USA].[WA].[Spokane]}\n" +
"{[Store].[All Stores].[USA].[WA].[Tacoma]}\n" +
"{[Store].[All Stores].[USA].[WA].[Walla Walla]}\n" +
"{[Store].[All Stores].[USA].[WA].[Yakima]}\n" +
"Row #0: 5\n" +
"Row #0: 5\n" +
"Row #0: 2\n" +
"Row #0: 5\n" +
"Row #0: 11\n" +
"Row #0: 5\n" +
"Row #0: 4\n");
assertQueryReturns(
"WITH MEMBER [Measures].[Foo] AS 'Iif(IsEmpty([Measures].[Unit Sales]), 5, [Measures].[Unit Sales])'\n" +
"SELECT {[Store].[USA].[WA].children} on columns\n" +
"FROM Sales\n" +
"WHERE ([Time].[1997].[Q4].[12],\n" +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Portsmouth].[Portsmouth Imported Beer],\n" +
" [Measures].[Foo])",
desiredResult);
assertQueryReturns(
"WITH MEMBER [Measures].[Foo] AS 'Iif([Measures].[Unit Sales] IS EMPTY, 5, [Measures].[Unit Sales])'\n" +
"SELECT {[Store].[USA].[WA].children} on columns\n" +
"FROM Sales\n" +
"WHERE ([Time].[1997].[Q4].[12],\n" +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Portsmouth].[Portsmouth Imported Beer],\n" +
" [Measures].[Foo])",
desiredResult);
}
public void testIsEmptyWithAggregate() {
assertQueryReturns(
"WITH MEMBER [gender].[foo] AS 'isEmpty(Aggregate({[Gender].m}))' " +
"SELECT {Gender.foo} on 0 from sales",
fold(
"Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Gender].[foo]}\n" +
"Row #0: false\n"));
}
public void testIsEmpty()
{
assertBooleanExprReturns("[Gender].[All Gender].Parent IS NULL", true);
// Any functions that return a member from parameters that
// include a member and that member is NULL also give a NULL.
// Not a runtime exception.
assertBooleanExprReturns("[Gender].CurrentMember.Parent.NextMember IS NULL", true);
if (!Bug.Bug1530543Fixed) {
return;
}
// When resolving a tuple's value in the cube, if there is
// at least one NULL member in the tuple should return a
// NULL cell value.
assertBooleanExprReturns("IsEmpty(([Time].currentMember.Parent, [Measures].[Unit Sales]))", false);
assertBooleanExprReturns("IsEmpty(([Time].currentMember, [Measures].[Unit Sales]))", false);
// EMPTY refers to a genuine cell value that exists in the cube space,
// and has no NULL members in the tuple,
// but has no fact data at that crossing,
// so it evaluates to EMPTY as a cell value.
assertBooleanExprReturns("IsEmpty(\n" +
" ([Time].[1997].[Q4].[12],\n" +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Portsmouth].[Portsmouth Imported Beer],\n" +
" [Store].[All Stores].[USA].[WA].[Bellingham]))",
true);
assertBooleanExprReturns("IsEmpty(\n" +
" ([Time].[1997].[Q4].[11],\n" +
" [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Portsmouth].[Portsmouth Imported Beer],\n" +
" [Store].[All Stores].[USA].[WA].[Bellingham]))",
false);
// The empty set is neither EMPTY nor NULL.
// should give 0 as a result, not NULL and not EMPTY.
assertQueryReturns(
"WITH SET [empty set] AS '{}'\n" +
" MEMBER [Measures].[Set Size] AS 'Count([empty set])'\n" +
" MEMBER [Measures].[Set Size Is Empty] AS 'CASE WHEN IsEmpty([Measures].[Set Size]) THEN 1 ELSE 0 END '\n" +
"SELECT [Measures].[Set Size] on columns",
"");
assertQueryReturns(
"WITH SET [empty set] AS '{}'\n" +
"WITH MEMBER [Measures].[Set Size] AS 'Count([empty set])'\n" +
"SELECT [Measures].[Set Size] on columns",
"");
// Run time errors are BAD things. They should not occur
// in almost all cases. In fact there should be no
// logically formed MDX that generates them. An ERROR
// value in a cell though is perfectly legal - e.g. a
// divide by 0.
// E.g.
String foo = "WITH [Measures].[Ratio This Period to Previous] as\n" +
"'([Measures].[Sales],[Time].CurrentMember/([Measures].[Sales],[Time].CurrentMember.PrevMember)'\n" +
"SELECT [Measures].[Ratio This Period to Previous] ON COLUMNS,\n" +
"[Time].Members ON ROWS\n" +
"FROM ...";
// For the [Time].[All Time] row as well as the first
// year, first month etc, the PrevMember will evaluate to
// NULL, the tuple will evaluate to NULL and the division
// will implicitly convert the NULL to 0 and then evaluate
// to an ERROR value due to a divide by 0.
// This leads to another point: NULL and EMPTY values get
// implicitly converted to 0 when treated as numeric
// values for division and multiplication but for addition
// and subtraction, NULL is treated as NULL (5+NULL yields
// NULL).
// I have no idea about how EMPTY works. I.e. is does
// 5+EMPTY yield 5 or EMPTY or NULL or what?
// E.g.
String foo2 = "WITH MEMBER [Measures].[5 plus empty] AS\n" +
"'5+([Product].[All Products].[Ski boots],[Geography].[All Geography].[Hawaii])'\n" +
"SELECT [Measures].[5 plus empty] ON COLUMNS\n" +
"FROM ...";
// Does this yield EMPTY, 5, NULL or ERROR?
// Lastly, IS NULL and IS EMPTY are both legal and
// distinct. <<Object>> IS {<<Object>> | NULL} and
// <<Value>> IS EMPTY.
// E.g.
// a) [Time].CurrentMember.Parent IS [Time].[Year].[2004]
// is also a perfectly legal expression and better than
// [Time].CurrentMember.Parent.Name="2004".
// b) ([Measures].[Sales],[Time].FirstSibling) IS EMPTY is
// a legal expression.
// Microsoft's site says that the EMPTY value participates in 3 value
// logic e.g. TRUE AND EMPTY gives EMPTY, FALSE AND EMPTY gives FALSE.
// todo: test for this
}
public void testQueryWithoutValidMeasure() {
String query = "with\n" +
"member measures.[without VM] as ' [measures].[unit sales] '\n" +
"select {measures.[without VM] } on 0,\n" +
"[Warehouse].[Country].members on 1 from [warehouse and sales]\n";
String expectedResult = "Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[without VM]}\n" +
"Axis #2:\n" +
"{[Warehouse].[All Warehouses].[Canada]}\n" +
"{[Warehouse].[All Warehouses].[Mexico]}\n" +
"{[Warehouse].[All Warehouses].[USA]}\n" +
"Row #0: \n" +
"Row #1: \n" +
"Row #2: \n";
assertQueryReturns(query, fold(expectedResult));
}
/** Tests the <code>ValidMeasure</code> function. */
public void testValidMeasure() {
String query = "with\n" +
"member measures.[with VM] as 'validmeasure([measures].[unit sales])'\n" +
"select { measures.[with VM]} on 0,\n" +
"[Warehouse].[Country].members on 1 from [warehouse and sales]\n";
String expectedResult = "Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[with VM]}\n" +
"Axis #2:\n" +
"{[Warehouse].[All Warehouses].[Canada]}\n" +
"{[Warehouse].[All Warehouses].[Mexico]}\n" +
"{[Warehouse].[All Warehouses].[USA]}\n" +
"Row #0: 266,773\n" +
"Row #1: 266,773\n" +
"Row #2: 266,773\n";
assertQueryReturns(query, fold(expectedResult));
}
public void _testValidMeasureNonEmpty() {
// Note that [with VM2] is NULL where it needs to be - and therefore
// does not prevent NON EMPTY from eliminating empty rows.
assertQueryReturns(
"with set [Foo] as ' Crossjoin({[Time].Children}, {[Measures].[Warehouse Sales]}) '\n" +
" member [Measures].[with VM] as 'ValidMeasure([Measures].[Unit Sales])'\n" +
" member [Measures].[with VM2] as 'Iif(Count(Filter([Foo], not isempty([Measures].CurrentMember))) > 0, ValidMeasure([Measures].[Unit Sales]), NULL)'\n" +
"select NON EMPTY Crossjoin({[Time].Children}, {[Measures].[with VM2], [Measures].[Warehouse Sales]}) ON COLUMNS,\n" +
" NON EMPTY {[Warehouse].[All Warehouses].[USA].[WA].Children} ON ROWS\n" +
"from [Warehouse and Sales]\n" +
"where [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Good].[Good Light Beer]",
fold("Axis #0:\n" +
"{[Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Beer].[Good].[Good Light Beer]}\n" +
"Axis #1:\n" +
"{[Time].[1997].[Q1], [Measures].[with VM2]}\n" +
"{[Time].[1997].[Q1], [Measures].[Warehouse Sales]}\n" +
"{[Time].[1997].[Q2], [Measures].[with VM2]}\n" +
"{[Time].[1997].[Q2], [Measures].[Warehouse Sales]}\n" +
"{[Time].[1997].[Q3], [Measures].[with VM2]}\n" +
"{[Time].[1997].[Q4], [Measures].[with VM2]}\n" +
"Axis #2:\n" +
"{[Warehouse].[All Warehouses].[USA].[WA].[Seattle]}\n" +
"{[Warehouse].[All Warehouses].[USA].[WA].[Tacoma]}\n" +
"{[Warehouse].[All Warehouses].[USA].[WA].[Yakima]}\n" +
"Row #0: 26\n" +
"Row #0: 34.793\n" +
"Row #0: 25\n" +
"Row #0: \n" +
"Row #0: 36\n" +
"Row #0: 28\n" +
"Row #1: 26\n" +
"Row #1: \n" +
"Row #1: 25\n" +
"Row #1: 64.615\n" +
"Row #1: 36\n" +
"Row #1: 28\n" +
"Row #2: 26\n" +
"Row #2: 79.657\n" +
"Row #2: 25\n" +
"Row #2: \n" +
"Row #2: 36\n" +
"Row #2: 28\n"));
}
public void testValidMeasureTupleHasAnotherMember() {
String query = "with\n" +
"member measures.[with VM] as 'validmeasure(([measures].[unit sales],[customers].[all customers]))'\n" +
"select { measures.[with VM]} on 0,\n" +
"[Warehouse].[Country].members on 1 from [warehouse and sales]\n";
String expectedResult = "Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[with VM]}\n" +
"Axis #2:\n" +
"{[Warehouse].[All Warehouses].[Canada]}\n" +
"{[Warehouse].[All Warehouses].[Mexico]}\n" +
"{[Warehouse].[All Warehouses].[USA]}\n" +
"Row #0: 266,773\n" +
"Row #1: 266,773\n" +
"Row #2: 266,773\n";
assertQueryReturns(query, fold(expectedResult));
}
public void testValidMeasureDepends() {
String s12 = TestContext.allDimsExcept("[Measures]");
getTestContext().assertExprDependsOn(
"ValidMeasure([Measures].[Unit Sales])", s12);
String s11 = TestContext.allDimsExcept("[Measures]", "[Time]");
getTestContext().assertExprDependsOn(
"ValidMeasure(([Measures].[Unit Sales], [Time].[1997].[Q1]))", s11);
String s1 = TestContext.allDimsExcept("[Measures]");
getTestContext().assertExprDependsOn(
"ValidMeasure(([Measures].[Unit Sales], [Time].CurrentMember.Parent))",
s1);
}
public void testAncestor() {
Member member = executeSingletonAxis("Ancestor([Store].[USA].[CA].[Los Angeles],[Store Country])");
Assert.assertEquals("USA", member.getName());
assertAxisThrows("Ancestor([Store].[USA].[CA].[Los Angeles],[Promotions].[Promotion Name])",
"Error while executing query");
}
public void testAncestorNumeric() {
Member member = executeSingletonAxis("Ancestor([Store].[USA].[CA].[Los Angeles],1)");
Assert.assertEquals("CA", member.getName());
member = executeSingletonAxis("Ancestor([Store].[USA].[CA].[Los Angeles], 0)");
Assert.assertEquals("Los Angeles", member.getName());
final TestContext testContextRagged =
getTestContext().withCube("[Sales Ragged]");
member = testContextRagged.executeSingletonAxis("Ancestor([Store].[All Stores].[Vatican], 1)");
Assert.assertEquals("All Stores", member.getName());
member = testContextRagged.executeSingletonAxis("Ancestor([Store].[USA].[Washington], 1)");
Assert.assertEquals("USA", member.getName());
// complicated way to say "1".
member = testContextRagged.executeSingletonAxis("Ancestor([Store].[USA].[Washington], 7 * 6 - 41)");
Assert.assertEquals("USA", member.getName());
member = testContextRagged.executeSingletonAxis("Ancestor([Store].[All Stores].[Vatican], 2)");
Assert.assertNull("Ancestor at 2 must be null", member);
member = testContextRagged.executeSingletonAxis("Ancestor([Store].[All Stores].[Vatican], -5)");
Assert.assertNull("Ancestor at -5 must be null", member);
}
public void testAncestorHigher() {
Member member = executeSingletonAxis("Ancestor([Store].[USA],[Store].[Store City])");
Assert.assertNull(member); // MSOLAP returns null
}
public void testAncestorSameLevel() {
Member member = executeSingletonAxis("Ancestor([Store].[Canada],[Store].[Store Country])");
Assert.assertEquals("Canada", member.getName());
}
public void testAncestorWrongHierarchy() {
// MSOLAP gives error "Formula error - dimensions are not
// valid (they do not match) - in the Ancestor function"
assertAxisThrows("Ancestor([Gender].[M],[Store].[Store Country])",
"Error while executing query");
}
public void testAncestorAllLevel() {
Member member = executeSingletonAxis("Ancestor([Store].[USA].[CA],[Store].Levels(0))");
Assert.assertTrue(member.isAll());
}
public void testAncestorWithHiddenParent() {
final TestContext testContext =
getTestContext().withCube("[Sales Ragged]");
Member member = testContext.executeSingletonAxis(
"Ancestor([Store].[All Stores].[Israel].[Haifa], [Store].[Store Country])");
assertNotNull("Member must not be null.", member);
Assert.assertEquals("Israel", member.getName());
}
public void testAncestorDepends() {
getTestContext().assertExprDependsOn(
"Ancestor([Store].CurrentMember, [Store].[Store Country]).Name",
"{[Store]}");
getTestContext().assertExprDependsOn(
"Ancestor([Store].[All Stores].[USA], [Store].CurrentMember.Level).Name",
"{[Store]}");
getTestContext().assertExprDependsOn(
"Ancestor([Store].[All Stores].[USA], [Store].[Store Country]).Name",
"{}");
getTestContext().assertExprDependsOn(
"Ancestor([Store].CurrentMember, 2+1).Name", "{[Store]}");
}
public void testOrdinal() {
final TestContext testContext =
getTestContext().withCube("Sales Ragged");
Cell cell = testContext.executeExprRaw("[Store].[All Stores].[Vatican].ordinal");
assertEquals("Vatican is at level 1.", 1, ((Number)cell.getValue()).intValue());
cell = testContext.executeExprRaw("[Store].[All Stores].[USA].[Washington].ordinal");
assertEquals("Washington is at level 3.", 3, ((Number) cell.getValue()).intValue());
}
public void testClosingPeriodNoArgs() {
getTestContext().assertMemberExprDependsOn("ClosingPeriod()", "{[Time]}");
// MSOLAP returns [1997].[Q4], because [Time].CurrentMember =
// [1997].
Member member = executeSingletonAxis("ClosingPeriod()");
Assert.assertEquals("[Time].[1997].[Q4]", member.getUniqueName());
}
public void testClosingPeriodLevel() {
getTestContext().assertMemberExprDependsOn(
"ClosingPeriod([Time].[Year])", "{[Time]}");
getTestContext().assertMemberExprDependsOn(
"([Measures].[Unit Sales], ClosingPeriod([Time].[Month]))",
"{[Time]}");
Member member;
member = executeSingletonAxis("ClosingPeriod([Year])");
Assert.assertEquals("[Time].[1997]", member.getUniqueName());
member = executeSingletonAxis("ClosingPeriod([Quarter])");
Assert.assertEquals("[Time].[1997].[Q4]", member.getUniqueName());
member = executeSingletonAxis("ClosingPeriod([Month])");
Assert.assertEquals("[Time].[1997].[Q4].[12]", member.getUniqueName());
assertQueryReturns(
"with member [Measures].[Closing Unit Sales] as '([Measures].[Unit Sales], ClosingPeriod([Time].[Month]))'\n" +
"select non empty {[Measures].[Closing Unit Sales]} on columns,\n" +
" {Descendants([Time].[1997])} on rows\n" +
"from [Sales]",
fold(
"Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[Closing Unit Sales]}\n" +
"Axis #2:\n" +
"{[Time].[1997]}\n" +
"{[Time].[1997].[Q1]}\n" +
"{[Time].[1997].[Q1].[1]}\n" +
"{[Time].[1997].[Q1].[2]}\n" +
"{[Time].[1997].[Q1].[3]}\n" +
"{[Time].[1997].[Q2]}\n" +
"{[Time].[1997].[Q2].[4]}\n" +
"{[Time].[1997].[Q2].[5]}\n" +
"{[Time].[1997].[Q2].[6]}\n" +
"{[Time].[1997].[Q3]}\n" +
"{[Time].[1997].[Q3].[7]}\n" +
"{[Time].[1997].[Q3].[8]}\n" +
"{[Time].[1997].[Q3].[9]}\n" +
"{[Time].[1997].[Q4]}\n" +
"{[Time].[1997].[Q4].[10]}\n" +
"{[Time].[1997].[Q4].[11]}\n" +
"{[Time].[1997].[Q4].[12]}\n" +
"Row #0: 26,796\n" +
"Row #1: 23,706\n" +
"Row #2: 21,628\n" +
"Row #3: 20,957\n" +
"Row #4: 23,706\n" +
"Row #5: 21,350\n" +
"Row #6: 20,179\n" +
"Row #7: 21,081\n" +
"Row #8: 21,350\n" +
"Row #9: 20,388\n" +
"Row #10: 23,763\n" +
"Row #11: 21,697\n" +
"Row #12: 20,388\n" +
"Row #13: 26,796\n" +
"Row #14: 19,958\n" +
"Row #15: 25,270\n" +
"Row #16: 26,796\n"));
assertQueryReturns(
"with member [Measures].[Closing Unit Sales] as '([Measures].[Unit Sales], ClosingPeriod([Time].[Month]))'\n" +
"select {[Measures].[Unit Sales], [Measures].[Closing Unit Sales]} on columns,\n" +
" {[Time].[1997], [Time].[1997].[Q1], [Time].[1997].[Q1].[1], [Time].[1997].[Q1].[3], [Time].[1997].[Q4].[12]} on rows\n" +
"from [Sales]",
fold(
"Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[Unit Sales]}\n" +
"{[Measures].[Closing Unit Sales]}\n" +
"Axis #2:\n" +
"{[Time].[1997]}\n" +
"{[Time].[1997].[Q1]}\n" +
"{[Time].[1997].[Q1].[1]}\n" +
"{[Time].[1997].[Q1].[3]}\n" +
"{[Time].[1997].[Q4].[12]}\n" +
"Row #0: 266,773\n" +
"Row #0: 26,796\n" +
"Row #1: 66,291\n" +
"Row #1: 23,706\n" +
"Row #2: 21,628\n" +
"Row #2: 21,628\n" +
"Row #3: 23,706\n" +
"Row #3: 23,706\n" +
"Row #4: 26,796\n" +
"Row #4: 26,796\n"));
}
public void testClosingPeriodLevelNotInTimeFails() {
assertAxisThrows("ClosingPeriod([Store].[Store City])",
"The <level> and <member> arguments to ClosingPeriod must be from the same hierarchy. The level was from '[Store]' but the member was from '[Time]'");
}
public void testClosingPeriodMember() {
if (false) {
// This test is mistaken. Valid forms are ClosingPeriod(<level>)
// and ClosingPeriod(<level>, <member>), but not
// ClosingPeriod(<member>)
Member member = executeSingletonAxis("ClosingPeriod([USA])");
Assert.assertEquals("WA", member.getName());
}
}
public void testClosingPeriodMemberLeaf() {
Member member;
if (false) {
// This test is mistaken. Valid forms are ClosingPeriod(<level>)
// and ClosingPeriod(<level>, <member>), but not
// ClosingPeriod(<member>)
member = executeSingletonAxis("ClosingPeriod([Time].[1997].[Q3].[8])");
Assert.assertNull(member);
} else if (isDefaultNullMemberRepresentation()) {
assertQueryReturns(
"with member [Measures].[Foo] as ' ClosingPeriod().uniquename '\n" +
"select {[Measures].[Foo]} on columns,\n" +
" {[Time].[1997],\n" +
" [Time].[1997].[Q2],\n" +
" [Time].[1997].[Q2].[4]} on rows\n" +
"from Sales",
fold(
"Axis #0:\n" +
"{}\n" +
"Axis #1:\n" +
"{[Measures].[Foo]}\n" +
"Axis #2:\n" +
"{[Time].[1997]}\n" +
"{[Time].[1997].[Q2]}\n" +
"{[Time].[1997].[Q2].[4]}\n" +
"Row #0: [Time].[1997].[Q4]\n" +
"Row #1: [Time].[1997].[Q2].[6]\n" +
"Row #2: [Time].[#null]\n" + // MSAS returns "" here.
""));
}
}
public void testClosingPeriod() {
getTestContext().assertMemberExprDependsOn(
"ClosingPeriod([Time].[Month], [Time].CurrentMember)", "{[Time]}");
String s1 = TestContext.allDimsExcept("[Measures]");
getTestContext().assertExprDependsOn(
"(([Measures].[Store Sales], ClosingPeriod([Time].[Month], [Time].CurrentMember)) - ([Measures].[Store Cost], ClosingPeriod([Time].[Month], [Time].CurrentMember)))",
s1);
getTestContext().assertMemberExprDependsOn(
"ClosingPeriod([Time].[Month], [Time].[1997].[Q3])", "{}");
assertAxisReturns("ClosingPeriod([Time].[Year], [Time].[1997].[Q3])",
"");
assertAxisReturns("ClosingPeriod([Time].[Quarter], [Time].[1997].[Q3])",
"[Time].[1997].[Q3]");
assertAxisReturns("ClosingPeriod([Time].[Month], [Time].[1997].[Q3])",
"[Time].[1997].[Q3].[9]");
assertAxisReturns("ClosingPeriod([Time].[Quarter], [Time].[1997])",
"[Time].[1997].[Q4]");
assertAxisReturns("ClosingPeriod([Time].[Year], [Time].[1997])",
"[Time].[1997]");
assertAxisReturns("ClosingPeriod([Time].[Month], [Time].[1997])",
"[Time].[1997].[Q4].[12]");
// leaf member
assertAxisReturns("ClosingPeriod([Time].[Year], [Time].[1997].[Q3].[8])",
"");
assertAxisReturns("ClosingPeriod([Time].[Quarter], [Time].[1997].[Q3].[8])",
"");