Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ predicate
| NOT? kind=IN '(' expression (',' expression)* ')'
| NOT? kind=IN '(' query ')'
| NOT? kind=RLIKE pattern=valueExpression
| NOT? kind=LIKE quantifier=(ANY | ALL) '(' expression (',' expression)* ')'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we don't need to support ESCAPE. Did I understand correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it~

| NOT? kind=LIKE pattern=valueExpression (ESCAPE escapeChar=STRING)?
| IS NOT? kind=NULL
| IS NOT? kind=(TRUE | FALSE | UNKNOWN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1387,14 +1387,23 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
case SqlBaseParser.IN =>
invertIfNotDefined(In(e, ctx.expression.asScala.map(expression)))
case SqlBaseParser.LIKE =>
val escapeChar = Option(ctx.escapeChar).map(string).map { str =>
if (str.length != 1) {
throw new ParseException("Invalid escape string." +
"Escape string must contains only one character.", ctx)
}
str
}.getOrElse('\\')
invertIfNotDefined(Like(e, expression(ctx.pattern), Literal(escapeChar)))
Option(ctx.quantifier).map(_.getType) match {
case Some(SqlBaseParser.ANY) if !ctx.expression.isEmpty =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this ctx.expression.isEmpty? It seems that the parser rule guarantee at least one expression.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it.

ctx.expression.asScala.map(expression).map(p => invertIfNotDefined(new Like(e, p)))
.reduceLeft(Or)
case Some(SqlBaseParser.ALL) if !ctx.expression.isEmpty =>
ctx.expression.asScala.map(expression).map(p => invertIfNotDefined(new Like(e, p)))
.reduceLeft(And)
case _ =>
val escapeChar = Option(ctx.escapeChar).map(string).map { str =>
if (str.length != 1) {
throw new ParseException("Invalid escape string." +
"Escape string must contains only one character.", ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: contains -> contain ?

}
str
}.getOrElse('\\')
invertIfNotDefined(Like(e, expression(ctx.pattern), Literal(escapeChar)))
}
case SqlBaseParser.RLIKE =>
invertIfNotDefined(RLike(e, expression(ctx.pattern)))
case SqlBaseParser.NULL if ctx.NOT != null =>
Expand Down
39 changes: 39 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/like-all.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE OR REPLACE TEMPORARY VIEW like_all_table AS SELECT * FROM (VALUES
('google', '%oo%'),
('facebook', '%oo%'),
('linkedin', '%in'))
as t1(company, pat);

select company from like_all_table where company like all ('%oo%','%go%') ;

select company from like_all_table where company like all ('microsoft','%yoo%') ;

select
company,
CASE
WHEN company like all ('%oo%','%go%') THEN 'Y'
ELSE 'N'
END AS is_available,
CASE
WHEN company like all ('%oo%','go%') OR company like all ('%in','ms%') THEN 'Y'
ELSE 'N'
END AS mix
From like_all_table ;

--Mix test with constant pattern and column value
select company from like_all_table where company like all ('%oo%',pat) ;

-- not like all test

select company from like_all_table where company not like all ('%oo%','%in','fa%') ;
select company from like_all_table where company not like all ('microsoft','%yoo%') ;

-- null test

select company from like_all_table where company like all ('%oo%',null) ;

select company from like_all_table where company not like all ('%oo%',null) ;

select company from like_all_table where company not like all (null,null) ;

select company from like_all_table where company not like all (null,null) ;
39 changes: 39 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/like-any.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE OR REPLACE TEMPORARY VIEW like_any_table AS SELECT * FROM (VALUES
('google', '%oo%'),
('facebook', '%oo%'),
('linkedin', '%in'))
as t1(company, pat);

select company from like_any_table where company like any ('%oo%','%in','fa%') ;

select company from like_any_table where company like any ('microsoft','%yoo%') ;

select
company,
CASE
WHEN company like any ('%oo%','%in','fa%') THEN 'Y'
ELSE 'N'
END AS is_available,
CASE
WHEN company like any ('%oo%','fa%') OR company like any ('%in','ms%') THEN 'Y'
ELSE 'N'
END AS mix
From like_any_table;

--Mix test with constant pattern and column value
select company from like_any_table where company like any ('%zz%',pat) ;

-- not like any test

select company from like_any_table where company not like any ('%oo%','%in','fa%') ;
select company from like_any_table where company not like any ('microsoft','%yoo%') ;

-- null test

select company from like_any_table where company like any ('%oo%',null) ;

select company from like_any_table where company not like any ('%oo%',null) ;

select company from like_any_table where company like any (null,null) ;

select company from like_any_table where company not like any (null,null) ;
109 changes: 109 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/like-all.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 11


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I've checked that the output is the same with PostgreSQL output: https://gist.github.com/maropu/fa4bd6491e21751d6bbc44c545390b0c

-- !query
CREATE OR REPLACE TEMPORARY VIEW like_all_table AS SELECT * FROM (VALUES
('google', '%oo%'),
('facebook', '%oo%'),
('linkedin', '%in'))
as t1(company, pat)
-- !query schema
struct<>
-- !query output



-- !query
select company from like_all_table where company like all ('%oo%','%go%')
-- !query schema
struct<company:string>
-- !query output
google


-- !query
select company from like_all_table where company like all ('microsoft','%yoo%')
-- !query schema
struct<company:string>
-- !query output



-- !query
select
company,
CASE
WHEN company like all ('%oo%','%go%') THEN 'Y'
ELSE 'N'
END AS is_available,
CASE
WHEN company like all ('%oo%','go%') OR company like all ('%in','ms%') THEN 'Y'
ELSE 'N'
END AS mix
From like_all_table
-- !query schema
struct<company:string,is_available:string,mix:string>
-- !query output
facebook N N
google Y Y
linkedin N N


-- !query
select company from like_all_table where company like all ('%oo%',pat)
-- !query schema
struct<company:string>
-- !query output
facebook
google


-- !query
select company from like_all_table where company not like all ('%oo%','%in','fa%')
-- !query schema
struct<company:string>
-- !query output



-- !query
select company from like_all_table where company not like all ('microsoft','%yoo%')
-- !query schema
struct<company:string>
-- !query output
facebook
google
linkedin


-- !query
select company from like_all_table where company like all ('%oo%',null)
-- !query schema
struct<company:string>
-- !query output



-- !query
select company from like_all_table where company not like all ('%oo%',null)
-- !query schema
struct<company:string>
-- !query output



-- !query
select company from like_all_table where company not like all (null,null)
-- !query schema
struct<company:string>
-- !query output



-- !query
select company from like_all_table where company not like all (null,null)
-- !query schema
struct<company:string>
-- !query output

115 changes: 115 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/like-any.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 11


-- !query
CREATE OR REPLACE TEMPORARY VIEW like_any_table AS SELECT * FROM (VALUES
('google', '%oo%'),
('facebook', '%oo%'),
('linkedin', '%in'))
as t1(company, pat)
-- !query schema
struct<>
-- !query output



-- !query
select company from like_any_table where company like any ('%oo%','%in','fa%')
-- !query schema
struct<company:string>
-- !query output
facebook
google
linkedin


-- !query
select company from like_any_table where company like any ('microsoft','%yoo%')
-- !query schema
struct<company:string>
-- !query output



-- !query
select
company,
CASE
WHEN company like any ('%oo%','%in','fa%') THEN 'Y'
ELSE 'N'
END AS is_available,
CASE
WHEN company like any ('%oo%','fa%') OR company like any ('%in','ms%') THEN 'Y'
ELSE 'N'
END AS mix
From like_any_table
-- !query schema
struct<company:string,is_available:string,mix:string>
-- !query output
facebook Y Y
google Y Y
linkedin Y Y


-- !query
select company from like_any_table where company like any ('%zz%',pat)
-- !query schema
struct<company:string>
-- !query output
facebook
google
linkedin


-- !query
select company from like_any_table where company not like any ('%oo%','%in','fa%')
-- !query schema
struct<company:string>
-- !query output
facebook
google
linkedin


-- !query
select company from like_any_table where company not like any ('microsoft','%yoo%')
-- !query schema
struct<company:string>
-- !query output
facebook
google
linkedin


-- !query
select company from like_any_table where company like any ('%oo%',null)
-- !query schema
struct<company:string>
-- !query output
facebook
google


-- !query
select company from like_any_table where company not like any ('%oo%',null)
-- !query schema
struct<company:string>
-- !query output
linkedin


-- !query
select company from like_any_table where company like any (null,null)
-- !query schema
struct<company:string>
-- !query output



-- !query
select company from like_any_table where company not like any (null,null)
-- !query schema
struct<company:string>
-- !query output