Skip to content

Commit b21157d

Browse files
committed
update
1 parent d93e832 commit b21157d

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

docs/sql-ref-datatypes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Spark SQL and DataFrames support the following data types:
3737
- `DecimalType`: Represents arbitrary-precision signed decimal numbers. Backed internally by `java.math.BigDecimal`. A `BigDecimal` consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.
3838
* String type
3939
- `StringType`: Represents character string values.
40+
- `VarcharType(length)`: A variant of `StringType` which has a length limitation. Data writing will fail if the input string exceeds the length limitation. Note: this type can only be used in table schema, not functions/operators.
41+
- `CharType(length)`: A variant of `VarcharType(length)` which is fixed length. Data writing will pad the input string if its length is smaller than the char type length. Char type comparison will pad the short one to the longer length.
4042
* Binary type
4143
- `BinaryType`: Represents byte sequence values.
4244
* Boolean type

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ trait CheckAnalysis extends PredicateHelper {
9494

9595
case p if p.analyzed => // Skip already analyzed sub-plans
9696

97-
case p if p.output.map(_.dataType).exists(CharVarcharUtils.hasCharVarchar) =>
97+
case p if p.resolved && p.output.map(_.dataType).exists(CharVarcharUtils.hasCharVarchar) =>
9898
throw new IllegalStateException(
9999
"[BUG] logical plan should not have output of char/varchar type: " + p)
100100

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ object CharVarcharUtils {
9292
}
9393
}
9494

95-
/**
96-
* Re-construct the original StructType from the type strings in the metadata of StructFields.
97-
* This is needed when dealing with char/varchar columns/fields.
98-
*/
99-
def getRawSchema(schema: StructType): StructType = {
100-
StructType(schema.map { field =>
101-
getRawType(field.metadata).map(rawType => field.copy(dataType = rawType)).getOrElse(field)
102-
})
103-
}
104-
10595
/**
10696
* Returns expressions to apply read-side char type padding for the given attributes. String
10797
* values should be right-padded to N characters if it's from a CHAR(N) column/field.

sql/catalyst/src/main/scala/org/apache/spark/sql/types/DataType.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.apache.spark.sql.AnalysisException
3232
import org.apache.spark.sql.catalyst.analysis.Resolver
3333
import org.apache.spark.sql.catalyst.expressions.{Cast, Expression}
3434
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
35+
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
3536
import org.apache.spark.sql.catalyst.util.DataTypeJsonUtils.{DataTypeJsonDeserializer, DataTypeJsonSerializer}
3637
import org.apache.spark.sql.catalyst.util.StringUtils.StringConcat
3738
import org.apache.spark.sql.internal.SQLConf
@@ -132,7 +133,8 @@ object DataType {
132133
ddl,
133134
CatalystSqlParser.parseDataType,
134135
"Cannot parse the data type: ",
135-
fallbackParser = CatalystSqlParser.parseTableSchema)
136+
fallbackParser = str => CharVarcharUtils.replaceCharVarcharWithString(
137+
CatalystSqlParser.parseTableSchema(str)))
136138
}
137139

138140
/**

sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
3535
}
3636

3737
test("char type values should be padded: partitioned columns") {
38-
// DS V2 doesn't support partitioned table.
39-
if (!conf.contains(SQLConf.DEFAULT_CATALOG.key)) {
40-
withTable("t") {
41-
sql(s"CREATE TABLE t(i STRING, c CHAR(5)) USING $format PARTITIONED BY (c)")
42-
sql("INSERT INTO t VALUES ('1', 'a')")
43-
checkAnswer(spark.table("t"), Row("1", "a" + " " * 4))
44-
}
38+
withTable("t") {
39+
sql(s"CREATE TABLE t(i STRING, c CHAR(5)) USING $format PARTITIONED BY (c)")
40+
sql("INSERT INTO t VALUES ('1', 'a')")
41+
checkAnswer(spark.table("t"), Row("1", "a" + " " * 4))
4542
}
4643
}
4744

0 commit comments

Comments
 (0)