viirya commented on a change in pull request #30412:
URL: https://github.com/apache/spark/pull/30412#discussion_r530846502
##########
File path: sql/core/src/main/scala/org/apache/spark/sql/Column.scala
##########
@@ -1181,7 +1181,9 @@ class Column(val expr: Expression) extends Logging {
* @group expr_ops
* @since 1.3.0
*/
- def cast(to: DataType): Column = withExpr { Cast(expr, to) }
+ def cast(to: DataType): Column = withExpr {
+ Cast(expr, CharVarcharUtils.replaceCharVarcharWithString(to))
+ }
Review comment:
So we can do `cast(CharType)`? It actually casts to StringType? But
don't we loss length info?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/types/VarcharType.scala
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.types
+
+import scala.math.Ordering
+import scala.reflect.runtime.universe.typeTag
+
+import org.apache.spark.annotation.Experimental
+import org.apache.spark.unsafe.types.UTF8String
+
+@Experimental
+case class VarcharType(length: Int) extends AtomicType {
+ require(length >= 0, "The length if varchar type cannot be negative.")
Review comment:
if -> of
##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/types/CharType.scala
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.types
+
+import scala.math.Ordering
+import scala.reflect.runtime.universe.typeTag
+
+import org.apache.spark.annotation.Experimental
+import org.apache.spark.unsafe.types.UTF8String
+
+@Experimental
+case class CharType(length: Int) extends AtomicType {
+ require(length >= 0, "The length if char type cannot be negative.")
Review comment:
if -> of
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.util
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.types._
+
+object CharVarcharUtils {
+
+ private val CHAR_VARCHAR_TYPE_STRING_METADATA_KEY =
"__CHAR_VARCHAR_TYPE_STRING"
+
+ /**
+ * Replaces CharType/VarcharType with StringType recursively in the given
struct type. If a
+ * top-level StructField's data type is CharType/VarcharType or has nested
CharType/VarcharType,
+ * this method will add the original type string to the StructField's
metadata, so that we can
+ * re-construct the original data type with CharType/VarcharType later when
needed.
+ */
+ def replaceCharVarcharWithStringInSchema(st: StructType): StructType = {
+ StructType(st.map { field =>
+ if (hasCharVarchar(field.dataType)) {
+ val metadata = new MetadataBuilder().withMetadata(field.metadata)
+ .putString(CHAR_VARCHAR_TYPE_STRING_METADATA_KEY,
field.dataType.sql).build()
+ field.copy(dataType = replaceCharVarcharWithString(field.dataType),
metadata = metadata)
+ } else {
+ field
+ }
+ })
+ }
+
+ /**
+ * Returns true if the given data type is CharType/VarcharType or has nested
CharType/VarcharType.
+ */
+ def hasCharVarchar(dt: DataType): Boolean = {
+ dt.existsRecursively(f => f.isInstanceOf[CharType] ||
f.isInstanceOf[VarcharType])
+ }
+
+ /**
+ * Replaces CharType/VarcharType with StringType recursively in the given
data type.
+ */
+ def replaceCharVarcharWithString(dt: DataType): DataType = dt match {
+ case ArrayType(et, nullable) =>
+ ArrayType(replaceCharVarcharWithString(et), nullable)
+ case MapType(kt, vt, nullable) =>
+ MapType(replaceCharVarcharWithString(kt),
replaceCharVarcharWithString(vt), nullable)
+ case StructType(fields) =>
+ StructType(fields.map { field =>
+ field.copy(dataType = replaceCharVarcharWithString(field.dataType))
+ })
+ case _: CharType => StringType
+ case _: VarcharType => StringType
+ case _ => dt
+ }
+
+ /**
+ * Removes the metadata entry that contains the original type string of
CharType/VarcharType from
+ * the given attribute's metadata.
+ */
+ def cleanAttrMetadata(attr: AttributeReference): AttributeReference = {
+ val cleaned = new MetadataBuilder().withMetadata(attr.metadata)
+ .remove(CHAR_VARCHAR_TYPE_STRING_METADATA_KEY).build()
+ attr.withMetadata(cleaned)
+ }
+
+ /**
+ * Re-construct the original data type from the type string in the given
metadata.
+ * This is needed when dealing with char/varchar columns/fields.
+ */
+ def getRawType(metadata: Metadata): Option[DataType] = {
+ if (metadata.contains(CHAR_VARCHAR_TYPE_STRING_METADATA_KEY)) {
+ Some(CatalystSqlParser.parseRawDataType(
+ metadata.getString(CHAR_VARCHAR_TYPE_STRING_METADATA_KEY)))
+ } else {
+ None
+ }
+ }
+
+ /**
+ * Returns expressions to apply read-side char type padding for the given
attributes. String
+ * values should be right-padded to N characters if it's from a CHAR(N)
column/field.
+ */
+ def charTypePadding(output: Seq[AttributeReference]): Seq[NamedExpression] =
{
+ output.map { attr =>
+ getRawType(attr.metadata).filter { rawType =>
+ rawType.existsRecursively(_.isInstanceOf[CharType])
+ }.map { rawType =>
+ Alias(charTypePadding(attr, rawType), attr.name)(explicitMetadata =
Some(attr.metadata))
+ }.getOrElse(attr)
+ }
+ }
+
+ private def charTypePadding(expr: Expression, dt: DataType): Expression = dt
match {
+ case CharType(length) => StringRPad(expr, Literal(length))
+
+ case StructType(fields) =>
+ val struct = CreateNamedStruct(fields.zipWithIndex.flatMap { case (f, i)
=>
+ Seq(Literal(f.name), charTypePadding(GetStructField(expr, i,
Some(f.name)), f.dataType))
+ })
+ if (expr.nullable) {
+ If(IsNull(expr), Literal(null, struct.dataType), struct)
+ } else {
+ struct
+ }
+
+ case ArrayType(et, containsNull) => charTypePaddingInArray(expr, et,
containsNull)
+
+ case MapType(kt, vt, valueContainsNull) =>
+ val newKeys = charTypePaddingInArray(MapKeys(expr), kt, containsNull =
false)
+ val newValues = charTypePaddingInArray(MapValues(expr), vt,
valueContainsNull)
+ MapFromArrays(newKeys, newValues)
+
+ case _ => expr
+ }
+
+ private def charTypePaddingInArray(
+ arr: Expression, et: DataType, containsNull: Boolean): Expression = {
+ val param = NamedLambdaVariable("x", replaceCharVarcharWithString(et),
containsNull)
+ val func = LambdaFunction(charTypePadding(param, et), Seq(param))
+ ArrayTransform(arr, func)
+ }
+
+ /**
+ * Returns an expression to apply write-side char type padding for the given
expression. A string
Review comment:
char type padding -> char type checking?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]