Skip to content

Commit 637f16e

Browse files
committed
[SPARK-45070][SQL][DOCS] Describe the binary and datetime formats of to_char/to_varchar
### What changes were proposed in this pull request? In the PR, I propose to document the recent changes related to the `format` of the `to_char`/`to_varchar` functions: 1. binary formats added by #42632 2. datetime formats introduced by #42534 ### Why are the changes needed? To inform users about recent changes. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? By CI. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #42801 from MaxGekk/doc-to_char-api. Authored-by: Max Gekk <[email protected]> Signed-off-by: Max Gekk <[email protected]>
1 parent e4d17e9 commit 637f16e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,6 +4280,7 @@ object functions {
42804280
*/
42814281
def to_binary(e: Column): Column = Column.fn("to_binary", e)
42824282

4283+
// scalastyle:off line.size.limit
42834284
/**
42844285
* Convert `e` to a string based on the `format`. Throws an exception if the conversion fails.
42854286
*
@@ -4300,13 +4301,20 @@ object functions {
43004301
* (optional, only allowed once at the beginning or end of the format string). Note that 'S'
43014302
* prints '+' for positive values but 'MI' prints a space.</li> <li>'PR': Only allowed at the
43024303
* end of the format string; specifies that the result string will be wrapped by angle
4303-
* brackets if the input value is negative.</li> </ul>
4304+
* brackets if the input value is negative.</li> </ul> If `e` is a datetime, `format` shall be
4305+
* a valid datetime pattern, see <a
4306+
* href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Datetime
4307+
* Patterns</a>. If `e` is a binary, it is converted to a string in one of the formats: <ul>
4308+
* <li>'base64': a base 64 string.</li> <li>'hex': a string in the hexadecimal format.</li>
4309+
* <li>'utf-8': the input binary is decoded to UTF-8 string.</li> </ul>
43044310
*
43054311
* @group string_funcs
43064312
* @since 3.5.0
43074313
*/
4314+
// scalastyle:on line.size.limit
43084315
def to_char(e: Column, format: Column): Column = Column.fn("to_char", e, format)
43094316

4317+
// scalastyle:off line.size.limit
43104318
/**
43114319
* Convert `e` to a string based on the `format`. Throws an exception if the conversion fails.
43124320
*
@@ -4327,11 +4335,17 @@ object functions {
43274335
* (optional, only allowed once at the beginning or end of the format string). Note that 'S'
43284336
* prints '+' for positive values but 'MI' prints a space.</li> <li>'PR': Only allowed at the
43294337
* end of the format string; specifies that the result string will be wrapped by angle
4330-
* brackets if the input value is negative.</li> </ul>
4338+
* brackets if the input value is negative.</li> </ul> If `e` is a datetime, `format` shall be
4339+
* a valid datetime pattern, see <a
4340+
* href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Datetime
4341+
* Patterns</a>. If `e` is a binary, it is converted to a string in one of the formats: <ul>
4342+
* <li>'base64': a base 64 string.</li> <li>'hex': a string in the hexadecimal format.</li>
4343+
* <li>'utf-8': the input binary is decoded to UTF-8 string.</li> </ul>
43314344
*
43324345
* @group string_funcs
43334346
* @since 3.5.0
43344347
*/
4348+
// scalastyle:on line.size.limit
43354349
def to_varchar(e: Column, format: Column): Column = Column.fn("to_varchar", e, format)
43364350

43374351
/**

python/pyspark/sql/functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10902,6 +10902,12 @@ def to_char(col: "ColumnOrName", format: "ColumnOrName") -> Column:
1090210902
values but 'MI' prints a space.
1090310903
'PR': Only allowed at the end of the format string; specifies that the result string
1090410904
will be wrapped by angle brackets if the input value is negative.
10905+
If `col` is a datetime, `format` shall be a valid datetime pattern, see
10906+
<a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Patterns</a>.
10907+
If `col` is a binary, it is converted to a string in one of the formats:
10908+
'base64': a base 64 string.
10909+
'hex': a string in the hexadecimal format.
10910+
'utf-8': the input binary is decoded to UTF-8 string.
1090510911
1090610912
.. versionadded:: 3.5.0
1090710913
@@ -10942,6 +10948,12 @@ def to_varchar(col: "ColumnOrName", format: "ColumnOrName") -> Column:
1094210948
values but 'MI' prints a space.
1094310949
'PR': Only allowed at the end of the format string; specifies that the result string
1094410950
will be wrapped by angle brackets if the input value is negative.
10951+
If `col` is a datetime, `format` shall be a valid datetime pattern, see
10952+
<a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Patterns</a>.
10953+
If `col` is a binary, it is converted to a string in one of the formats:
10954+
'base64': a base 64 string.
10955+
'hex': a string in the hexadecimal format.
10956+
'utf-8': the input binary is decoded to UTF-8 string.
1094510957
1094610958
.. versionadded:: 3.5.0
1094710959

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,7 @@ object functions {
44004400
new ToBinary(e.expr)
44014401
}
44024402

4403+
// scalastyle:off line.size.limit
44034404
/**
44044405
* Convert `e` to a string based on the `format`.
44054406
* Throws an exception if the conversion fails. The format can consist of the following
@@ -4421,11 +4422,20 @@ object functions {
44214422
* 'PR': Only allowed at the end of the format string; specifies that the result string will be
44224423
* wrapped by angle brackets if the input value is negative.
44234424
*
4425+
* If `e` is a datetime, `format` shall be a valid datetime pattern, see
4426+
* <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Datetime Patterns</a>.
4427+
* If `e` is a binary, it is converted to a string in one of the formats:
4428+
* 'base64': a base 64 string.
4429+
* 'hex': a string in the hexadecimal format.
4430+
* 'utf-8': the input binary is decoded to UTF-8 string.
4431+
*
44244432
* @group string_funcs
44254433
* @since 3.5.0
44264434
*/
4435+
// scalastyle:on line.size.limit
44274436
def to_char(e: Column, format: Column): Column = call_function("to_char", e, format)
44284437

4438+
// scalastyle:off line.size.limit
44294439
/**
44304440
* Convert `e` to a string based on the `format`.
44314441
* Throws an exception if the conversion fails. The format can consist of the following
@@ -4447,9 +4457,17 @@ object functions {
44474457
* 'PR': Only allowed at the end of the format string; specifies that the result string will be
44484458
* wrapped by angle brackets if the input value is negative.
44494459
*
4460+
* If `e` is a datetime, `format` shall be a valid datetime pattern, see
4461+
* <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html">Datetime Patterns</a>.
4462+
* If `e` is a binary, it is converted to a string in one of the formats:
4463+
* 'base64': a base 64 string.
4464+
* 'hex': a string in the hexadecimal format.
4465+
* 'utf-8': the input binary is decoded to UTF-8 string.
4466+
*
44504467
* @group string_funcs
44514468
* @since 3.5.0
44524469
*/
4470+
// scalastyle:on line.size.limit
44534471
def to_varchar(e: Column, format: Column): Column = call_function("to_varchar", e, format)
44544472

44554473
/**

0 commit comments

Comments
 (0)