Review Board 1.7.22


HIVE-2005 Implement BETWEEN operator

Review Request #3008 - Created Dec. 5, 2011 and submitted

Navis Ryu
trunk
HIVE-2005
Reviewers
hive
cws, jvs
hive-git
Revised version using 'between' UDF
added new test : udf_between.q
ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
Diff Revision 1 Diff Revision 4
[20] 15 lines
[+20]
16
 * limitations under the License.
16
 * limitations under the License.
17
 */
17
 */
18

    
   
18

   
19
package org.apache.hadoop.hive.ql.exec;
19
package org.apache.hadoop.hive.ql.exec;
20

    
   
20

   

    
   
21
import java.io.InputStream;
21
import java.lang.reflect.Method;
22
import java.lang.reflect.Method;

    
   
23
import java.net.URL;
22
import java.util.ArrayList;
24
import java.util.ArrayList;
23
import java.util.HashMap;
25
import java.util.HashMap;
24
import java.util.HashSet;
26
import java.util.HashSet;
25
import java.util.LinkedHashMap;
27
import java.util.LinkedHashMap;
26
import java.util.List;
28
import java.util.List;
27
import java.util.Map;
29
import java.util.Map;
28
import java.util.Set;
30
import java.util.Set;
29
import java.util.TreeSet;
31
import java.util.TreeSet;
30
import java.util.regex.Pattern;
32
import java.util.regex.Pattern;
31
import java.util.regex.PatternSyntaxException;
33
import java.util.regex.PatternSyntaxException;
32

    
   
34

   

    
   
35
import javax.xml.parsers.DocumentBuilder;

    
   
36
import javax.xml.parsers.DocumentBuilderFactory;

    
   
37

   
33
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.Log;
34
import org.apache.commons.logging.LogFactory;
39
import org.apache.commons.logging.LogFactory;
35
import org.apache.hadoop.hive.ql.metadata.HiveException;
40
import org.apache.hadoop.hive.ql.metadata.HiveException;
36
import org.apache.hadoop.hive.ql.parse.SemanticException;
41
import org.apache.hadoop.hive.ql.parse.SemanticException;
37
import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
42
import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
[+20] [20] 173 lines
[+20]
211
import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
216
import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
212
import org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo;
217
import org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo;
213
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
218
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
214
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
219
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
215
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
220
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;

    
   
221
import org.apache.hadoop.io.IOUtils;
216
import org.apache.hadoop.util.ReflectionUtils;
222
import org.apache.hadoop.util.ReflectionUtils;
217

    
   
223

   

    
   
224
import org.w3c.dom.Document;

    
   
225
import org.w3c.dom.Element;

    
   
226
import org.w3c.dom.Node;

    
   
227
import org.w3c.dom.NodeList;
218

    
   
228

   
219
/**
229
/**
220
 * FunctionRegistry.
230
 * FunctionRegistry.
221
 */
231
 */
222
public final class FunctionRegistry {
232
public final class FunctionRegistry {
[+20] [20] 907 lines
[+20] [+] public static boolean isOpNot(ExprNodeDesc desc) {
1130
  public static boolean isOpPositive(ExprNodeDesc desc) {
1140
  public static boolean isOpPositive(ExprNodeDesc desc) {
1131
    Class<? extends UDF> udfClass = getUDFClassFromExprDesc(desc);
1141
    Class<? extends UDF> udfClass = getUDFClassFromExprDesc(desc);
1132
    return UDFOPPositive.class == udfClass;
1142
    return UDFOPPositive.class == udfClass;
1133
  }
1143
  }
1134

    
   
1144

   

    
   
1145
  /**

    
   
1146
   * Registers the appropriate kind of temporary function based on a class's

    
   
1147
   * type.

    
   
1148
   *

    
   
1149
   * @param functionName name under which to register function

    
   
1150
   *

    
   
1151
   * @param udfClass class implementing UD[A|T]F

    
   
1152
   *

    
   
1153
   * @return true if udfClass's type was recognized (so registration

    
   
1154
   * succeeded); false otherwise

    
   
1155
   */

    
   
1156
  public static boolean registerTemporaryFunction(

    
   
1157
    String functionName, Class<?> udfClass) {

    
   
1158

   

    
   
1159
    if (UDF.class.isAssignableFrom(udfClass)) {

    
   
1160
      FunctionRegistry.registerTemporaryUDF(

    
   
1161
        functionName, (Class<? extends UDF>) udfClass, false);

    
   
1162
    } else if (GenericUDF.class.isAssignableFrom(udfClass)) {

    
   
1163
      FunctionRegistry.registerTemporaryGenericUDF(

    
   
1164
        functionName, (Class<? extends GenericUDF>) udfClass);

    
   
1165
    } else if (GenericUDTF.class.isAssignableFrom(udfClass)) {

    
   
1166
      FunctionRegistry.registerTemporaryGenericUDTF(

    
   
1167
        functionName, (Class<? extends GenericUDTF>) udfClass);

    
   
1168
    } else if (UDAF.class.isAssignableFrom(udfClass)) {

    
   
1169
      FunctionRegistry.registerTemporaryUDAF(

    
   
1170
        functionName, (Class<? extends UDAF>) udfClass);

    
   
1171
    } else if (GenericUDAFResolver.class.isAssignableFrom(udfClass)) {

    
   
1172
      FunctionRegistry.registerTemporaryGenericUDAF(

    
   
1173
        functionName, (GenericUDAFResolver)

    
   
1174
        ReflectionUtils.newInstance(udfClass, null));

    
   
1175
    } else {

    
   
1176
      return false;

    
   
1177
    }

    
   
1178
    return true;

    
   
1179
  }

    
   
1180

   

    
   
1181
  /**

    
   
1182
   * Registers Hive functions from a plugin jar, using metadata from

    
   
1183
   * the jar's META-INF/class-info.xml.

    
   
1184
   *

    
   
1185
   * @param jarLocation URL for reading jar file

    
   
1186
   *

    
   
1187
   * @param classLoader classloader to use for loading function classes

    
   
1188
   */

    
   
1189
  public static void registerFunctionsFromPluginJar(

    
   
1190
    URL jarLocation,

    
   
1191
    ClassLoader classLoader) throws Exception {

    
   
1192

   

    
   
1193
    URL url = new URL("jar:" + jarLocation + "!/META-INF/class-info.xml");

    
   
1194
    InputStream inputStream = null;

    
   
1195
    try {

    
   
1196
      inputStream = url.openStream();

    
   
1197
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    
   
1198
      DocumentBuilder docBuilder = dbf.newDocumentBuilder();

    
   
1199
      Document doc = docBuilder.parse(inputStream);

    
   
1200
      Element root = doc.getDocumentElement();

    
   
1201
      if (!root.getTagName().equals("ClassList")) {

    
   
1202
        return;

    
   
1203
      }

    
   
1204
      NodeList children = root.getElementsByTagName("Class");

    
   
1205
      for (int i = 0; i < children.getLength(); ++i) {

    
   
1206
        Element child = (Element) children.item(i);

    
   
1207
        String javaName = child.getAttribute("javaname");

    
   
1208
        String sqlName = child.getAttribute("sqlname");

    
   
1209
        Class<?> udfClass = Class.forName(javaName, true, classLoader);

    
   
1210
        boolean registered = registerTemporaryFunction(sqlName, udfClass);

    
   
1211
        if (!registered) {

    
   
1212
          throw new RuntimeException(

    
   
1213
            "Class " + udfClass + " is not a Hive function implementation");

    
   
1214
        }

    
   
1215
      }

    
   
1216
    } finally {

    
   
1217
      IOUtils.closeStream(inputStream);

    
   
1218
    }

    
   
1219
  }

    
   
1220

   
1135
  private FunctionRegistry() {
1221
  private FunctionRegistry() {
1136
    // prevent instantiation
1222
    // prevent instantiation
1137
  }
1223
  }
1138
}
1224
}
ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g
Diff Revision 1 Diff Revision 4
 
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBetween.java
Diff Revision 1 Diff Revision 4
 
ql/src/test/queries/clientpositive/udf_between.q
Diff Revision 1 Diff Revision 4
 
ql/src/test/results/clientpositive/show_functions.q.out
Diff Revision 1 Diff Revision 4
 
ql/src/test/results/clientpositive/udf_between.q.out
Diff Revision 1 Diff Revision 4
 
  1. ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java: Loading...
  2. ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g: Loading...
  3. ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBetween.java: Loading...
  4. ql/src/test/queries/clientpositive/udf_between.q: Loading...
  5. ql/src/test/results/clientpositive/show_functions.q.out: Loading...
  6. ql/src/test/results/clientpositive/udf_between.q.out: Loading...