gengliangwang commented on code in PR #59926: URL: https://github.com/apache/spark/pull/59926#discussion_r4418528345
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/IndeterminateCollationCheckSuite.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, DropField, Length, UpdateFields, Upper} +import org.apache.spark.sql.types.{DataType, IndeterminateStringType, IntegerType, StringType, StructType} + +/** The indeterminate-collation check of [[CollationTypeCoercion]]. */ +class IndeterminateCollationCheckSuite extends SparkFunSuite { + + private val struct = AttributeReference( + "s", new StructType().add("a", IntegerType).add("b", StringType))() + + private val indeterminate = AttributeReference("x", IndeterminateStringType)() + + test("does not ask the field operations of UpdateFields for a data type") { + val drop = new CountingDropField("b") + val update = UpdateFields(struct, Seq(drop)) + assert(CollationTypeCoercion(update) eq update) + assert(drop.dataTypeRequests == 0) Review Comment: Done in 5426b635d98. The test now applies the rule to every node with `transformUp`, and the `UpdateFields` has a `WithField`, a field operation and an `UpdateNestedField` with a nested field operation. Neither field operation is asked for a data type. With the old check, the test fails (`1 did not equal 0`). ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCoercion.scala: ########## @@ -507,8 +507,11 @@ object CollationTypeCoercion extends SQLConfHelper { case _: Throwable => NullType } - expression.children.exists(child => - shouldFailWithIndeterminateCollation(expression, getDataTypeSafe(child))) + // Most expressions can contain indeterminate collation, so their children's types are not + // needed. Some of those types are an internal error to build and swallow, such as the type of + // a WithField or DropField operation of an UpdateFields. Review Comment: Done, I took your wording. ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CollationTypeCoercionSuite.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, DropField, Length, UpdateFields, Upper} +import org.apache.spark.sql.types.{DataType, IndeterminateStringType, IntegerType, StringType, StructType} + +/** The indeterminate-collation check of [[CollationTypeCoercion]]. */ +class IndeterminateCollationCheckSuite extends SparkFunSuite { + + private val struct = AttributeReference( + "s", new StructType().add("a", IntegerType).add("b", StringType))() + + private val indeterminate = AttributeReference("x", IndeterminateStringType)() + + test("does not ask the field operations of UpdateFields for a data type") { + val drop = new CountingDropField("b") + val update = UpdateFields(struct, Seq(drop)) + assert(CollationTypeCoercion(update) eq update) + assert(drop.dataTypeRequests == 0) + } + + test("fails an expression that does not accept an indeterminate collation input") { + checkError( + exception = intercept[AnalysisException] { + CollationTypeCoercion(Upper(indeterminate)) + }, + condition = "INDETERMINATE_COLLATION_IN_EXPRESSION", + parameters = Map("expr" -> "\"upper(x)\"")) Review Comment: Added `fails when only a later child has an indeterminate collation` with `EqualTo(Literal("a"), indeterminate)`. ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/IndeterminateCollationCheckSuite.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, DropField, Length, UpdateFields, Upper} +import org.apache.spark.sql.types.{DataType, IndeterminateStringType, IntegerType, StringType, StructType} + +/** The indeterminate-collation check of [[CollationTypeCoercion]]. */ +class IndeterminateCollationCheckSuite extends SparkFunSuite { + + private val struct = AttributeReference( + "s", new StructType().add("a", IntegerType).add("b", StringType))() + + private val indeterminate = AttributeReference("x", IndeterminateStringType)() + + test("does not ask the field operations of UpdateFields for a data type") { + val drop = new CountingDropField("b") + val update = UpdateFields(struct, Seq(drop)) + assert(CollationTypeCoercion(update) eq update) + assert(drop.dataTypeRequests == 0) + } + + test("fails an expression that does not accept an indeterminate collation input") { + checkError( + exception = intercept[AnalysisException] { + CollationTypeCoercion(Upper(indeterminate)) + }, + condition = "INDETERMINATE_COLLATION_IN_EXPRESSION", + parameters = Map("expr" -> "\"upper(x)\"")) + } + + test("keeps an expression that accepts an indeterminate collation input") { + val length = Length(indeterminate) + assert(CollationTypeCoercion(length) eq length) + } + + /** A [[DropField]] that counts the requests for its data type, which it does not have. */ + private class CountingDropField(name: String) extends DropField(name) { Review Comment: Replaced it with a test-only `CountingFieldOperation` case class that extends `StructFieldsOperation with LeafLike[Expression]`. ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/IndeterminateCollationCheckSuite.scala: ########## @@ -0,0 +1,63 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, DropField, Length, UpdateFields, Upper} +import org.apache.spark.sql.types.{DataType, IndeterminateStringType, IntegerType, StringType, StructType} + +/** The indeterminate-collation check of [[CollationTypeCoercion]]. */ +class IndeterminateCollationCheckSuite extends SparkFunSuite { Review Comment: Renamed to `CollationTypeCoercionSuite`. -- 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]
