Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ASSERT |
|
| 5.0;5 |
1 | /* | |
2 | * Copyright 2010 LinkedIn, Inc | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
5 | * use this file except in compliance with the License. You may obtain a copy of | |
6 | * the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
13 | * License for the specific language governing permissions and limitations under | |
14 | * the License. | |
15 | */ | |
16 | ||
17 | package datafu.pig.util; | |
18 | ||
19 | import java.io.IOException; | |
20 | ||
21 | import org.apache.pig.FilterFunc; | |
22 | import org.apache.pig.data.Tuple; | |
23 | ||
24 | /** | |
25 | * Asserts some boolean. There is a unary and a binary version. | |
26 | * <p> | |
27 | * The unary version just takes a boolean, and throws out a generic exception message when the | |
28 | * assertion is violated. | |
29 | * <p> | |
30 | * The binary version takes a String as a second argument and throws that out when the assertion | |
31 | * is violated. | |
32 | * <p> | |
33 | * Unfortunately, the pig interpreter doesn't recognize boolean expressions nested as function | |
34 | * arguments, so this has reverted to C-style booleans. That is, the first argument should be | |
35 | * an integer. 0 for false, anything else for true. | |
36 | * <p> | |
37 | * Example: | |
38 | * <pre> | |
39 | * {@code | |
40 | * FILTER members BY ASSERT( (member_id < 0 ? 1 : 0), 'This should always fail, as nobody has a negative member_id' ); | |
41 | * } | |
42 | * </pre></p> | |
43 | */ | |
44 | 5600 | public class ASSERT extends FilterFunc |
45 | { | |
46 | @Override | |
47 | public Boolean exec(Tuple tuple) | |
48 | throws IOException | |
49 | { | |
50 | 4 | if ((Integer) tuple.get(0) == 0) { |
51 | 2 | if (tuple.size() > 1) { |
52 | 1 | throw new IOException("Assertion violated: " + tuple.get(1).toString()); |
53 | } | |
54 | else { | |
55 | 1 | throw new IOException("Assertion violated. What assertion, I do not know, but it was officially violated."); |
56 | } | |
57 | } | |
58 | else { | |
59 | 2 | return true; |
60 | } | |
61 | } | |
62 | } |