Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NullToEmptyBag |
|
| 4.0;4 |
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.bags; | |
18 | ||
19 | import java.io.IOException; | |
20 | ||
21 | import org.apache.pig.EvalFunc; | |
22 | import org.apache.pig.data.BagFactory; | |
23 | import org.apache.pig.data.DataBag; | |
24 | import org.apache.pig.data.Tuple; | |
25 | import org.apache.pig.impl.logicalLayer.schema.Schema; | |
26 | ||
27 | /** | |
28 | * UDF that, if the input is null, returns an empty bag; otherwise, | |
29 | * returns the input bag unchanged. | |
30 | */ | |
31 | 856 | public class NullToEmptyBag extends EvalFunc<DataBag> |
32 | { | |
33 | @Override | |
34 | public DataBag exec(Tuple tuple) throws IOException | |
35 | { | |
36 | 9 | if (tuple.size() == 0 || tuple.get(0) == null) |
37 | 3 | return BagFactory.getInstance().newDefaultBag(); |
38 | 6 | Object o = tuple.get(0); |
39 | 6 | if (o instanceof DataBag) |
40 | 6 | return (DataBag)o; |
41 | else | |
42 | 0 | throw new IllegalArgumentException("expected a null or a bag"); |
43 | } | |
44 | ||
45 | @Override | |
46 | public Schema outputSchema(Schema input) | |
47 | { | |
48 | 135 | return input; |
49 | } | |
50 | } |