You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by "TaoZex (via GitHub)" <gi...@apache.org> on 2023/01/28 13:25:23 UTC

[GitHub] [hive] TaoZex commented on a diff in pull request #3982: HIVE-26957: Add convertCharset(s, from, to) function

TaoZex commented on code in PR #3982:
URL: https://github.com/apache/hive/pull/3982#discussion_r1089731882


##########
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFConvertCharset.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.udf.generic;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CodingErrorAction;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping;
+
+@Description(name = "convertCharset", value = "_FUNC_(str, str, str) - Converts the first argument from the second argument character set to the third argument character set", extended =
+    "Possible options for the character set are 'US-ASCII', 'ISO-8859-1',\n"
+        + "'UTF-8', 'UTF-16BE', 'UTF-16LE', and 'UTF-16'. If either argument\n"
+        + "is null, the result will also be null") public class GenericUDFConvertCharset extends GenericUDF {
+  private transient CharsetEncoder encoder = null;
+  private transient CharsetDecoder decoder = null;
+  private transient PrimitiveObjectInspector stringOI = null;
+  private transient PrimitiveObjectInspector fromCharsetOI = null;
+  private transient PrimitiveObjectInspector toCharsetOI = null;
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 3) {
+      throw new UDFArgumentLengthException("ConvertCharset() requires exactly three arguments");
+    }
+
+    if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE
+        || PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP
+        != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(
+        ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory())) {
+      throw new UDFArgumentTypeException(0, "The first argument to ConvertCharset() must be a string/varchar");
+    }
+
+    stringOI = (PrimitiveObjectInspector) arguments[0];
+
+    if (arguments[1].getCategory() != ObjectInspector.Category.PRIMITIVE
+        || PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP
+        != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(
+        ((PrimitiveObjectInspector) arguments[1]).getPrimitiveCategory())) {
+      throw new UDFArgumentTypeException(1, "The second argument to ConvertCharset() must be a string/varchar");
+    }
+
+    fromCharsetOI = (PrimitiveObjectInspector) arguments[1];
+
+    if (arguments[2].getCategory() != ObjectInspector.Category.PRIMITIVE
+        || PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP
+        != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(
+        ((PrimitiveObjectInspector) arguments[2]).getPrimitiveCategory())) {
+      throw new UDFArgumentTypeException(2, "The third argument to ConvertCharset() must be a string/varchar");
+    }

Review Comment:
   Thanks for your reply, I have modified the code according to the suggestion.
   But I ran into some problems in the test, could you give me some advice?
   http://ci.hive.apache.org/blue/organizations/jenkins/hive-precommit/detail/PR-3982/5/pipeline/871



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org