mihailom-db commented on code in PR #46801: URL: https://github.com/apache/spark/pull/46801#discussion_r1632683134
########## sql/core/src/test/scala/org/apache/spark/sql/CollationExpressionWalkerSuite.scala: ########## @@ -0,0 +1,294 @@ +/* + * 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.SparkFunSuite +import org.apache.spark.sql.catalyst.expressions.{EmptyRow, EvalMode, ExpectsInputTypes, Expression, GenericInternalRow, Literal} +import org.apache.spark.sql.internal.types.{AbstractArrayType, StringTypeAnyCollation, StringTypeBinaryLcase} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{AbstractDataType, AnyDataType, AnyTimestampType, ArrayType, BinaryType, BooleanType, DataType, DatetimeType, Decimal, DecimalType, IntegerType, LongType, MapType, NumericType, StringType, StructType, TypeCollection} +import org.apache.spark.unsafe.types.UTF8String +import org.apache.spark.util.Utils + +/** + * This suite is introduced in order to test a bulk of expressions and functionalities related to + * collations + */ +class CollationExpressionWalkerSuite extends SparkFunSuite with SharedSparkSession { + + // Trait to distinguish different cases for generation + sealed trait CollationType + + case object Utf8Binary extends CollationType + + case object Utf8BinaryLcase extends CollationType + + /** + * Helper function to generate all necesary parameters + * + * @param inputEntry - List of all input entries that need to be generated + * @param collationType - Flag defining collation type to use + * @return + */ + def generateData( + inputEntry: Seq[Any], + collationType: CollationType): Seq[Any] = { + inputEntry.map(generateSingleEntry(_, collationType)) + } + + /** + * Helper function to generate single entry of data. + * @param inputEntry - Single input entry that requires generation + * @param collationType - Flag defining collation type to use + * @return + */ + def generateSingleEntry( + inputEntry: Any, + collationType: CollationType): Any = + inputEntry match { + case e: Class[_] if e.isAssignableFrom(classOf[Expression]) => + generateLiterals(StringTypeAnyCollation, collationType) + case se: Class[_] if se.isAssignableFrom(classOf[Seq[Expression]]) => + Seq(generateLiterals(StringTypeAnyCollation, collationType), + generateLiterals(StringTypeAnyCollation, collationType)) + case oe: Class[_] if oe.isAssignableFrom(classOf[Option[Any]]) => None + case b: Class[_] if b.isAssignableFrom(classOf[Boolean]) => false + case dt: Class[_] if dt.isAssignableFrom(classOf[DataType]) => StringType + case st: Class[_] if st.isAssignableFrom(classOf[StructType]) => StructType + case em: Class[_] if em.isAssignableFrom(classOf[EvalMode.Value]) => EvalMode.LEGACY + case m: Class[_] if m.isAssignableFrom(classOf[Map[_, _]]) => Map.empty + case c: Class[_] if c.isAssignableFrom(classOf[Char]) => '\\' + case i: Class[_] if i.isAssignableFrom(classOf[Int]) => 0 + case l: Class[_] if l.isAssignableFrom(classOf[Long]) => 0 + case adt: AbstractDataType => generateLiterals(adt, collationType) + case Nil => Seq() + case (head: AbstractDataType) :: rest => generateData(head :: rest, collationType) + } + + /** + * Helper function to generate single literal from the given type. + * + * @param inputType - Single input literal type that requires generation + * @param collationType - Flag defining collation type to use + * @return + */ + def generateLiterals( + inputType: AbstractDataType, + collationType: CollationType): Expression = + inputType match { + // TODO: Try to make this a bit more random. Review Comment: Well this is a good assumption, but we need to generate the same strings, ints, longs etc. for both cases (UTF8_BINARY and UTF8_BINARY_LCASE), so I need to do something to make sure seeds are the same. -- 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]
