Skip to content

Commit

Permalink
[SPARK-20754][SQL][FOLLOWUP] Add Function Alias For MOD/POSITION.
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

#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.
  • Loading branch information
wangyum authored and gatorsmile committed Jun 14, 2017
1 parent dccc0aa commit 4d01aa4
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -850,6 +852,7 @@ MACRO: 'MACRO';
IGNORE: 'IGNORE';

IF: 'IF';
POSITION: 'POSITION';

EQ : '=' | '==';
NSEQ: '<=>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion sql/core/src/test/resources/sql-tests/inputs/operators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
10 changes: 9 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/operators.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 50
-- Number of queries: 51


-- !query 0
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 7
-- Number of queries: 8


-- !query 0
Expand Down Expand Up @@ -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

0 comments on commit 4d01aa4

Please sign in to comment.