This is an automated email from the ASF dual-hosted git repository.
yao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 8b16196ad89a [SPARK-48680][SQL][DOCS] Add missing Java APIs and
language-specific documentations for char/varchar
8b16196ad89a is described below
commit 8b16196ad89ad34e1981fc8df5f113094919e184
Author: Kent Yao <[email protected]>
AuthorDate: Mon Jun 24 16:25:24 2024 +0800
[SPARK-48680][SQL][DOCS] Add missing Java APIs and language-specific
documentations for char/varchar
### What changes were proposed in this pull request?
- Add Java APIs to create char/varchar types
- Doc char/varchar to language-specific tables
### Why are the changes needed?
- feature parity among polyglot APIs
- document improvement
### Does this PR introduce _any_ user-facing change?
yes, new APIs
### How was this patch tested?
doc build
### Was this patch authored or co-authored using generative AI tooling?
no
Closes #47052 from yaooqinn/SPARK-48680.
Authored-by: Kent Yao <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
---
docs/sql-ref-datatypes.md | 10 +++++++++-
.../java/org/apache/spark/sql/types/DataTypes.java | 18 ++++++++++++++++++
.../org/apache/spark/sql/types/DataTypeSuite.scala | 9 +++++++++
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/docs/sql-ref-datatypes.md b/docs/sql-ref-datatypes.md
index 8d75b4a175ab..3a4530dcecae 100644
--- a/docs/sql-ref-datatypes.md
+++ b/docs/sql-ref-datatypes.md
@@ -126,7 +126,9 @@ from pyspark.sql.types import *
|**FloatType**|float<br/>**Note:** Numbers will be converted to 4-byte
single-precision floating point numbers at runtime.|FloatType()|
|**DoubleType**|float|DoubleType()|
|**DecimalType**|decimal.Decimal|DecimalType()|
-|**StringType**|string|StringType()|
+|**StringType**|str|StringType()|
+|**CharType(length)**|str|CharType(length)|
+|**VarcharType(length)**|str|VarcharType(length)|
|**BinaryType**|bytearray|BinaryType()|
|**BooleanType**|bool|BooleanType()|
|**TimestampType**|datetime.datetime|TimestampType()|
@@ -157,6 +159,8 @@ You can access them by doing
|**DoubleType**|Double|DoubleType|
|**DecimalType**|java.math.BigDecimal|DecimalType|
|**StringType**|String|StringType|
+|**CharType(length)**|String|CharType(length)|
+|**VarcharType(length)**|String|VarcharType(length)|
|**BinaryType**|Array[Byte]|BinaryType|
|**BooleanType**|Boolean|BooleanType|
|**TimestampType**|java.time.Instant or java.sql.Timestamp|TimestampType|
@@ -188,6 +192,8 @@ please use factory methods provided in
|**DoubleType**|double or Double|DataTypes.DoubleType|
|**DecimalType**|java.math.BigDecimal|DataTypes.createDecimalType()<br/>DataTypes.createDecimalType(*precision*,
*scale*).|
|**StringType**|String|DataTypes.StringType|
+|**CharType(length)**|String|DataTypes.createCharType(length)|
+|**VarcharType(length)**|String|DataTypes.createVarcharType(length)|
|**BinaryType**|byte[]|DataTypes.BinaryType|
|**BooleanType**|boolean or Boolean|DataTypes.BooleanType|
|**TimestampType**|java.time.Instant or
java.sql.Timestamp|DataTypes.TimestampType|
@@ -242,6 +248,8 @@ The following table shows the type names as well as aliases
used in Spark SQL pa
|**TimestampType**|TIMESTAMP, TIMESTAMP_LTZ|
|**TimestampNTZType**|TIMESTAMP_NTZ|
|**StringType**|STRING|
+|**CharType(length)**|CHAR(length)|
+|**VarcharType(length)**|VARCHAR(length)|
|**BinaryType**|BINARY|
|**DecimalType**|DECIMAL, DEC, NUMERIC|
|**YearMonthIntervalType**|INTERVAL YEAR, INTERVAL YEAR TO MONTH, INTERVAL
MONTH|
diff --git a/sql/api/src/main/java/org/apache/spark/sql/types/DataTypes.java
b/sql/api/src/main/java/org/apache/spark/sql/types/DataTypes.java
index 32c20dedac4c..0034b8e71518 100644
--- a/sql/api/src/main/java/org/apache/spark/sql/types/DataTypes.java
+++ b/sql/api/src/main/java/org/apache/spark/sql/types/DataTypes.java
@@ -259,4 +259,22 @@ public class DataTypes {
return StructType$.MODULE$.apply(fields);
}
+
+ /**
+ * Creates a CharType with the given length.
+ *
+ * @since 4.0.0
+ */
+ public static CharType createCharType(int length) {
+ return new CharType(length);
+ }
+
+ /**
+ * Creates a VarcharType with the given length.
+ *
+ * @since 4.0.0
+ */
+ public static VarcharType createVarcharType(int length) {
+ return new VarcharType(length);
+ }
}
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala
index 3a88b900430d..8fd9b7c43a65 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala
@@ -1000,4 +1000,13 @@ class DataTypeSuite extends SparkFunSuite {
parameters = Map("provider" -> "badProvider", "supportedProviders" ->
"spark, icu")
)
}
+
+ test("SPARK-48680: Add CharType and VarcharType to DataTypes JAVA API") {
+ assert(DataTypes.createCharType(1) === CharType(1))
+ assert(DataTypes.createVarcharType(100) === VarcharType(100))
+ val exception = intercept[IllegalArgumentException] {
+ DataTypes.createVarcharType(-1)
+ }
+ assert(exception.getMessage.contains("The length of varchar type cannot be
negative."))
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]