Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -532,7 +532,9 @@ predicate
: NOT? kind=BETWEEN lower=valueExpression AND upper=valueExpression
| NOT? kind=IN '(' expression (',' expression)* ')'
| NOT? kind=IN '(' query ')'
| NOT? kind=(RLIKE | LIKE) pattern=valueExpression
| NOT? kind=LIKE pattern=valueExpression
| NOT? kind=RLIKE regex=regexString
| NOT? kind=RLIKE pattern=valueExpression
| IS NOT? kind=NULL
;

Expand Down Expand Up @@ -576,6 +578,10 @@ constant
| STRING+ #stringLiteral
;

regexString
: STRING+ #regexPattern
;

comparisonOperator
: EQ | NEQ | NEQJ | LT | LTE | GT | GTE | NSEQ
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,11 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
case SqlBaseParser.LIKE =>
invertIfNotDefined(Like(e, expression(ctx.pattern)))
case SqlBaseParser.RLIKE =>
invertIfNotDefined(RLike(e, expression(ctx.pattern)))
if (ctx.pattern != null) {
invertIfNotDefined(RLike(e, expression(ctx.pattern)))
} else {
invertIfNotDefined(RLike(e, expression(ctx.regex)))
}
case SqlBaseParser.NULL if ctx.NOT != null =>
IsNotNull(e)
case SqlBaseParser.NULL =>
Expand Down Expand Up @@ -1398,6 +1402,10 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
Literal(createString(ctx))
}

override def visitRegexPattern(ctx: RegexPatternContext): Literal = withOrigin(ctx) {
Literal(ctx.STRING().asScala.map(regexString).mkString)
}

/**
* Create a String from a string literal context. This supports multiple consecutive string
* literals, these are concatenated, for example this expression "'hello' 'world'" will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ object ParserUtils {
/** Convert a string node into a string. */
def string(node: TerminalNode): String = unescapeSQLString(node.getText)

/** Convert a string node to a regex string. */
def regexString(node: TerminalNode): String = {
node.getText.slice(1, node.getText.size - 1)
}

/** Get the origin (line and position) of the token. */
def position(token: Token): Origin = {
val opt = Option(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ class ExpressionParserSuite extends PlanTest {
assertEqual("a not rlike 'pattern%'", !('a rlike "pattern%"))
assertEqual("a regexp 'pattern%'", 'a rlike "pattern%")
assertEqual("a not regexp 'pattern%'", !('a rlike "pattern%"))

assertEqual("a rlike '^\\x20[\\x20-\\x23]+$'", 'a rlike "^\\x20[\\x20-\\x23]+$")
assertEqual("a rlike 'pattern\\\\'", 'a rlike "pattern\\\\")
assertEqual("a rlike 'pattern\\t\\n'", 'a rlike "pattern\\t\\n")
intercept("a rlike 'pattern\\'", "mismatched input")
}

test("is null expressions") {
Expand Down
10 changes: 10 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,16 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
val ds = Seq(WithMapInOption(Some(Map(1 -> 1)))).toDS()
checkDataset(ds, WithMapInOption(Some(Map(1 -> 1))))
}

test("do not unescaped regex pattern string") {
val data = Seq("\u0020\u0021\u0023", "abc")
val df = data.toDF()
val rlike1 = df.filter("value rlike '^\\x20[\\x20-\\x23]+$'")
val rlike2 = df.filter($"value".rlike("^\\x20[\\x20-\\x23]+$"))
val rlike3 = df.filter("value rlike '^\\\\x20[\\\\x20-\\\\x23]+$'")
checkAnswer(rlike1, rlike2)
assert(rlike3.count() == 0)
}
}

case class WithImmutableMap(id: String, map_test: scala.collection.immutable.Map[Long, String])
Expand Down