[SPARK-20754][SQL][FOLLOWUP] Add Function Alias For MOD/POSITION.
## What changes were proposed in this pull request? https://github.com/apache/spark/pull/18106 Support TRUNC (number), We should also add function alias for `MOD `and `POSITION`. `POSITION(substr IN str) `is a synonym for `LOCATE(substr,str)`. same as MySQL: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_position ## How was this patch tested? unit tests Author: Yuming Wang <wgyumg@gmail.com> Closes #18206 from wangyum/SPARK-20754-mod&position.
This commit is contained in:
parent
dccc0aa3cf
commit
4d01aa4648
|
@ -563,6 +563,7 @@ primaryExpression
|
|||
| CAST '(' expression AS dataType ')' #cast
|
||||
| FIRST '(' expression (IGNORE NULLS)? ')' #first
|
||||
| LAST '(' expression (IGNORE NULLS)? ')' #last
|
||||
| POSITION '(' substr=valueExpression IN str=valueExpression ')' #position
|
||||
| constant #constantDefault
|
||||
| ASTERISK #star
|
||||
| qualifiedName '.' ASTERISK #star
|
||||
|
@ -720,6 +721,7 @@ nonReserved
|
|||
| SET | RESET
|
||||
| VIEW | REPLACE
|
||||
| IF
|
||||
| POSITION
|
||||
| NO | DATA
|
||||
| START | TRANSACTION | COMMIT | ROLLBACK | IGNORE
|
||||
| SORT | CLUSTER | DISTRIBUTE | UNSET | TBLPROPERTIES | SKEWED | STORED | DIRECTORIES | LOCATION
|
||||
|
@ -850,6 +852,7 @@ MACRO: 'MACRO';
|
|||
IGNORE: 'IGNORE';
|
||||
|
||||
IF: 'IF';
|
||||
POSITION: 'POSITION';
|
||||
|
||||
EQ : '=' | '==';
|
||||
NSEQ: '<=>';
|
||||
|
|
|
@ -240,6 +240,7 @@ object FunctionRegistry {
|
|||
expression[Log1p]("log1p"),
|
||||
expression[Log2]("log2"),
|
||||
expression[Log]("ln"),
|
||||
expression[Remainder]("mod"),
|
||||
expression[UnaryMinus]("negative"),
|
||||
expression[Pi]("pi"),
|
||||
expression[Pmod]("pmod"),
|
||||
|
@ -325,6 +326,7 @@ object FunctionRegistry {
|
|||
expression[StringTrimLeft]("ltrim"),
|
||||
expression[JsonTuple]("json_tuple"),
|
||||
expression[ParseUrl]("parse_url"),
|
||||
expression[StringLocate]("position"),
|
||||
expression[FormatString]("printf"),
|
||||
expression[RegExpExtract]("regexp_extract"),
|
||||
expression[RegExpReplace]("regexp_replace"),
|
||||
|
|
|
@ -320,6 +320,8 @@ case class Divide(left: Expression, right: Expression) extends BinaryArithmetic
|
|||
Examples:
|
||||
> SELECT 2 _FUNC_ 1.8;
|
||||
0.2
|
||||
> SELECT MOD(2, 1.8);
|
||||
0.2
|
||||
""")
|
||||
case class Remainder(left: Expression, right: Expression) extends BinaryArithmetic {
|
||||
|
||||
|
|
|
@ -654,8 +654,12 @@ case class SubstringIndex(strExpr: Expression, delimExpr: Expression, countExpr:
|
|||
""",
|
||||
extended = """
|
||||
Examples:
|
||||
> SELECT _FUNC_('bar', 'foobarbar');
|
||||
4
|
||||
> SELECT _FUNC_('bar', 'foobarbar', 5);
|
||||
7
|
||||
> SELECT POSITION('bar' IN 'foobarbar');
|
||||
4
|
||||
""")
|
||||
// scalastyle:on line.size.limit
|
||||
case class StringLocate(substr: Expression, str: Expression, start: Expression)
|
||||
|
|
|
@ -1076,6 +1076,13 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
|
|||
Last(expression(ctx.expression), Literal(ignoreNullsExpr)).toAggregateExpression()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Position expression.
|
||||
*/
|
||||
override def visitPosition(ctx: PositionContext): Expression = withOrigin(ctx) {
|
||||
new StringLocate(expression(ctx.substr), expression(ctx.str))
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a (windowed) Function expression.
|
||||
*/
|
||||
|
|
|
@ -51,7 +51,7 @@ class TableIdentifierParserSuite extends SparkFunSuite {
|
|||
"rollup", "row", "rows", "set", "smallint", "table", "timestamp", "to", "trigger",
|
||||
"true", "truncate", "update", "user", "values", "with", "regexp", "rlike",
|
||||
"bigint", "binary", "boolean", "current_date", "current_timestamp", "date", "double", "float",
|
||||
"int", "smallint", "timestamp", "at")
|
||||
"int", "smallint", "timestamp", "at", "position")
|
||||
|
||||
val hiveStrictNonReservedKeyword = Seq("anti", "full", "inner", "left", "semi", "right",
|
||||
"natural", "union", "intersect", "except", "database", "on", "join", "cross", "select", "from",
|
||||
|
|
|
@ -76,4 +76,7 @@ select floor(0.01);
|
|||
select floor(-0.10);
|
||||
|
||||
-- comparison operator
|
||||
select 1 > 0.00001
|
||||
select 1 > 0.00001;
|
||||
|
||||
-- mod
|
||||
select mod(7, 2), mod(7, 0), mod(0, 2), mod(7, null), mod(null, 2), mod(null, null);
|
||||
|
|
|
@ -15,3 +15,6 @@ select replace('abc', 'b');
|
|||
|
||||
-- uuid
|
||||
select length(uuid()), (uuid() <> uuid());
|
||||
|
||||
-- position
|
||||
select position('bar' in 'foobarbar'), position(null, 'foobarbar'), position('aaads', null);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-- Automatically generated by SQLQueryTestSuite
|
||||
-- Number of queries: 50
|
||||
-- Number of queries: 51
|
||||
|
||||
|
||||
-- !query 0
|
||||
|
@ -412,3 +412,11 @@ select 1 > 0.00001
|
|||
struct<(CAST(1 AS BIGINT) > 0):boolean>
|
||||
-- !query 49 output
|
||||
true
|
||||
|
||||
|
||||
-- !query 50
|
||||
select mod(7, 2), mod(7, 0), mod(0, 2), mod(7, null), mod(null, 2), mod(null, null)
|
||||
-- !query 50 schema
|
||||
struct<(7 % 2):int,(7 % 0):int,(0 % 2):int,(7 % CAST(NULL AS INT)):int,(CAST(NULL AS INT) % 2):int,(CAST(NULL AS DOUBLE) % CAST(NULL AS DOUBLE)):double>
|
||||
-- !query 50 output
|
||||
1 NULL 0 NULL NULL NULL
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-- Automatically generated by SQLQueryTestSuite
|
||||
-- Number of queries: 7
|
||||
-- Number of queries: 8
|
||||
|
||||
|
||||
-- !query 0
|
||||
|
@ -78,3 +78,11 @@ select length(uuid()), (uuid() <> uuid())
|
|||
struct<length(uuid()):int,(NOT (uuid() = uuid())):boolean>
|
||||
-- !query 6 output
|
||||
36 true
|
||||
|
||||
|
||||
-- !query 7
|
||||
select position('bar' in 'foobarbar'), position(null, 'foobarbar'), position('aaads', null)
|
||||
-- !query 7 schema
|
||||
struct<locate(bar, foobarbar, 1):int,locate(CAST(NULL AS STRING), foobarbar, 1):int,locate(aaads, CAST(NULL AS STRING), 1):int>
|
||||
-- !query 7 output
|
||||
4 NULL NULL
|
||||
|
|
Loading…
Reference in a new issue