You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2019/06/18 21:08:49 UTC
[spark] branch master updated: [SPARK-28088][SQL] Enhance LPAD/RPAD
function
This is an automated email from the ASF dual-hosted git repository.
dongjoon 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 c7f0301 [SPARK-28088][SQL] Enhance LPAD/RPAD function
c7f0301 is described below
commit c7f0301477da19b41380cef218da447dc8f85a0e
Author: Yuming Wang <yu...@ebay.com>
AuthorDate: Tue Jun 18 14:08:18 2019 -0700
[SPARK-28088][SQL] Enhance LPAD/RPAD function
## What changes were proposed in this pull request?
This pr enhances `LPAD`/`RPAD` function to make `pad` parameter optional.
PostgreSQL, Vertica, Teradata, Oracle and DB2 support make `pad` parameter optional. MySQL, Hive and Presto does not support make `pad` parameter optional. SQL Server does not have `lapd`/`rpad` function.
**PostgreSQL**:
```
postgres=# select substr(version(), 0, 16), lpad('hi', 5), rpad('hi', 5);
substr | lpad | rpad
-----------------+-------+-------
PostgreSQL 11.3 | hi | hi
(1 row)
```
**Vertica**:
```
dbadmin=> select version(), lpad('hi', 5), rpad('hi', 5);
version | lpad | rpad
------------------------------------+-------+-------
Vertica Analytic Database v9.1.1-0 | hi | hi
(1 row)
```
**Teradata**:

**Oracle**:

**DB2**:

More details:
https://www.postgresql.org/docs/11/functions-string.html
https://docs.teradata.com/reader/kmuOwjp1zEYg98JsB8fu_A/e5w8LujIQDlVmRSww2E27A
## How was this patch tested?
unit tests
Closes #24899 from wangyum/SPARK-28088.
Authored-by: Yuming Wang <yu...@ebay.com>
Signed-off-by: Dongjoon Hyun <dh...@apple.com>
---
.../catalyst/expressions/stringExpressions.scala | 22 ++++++++++++++++++----
.../expressions/StringExpressionsSuite.scala | 4 ++++
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
index 576eaec..a49b9bf 100755
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
@@ -1088,8 +1088,9 @@ case class StringLocate(substr: Expression, str: Expression, start: Expression)
*/
@ExpressionDescription(
usage = """
- _FUNC_(str, len, pad) - Returns `str`, left-padded with `pad` to a length of `len`.
+ _FUNC_(str, len[, pad]) - Returns `str`, left-padded with `pad` to a length of `len`.
If `str` is longer than `len`, the return value is shortened to `len` characters.
+ If `pad` is not specified, `str` will be padded to the left with space characters.
""",
examples = """
Examples:
@@ -1097,11 +1098,17 @@ case class StringLocate(substr: Expression, str: Expression, start: Expression)
???hi
> SELECT _FUNC_('hi', 1, '??');
h
+ > SELECT _FUNC_('hi', 5);
+ hi
""",
since = "1.5.0")
-case class StringLPad(str: Expression, len: Expression, pad: Expression)
+case class StringLPad(str: Expression, len: Expression, pad: Expression = Literal(" "))
extends TernaryExpression with ImplicitCastInputTypes {
+ def this(str: Expression, len: Expression) = {
+ this(str, len, Literal(" "))
+ }
+
override def children: Seq[Expression] = str :: len :: pad :: Nil
override def dataType: DataType = StringType
override def inputTypes: Seq[DataType] = Seq(StringType, IntegerType, StringType)
@@ -1122,8 +1129,9 @@ case class StringLPad(str: Expression, len: Expression, pad: Expression)
*/
@ExpressionDescription(
usage = """
- _FUNC_(str, len, pad) - Returns `str`, right-padded with `pad` to a length of `len`.
+ _FUNC_(str, len[, pad]) - Returns `str`, right-padded with `pad` to a length of `len`.
If `str` is longer than `len`, the return value is shortened to `len` characters.
+ If `pad` is not specified, `str` will be padded to the right with space characters.
""",
examples = """
Examples:
@@ -1131,11 +1139,17 @@ case class StringLPad(str: Expression, len: Expression, pad: Expression)
hi???
> SELECT _FUNC_('hi', 1, '??');
h
+ > SELECT _FUNC_('hi', 5);
+ hi
""",
since = "1.5.0")
-case class StringRPad(str: Expression, len: Expression, pad: Expression)
+case class StringRPad(str: Expression, len: Expression, pad: Expression = Literal(" "))
extends TernaryExpression with ImplicitCastInputTypes {
+ def this(str: Expression, len: Expression) = {
+ this(str, len, Literal(" "))
+ }
+
override def children: Seq[Expression] = str :: len :: pad :: Nil
override def dataType: DataType = StringType
override def inputTypes: Seq[DataType] = Seq(StringType, IntegerType, StringType)
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala
index 08f42fc..2e70321 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala
@@ -614,6 +614,8 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(StringLPad(s1, s2, s3), null, row3)
checkEvaluation(StringLPad(s1, s2, s3), null, row4)
checkEvaluation(StringLPad(s1, s2, s3), null, row5)
+ checkEvaluation(StringLPad(Literal("hi"), Literal(5)), " hi")
+ checkEvaluation(StringLPad(Literal("hi"), Literal(1)), "h")
checkEvaluation(StringRPad(Literal("hi"), Literal(5), Literal("??")), "hi???", row1)
checkEvaluation(StringRPad(Literal("hi"), Literal(1), Literal("??")), "h", row1)
@@ -622,6 +624,8 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(StringRPad(s1, s2, s3), null, row3)
checkEvaluation(StringRPad(s1, s2, s3), null, row4)
checkEvaluation(StringRPad(s1, s2, s3), null, row5)
+ checkEvaluation(StringRPad(Literal("hi"), Literal(5)), "hi ")
+ checkEvaluation(StringRPad(Literal("hi"), Literal(1)), "h")
}
test("REPEAT") {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org