veghlaci05 commented on code in PR #4696: URL: https://github.com/apache/hive/pull/4696#discussion_r1417192870
########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToArray.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.SettableUDF; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; + +@Description(name = "toArray", value = "_FUNC_(x) - converts it's parameter to _FUNC_" + + "Currently only null literal is supported.") +public class GenericUDFToArray extends GenericUDF implements SettableUDF { + private ListTypeInfo typeInfo; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + return typeInfo.createObjectInspector(); Review Comment: The code in `typeInfo.createObjectInspector()` should go here directly. Same for the other two UDF functions. I see no benefit of implementing this method in all the TypeInfo subclasses, while it is needed in only 3 of them. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToArray.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.SettableUDF; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; + +@Description(name = "toArray", value = "_FUNC_(x) - converts it's parameter to _FUNC_" + + "Currently only null literal is supported.") +public class GenericUDFToArray extends GenericUDF implements SettableUDF { + private ListTypeInfo typeInfo; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + return typeInfo.createObjectInspector(); + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + return null; + } + + @Override + public String getDisplayString(String[] children) { + StringBuilder sb = new StringBuilder(); + sb.append("toArray("); + for (int i = 0; i < children.length; ++i) { + if (i != 0) { + sb.append(","); + } + sb.append(children[i]); + } Review Comment: You may replace it with `String.join()`. Same for other two classes. ########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java: ########## @@ -203,53 +201,56 @@ private static ASTNode createNullField(RelDataType fieldType) { return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node(); } + return createCastNull(fieldType); + } + + private static ASTNode createCastNull(RelDataType fieldType) { Review Comment: This method is very small and used only in `emptyPlan()`. I think it should be eliminated and the content should be moved to `emptyPlan()` ########## ql/src/test/queries/clientpositive/cast_null_to_complex.q: ########## @@ -0,0 +1,13 @@ +SET hive.cli.print.header=true; + +explain cbo +select cast(null as map<int, string>), cast(null as array<map<int, string>>), cast(null as int), cast(null as struct<f1:array<map<int, string>>, f2:struct<a:double, b:string>>); +explain +select cast(null as map<int, string>), cast(null as array<map<int, string>>), cast(null as int), cast(null as struct<f1:array<map<int, string>>, f2:struct<a:double, b:string>>); +select cast(null as map<int, string>), cast(null as array<map<int, string>>), cast(null as int), cast(null as struct<f1:array<map<int, string>>, f2:struct<a:double, b:string>>); + + +create table t1 as +select cast(null as map<int, string>), cast(null as array<map<int, string>>), cast(null as int), cast(null as struct<f1:array<map<int, string>>, f2:struct<a:double, b:string>>); Review Comment: Is this select equivalent with a table having the same fields? I mean in the select the cast(null as <type>) is already there. There should be one more test where a table is created with these complex fields, and the table is referred in the CTAS. That would test if the CAST expressions are generated correctly in the background. Also, the original issue reported with a sub-query, so that case also should be covered. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToArray.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.SettableUDF; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; + +@Description(name = "toArray", value = "_FUNC_(x) - converts it's parameter to _FUNC_" + + "Currently only null literal is supported.") +public class GenericUDFToArray extends GenericUDF implements SettableUDF { + private ListTypeInfo typeInfo; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + return typeInfo.createObjectInspector(); + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + return null; Review Comment: As you noted in the PR description, I think it worth adding here a short description that this is just a basic implementation which will evaluate all passed values to null, and why it is needed. ########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java: ########## @@ -203,53 +201,56 @@ private static ASTNode createNullField(RelDataType fieldType) { return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node(); } + return createCastNull(fieldType); + } + + private static ASTNode createCastNull(RelDataType fieldType) { + ASTNode astNode = convertType(fieldType); + return ASTBuilder.construct(HiveParser.TOK_FUNCTION, "TOK_FUNCTION") + .add(astNode) + .add(HiveParser.TOK_NULL, "TOK_NULL") + .node(); + } + + public static ASTNode convertType(RelDataType fieldType) { Review Comment: Why it is public? It is used only in `ASTConverter` and `TestASTConverter` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
