Skip to content

Commit

Permalink
Change parser to support POSITION(substr IN str).
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyum committed Jun 13, 2017
1 parent 746b37d commit 8cce02b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ primaryExpression
| identifier #columnReference
| base=primaryExpression '.' fieldName=identifier #dereference
| '(' expression ')' #parenthesizedExpression
| POSITION '(' valueExpression IN valueExpression ')' #position
;

constant
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 @@ -851,6 +853,8 @@ IGNORE: 'IGNORE';

IF: 'IF';

POSITION: 'POSITION';

EQ : '=' | '==';
NSEQ: '<=>';
NEQ : '<>';
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 @@ -658,6 +658,8 @@ case class SubstringIndex(strExpr: Expression, delimExpr: Expression, countExpr:
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) {
StringLocate(expression(ctx.valueExpression(0)), expression(ctx.valueExpression(1)), Literal(1))
}

/**
* Create a (windowed) Function expression.
*/
Expand Down
2 changes: 1 addition & 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,7 +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 @@ -17,4 +17,4 @@ select replace('abc', 'b');
select length(uuid()), (uuid() <> uuid());

-- position
select position('bar', 'foobarbar'), position('bar', 'foobarbar', 5), position(null, 'foobarbar'), position('aaads', null);
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
Expand Up @@ -81,8 +81,8 @@ struct<length(uuid()):int,(NOT (uuid() = uuid())):boolean>


-- !query 7
select position('bar', 'foobarbar'), position('bar', 'foobarbar', 5), position(null, 'foobarbar'), position('aaads', null)
select position('bar' in 'foobarbar'), position(null, 'foobarbar'), position('aaads', null)
-- !query 7 schema
struct<locate(bar, foobarbar, 1):int,locate(bar, foobarbar, 5):int,locate(CAST(NULL AS STRING), foobarbar, 1):int,locate(aaads, CAST(NULL AS STRING), 1):int>
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 7 NULL NULL
4 NULL NULL

0 comments on commit 8cce02b

Please sign in to comment.