Review Board 1.7.22


PIG-3269 In operator support

Review Request #10337 - Created April 8, 2013 and submitted

Cheolsoo Park
PIG-3269
Reviewers
pig
pig-git
Add SQL IN operator to Pig. Please see:
https://issues.apache.org/jira/browse/PIG-3269
Added two new test suites:
- TestIn.java
- TestInUdf.java

Also ran ant test-commit.
src/org/apache/pig/builtin/IN.java
New File

    
   
1
/*

    
   
2
 * Licensed to the Apache Software Foundation (ASF) under one

    
   
3
 * or more contributor license agreements.  See the NOTICE file

    
   
4
 * distributed with this work for additional information

    
   
5
 * regarding copyright ownership.  The ASF licenses this file

    
   
6
 * to you under the Apache License, Version 2.0 (the

    
   
7
 * "License"); you may not use this file except in compliance

    
   
8
 * with the License.  You may obtain a copy of the License at

    
   
9
 *

    
   
10
 *     http://www.apache.org/licenses/LICENSE-2.0

    
   
11
 *

    
   
12
 * Unless required by applicable law or agreed to in writing, software

    
   
13
 * distributed under the License is distributed on an "AS IS" BASIS,

    
   
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

    
   
15
 * See the License for the specific language governing permissions and

    
   
16
 * limitations under the License.

    
   
17
 */

    
   
18
package org.apache.pig.builtin;

    
   
19

   

    
   
20
import java.io.IOException;

    
   
21

   

    
   
22
import org.apache.pig.EvalFunc;

    
   
23
import org.apache.pig.backend.executionengine.ExecException;

    
   
24
import org.apache.pig.data.DataType;

    
   
25
import org.apache.pig.data.Tuple;

    
   
26
import org.apache.pig.impl.logicalLayer.schema.Schema;

    
   
27

   

    
   
28
/**

    
   
29
 * IN EvalFunc mimics the behavior of SQL IN operator. It takes more than or

    
   
30
 * equal to two arguments and compares the first argument against the rest one

    
   
31
 * by one. If it finds a match, true is returned; otherwise, false is returned.

    
   
32
 * If the first argument is null, it always returns false.

    
   
33
 */

    
   
34
public class IN extends EvalFunc<Boolean> {

    
   
35
    @Override

    
   
36
    public Boolean exec(Tuple input) throws IOException {

    
   
37
        if (input.size() < 2) {

    
   
38
            throw new ExecException("Invalid number of args");

    
   
39
        }

    
   
40

   

    
   
41
        Object expr = input.get(0);

    
   
42
        if (expr == null) {

    
   
43
            // If 1st argument (lhs operand of IN operator) is null, always

    
   
44
            // return false.

    
   
45
            return false;

    
   
46
        }

    
   
47

   

    
   
48
        for (int i = 1; i < input.size(); i++) {

    
   
49
            if (expr.equals(input.get(i))) {

    
   
50
                return true;

    
   
51
            }

    
   
52
        }

    
   
53
        return false;

    
   
54
    }

    
   
55

   

    
   
56
    @Override

    
   
57
    public Schema outputSchema(Schema input) {

    
   
58
        return new Schema(new Schema.FieldSchema(null, DataType.BOOLEAN));

    
   
59
    }

    
   
60
};
src/org/apache/pig/parser/AstPrinter.g
Revision 5d83bea New Change
 
src/org/apache/pig/parser/AstValidator.g
Revision b85e238 New Change
 
src/org/apache/pig/parser/LogicalPlanGenerator.g
Revision 26f13f6 New Change
 
src/org/apache/pig/parser/QueryLexer.g
Revision c7f9fdd New Change
 
src/org/apache/pig/parser/QueryParser.g
Revision 538621b New Change
 
test/commit-tests
Revision 5081fbc New Change
 
test/org/apache/pig/builtin/TestInUdf.java
New File
 
test/org/apache/pig/test/TestIn.java
New File
 
  1. src/org/apache/pig/builtin/IN.java: Loading...
  2. src/org/apache/pig/parser/AstPrinter.g: Loading...
  3. src/org/apache/pig/parser/AstValidator.g: Loading...
  4. src/org/apache/pig/parser/LogicalPlanGenerator.g: Loading...
  5. src/org/apache/pig/parser/QueryLexer.g: Loading...
  6. src/org/apache/pig/parser/QueryParser.g: Loading...
  7. test/commit-tests: Loading...
  8. test/org/apache/pig/builtin/TestInUdf.java: Loading...
  9. test/org/apache/pig/test/TestIn.java: Loading...