MaxGekk commented on code in PR #45064: URL: https://github.com/apache/spark/pull/45064#discussion_r1487518940
########## sql/core/src/test/scala/org/apache/spark/sql/CollationSuite.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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 + +import org.apache.spark.SparkException +import org.apache.spark.sql.catalyst.util.CollationFactory +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.StringType + +class CollationSuite extends QueryTest with SharedSparkSession { + test("collate returns proper type") { + Seq("ucs_basic", "ucs_basic_lcase", "unicode", "unicode_ci").foreach { collationName => + checkAnswer(sql(s"select 'aaa' collate '$collationName'"), Row("aaa")) + val collationId = CollationFactory.collationNameToId(collationName) + assert(sql(s"select 'aaa' collate '$collationName'").schema(0).dataType + == StringType(collationId)) + } + } + + test("collation name is case insensitive") { + Seq("uCs_BasIc", "uCs_baSic_Lcase", "uNicOde", "UNICODE_ci").foreach { collationName => + checkAnswer(sql(s"select 'aaa' collate '$collationName'"), Row("aaa")) + val collationId = CollationFactory.collationNameToId(collationName) + assert(sql(s"select 'aaa' collate '$collationName'").schema(0).dataType + == StringType(collationId)) + } + } + + test("collation expression returns name of collation") { + Seq("ucs_basic", "ucs_basic_lcase", "unicode", "unicode_ci").foreach { collationName => + checkAnswer( + sql(s"select collation('aaa' collate '$collationName')"), Row(collationName.toUpperCase())) + } + } + + test("collate function syntax") { + assert(sql(s"select collate('aaa', 'ucs_basic')").schema(0).dataType == StringType(0)) + assert(sql(s"select collate('aaa', 'ucs_basic_lcase')").schema(0).dataType == StringType(1)) + } + + test("collate function syntax invalid args") { + Seq("'aaa','a','b'", "'aaa'").foreach(args => { + checkError( + exception = intercept[AnalysisException] { + sql(s"select collate($args)") + }, + errorClass = "WRONG_NUM_ARGS.WITHOUT_SUGGESTION", + sqlState = "42605", + parameters = Map( + "functionName" -> "`collate`", + "expectedNum" -> "2", + "actualNum" -> args.split(',').length.toString, + "docroot" -> "https://spark.apache.org/docs/latest"), + context = ExpectedContext( + fragment = s"collate($args)", + start = 7, + stop = 15 + args.length) + ) + }) + } + + test("collation expression returns default collation") { + checkAnswer(sql(s"select collation('aaa')"), Row("UCS_BASIC")) + } + + test("invalid collation name throws exception") { + checkError( + exception = intercept[SparkException] { sql("select 'aaa' collate 'UCS_BASIS'") }, Review Comment: Could you check `NULL` as the collation name: ```scala sql("select collate('aaa', CAST(NULL AS STRING))") ``` Users might get a NPE: ```java Caused by: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.apache.spark.sql.catalyst.expressions.Expression.eval(org.apache.spark.sql.catalyst.InternalRow)" is null at org.apache.spark.sql.catalyst.expressions.CollateExpressionBuilder$.build(collationExpressions.scala:40) at org.apache.spark.sql.catalyst.expressions.CollateExpressionBuilder$.build(collationExpressions.scala:36) ``` ########## sql/core/src/test/scala/org/apache/spark/sql/CollationSuite.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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 + +import org.apache.spark.SparkException +import org.apache.spark.sql.catalyst.util.CollationFactory +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.StringType + +class CollationSuite extends QueryTest with SharedSparkSession { + test("collate returns proper type") { + Seq("ucs_basic", "ucs_basic_lcase", "unicode", "unicode_ci").foreach { collationName => + checkAnswer(sql(s"select 'aaa' collate '$collationName'"), Row("aaa")) + val collationId = CollationFactory.collationNameToId(collationName) + assert(sql(s"select 'aaa' collate '$collationName'").schema(0).dataType + == StringType(collationId)) + } + } + + test("collation name is case insensitive") { + Seq("uCs_BasIc", "uCs_baSic_Lcase", "uNicOde", "UNICODE_ci").foreach { collationName => + checkAnswer(sql(s"select 'aaa' collate '$collationName'"), Row("aaa")) + val collationId = CollationFactory.collationNameToId(collationName) + assert(sql(s"select 'aaa' collate '$collationName'").schema(0).dataType + == StringType(collationId)) + } + } + + test("collation expression returns name of collation") { + Seq("ucs_basic", "ucs_basic_lcase", "unicode", "unicode_ci").foreach { collationName => + checkAnswer( + sql(s"select collation('aaa' collate '$collationName')"), Row(collationName.toUpperCase())) + } + } + + test("collate function syntax") { + assert(sql(s"select collate('aaa', 'ucs_basic')").schema(0).dataType == StringType(0)) + assert(sql(s"select collate('aaa', 'ucs_basic_lcase')").schema(0).dataType == StringType(1)) + } + + test("collate function syntax invalid args") { + Seq("'aaa','a','b'", "'aaa'").foreach(args => { Review Comment: Could you check more corner cases: 1. No args at all 2. NULLs 3. `collationName` is not string literal: other type, foldable string 4. the first argument is not string (should we support implicit conversions to string?) -- 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]
