Uh oh!
There was an error while loading. Please reload this page.
[SPARK-21261][DOCS]SQL Regex document fix - #18477
Conversation
gf53520
commented
Jun 30, 2017
test this please |
There was a problem hiding this comment.
There is another example that needs the same change near the end of the file too.
There was a problem hiding this comment.
Do we need to fix this? I remember in the doc, we use unescaped characters.
There was a problem hiding this comment.
@viirya I'm not an expert here, but reading the docs on line 160, I think this needs to be escaped in order to be consistent with Spark 2 default behavior? my assumption was that this was just never updated.
There was a problem hiding this comment.
Hmm, when I wrote the docs on line 160, I was suggested to use unescaped characters.
Since Spark 2.0, string literals (including regex patterns) are unescaped in our SQL parser. For example, to match "\abc", a regular expression for
regexpcan be "^\abc$".
Actually, you need to write like this in spark-shell:
scala> sql("SELECT like('\\\\abc', '\\\\\\\\abc')").show
+---------------+
|\abc LIKE \\abc|
+---------------+
| true|
+---------------+
scala> sql("SELECT regexp_replace('100-200', '(\\\\d+)', 'num')").show
+-----------------------------------+
|regexp_replace(100-200, (\d+), num)|
+-----------------------------------+
| num-num|
+-----------------------------------+
The behavior of Spark 2 when parsing SQL string literal reads \\\\abc as \abc and (\\\\d+) as (\d+) in spark-shell.
But in spark-sql, you write the queries like this:
spark-sql> SELECT like('\\abc', '\\\\abc');
true
Time taken: 0.061 seconds, Fetched 1 row(s)
spark-sql> SELECT regexp_replace('100-200', '(\\d+)', 'num');
num-num
Time taken: 0.117 seconds, Fetched 1 row(s)
So depending how the shell environment processes string escaping, the query looks different. In the docs, it seems to me that writing in unescaped style can avoid this confusion?
There was a problem hiding this comment.
Is the better fix to make it clear that this example uses unescaped style @viirya ?
There was a problem hiding this comment.
I add spark-sql and scala to make it clear.
gatorsmile
commented
Oct 24, 2017
@visaxin Could you address the comment? |
visaxin
commented
Oct 24, 2017
@gatorsmile Done |
| spark-sql> SELECT _FUNC_('100-200', '(\\d+)-(\\d+)', 1); | ||
| 100 | ||
| scala> SELECT _FUNC_('100-200', '(\\\\d+)-(\\\\d+)', 1); |
There was a problem hiding this comment.
scala> spark.sql("SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1)").collect()
| 100 | ||
| scala> SELECT _FUNC_('100-200', '(\\\\d+)-(\\\\d+)', 1); | ||
| 100 |
| num-num | ||
| scala> SELECT _FUNC_('100-200', '(\\\\d+)', 'num'); | ||
| num-num |
There was a problem hiding this comment.
scala> spark.sql("SELECT regexp_replace('100-200', '(\\d+)', 'num')").collect()
Array([num-num])
gatorsmile
commented
Oct 24, 2017
LGTM except the above three comments. |
srowen
commented
May 11, 2018
@visaxin this one is old but could you update it per the last review comments? |
AmplabJenkins
commented
Jun 9, 2018
Can one of the admins verify this patch? |
srowen
commented
Jul 2, 2018
Ping @visaxin |
HyukjinKwon
commented
Jul 16, 2018
Ping @visaxin |
srowen
commented
Jul 18, 2018
I took this over at #21808 |
## What changes were proposed in this pull request? Fix regexes in spark-sql command examples. This takes over #18477 ## How was this patch tested? Existing tests. I verified the existing example doesn't work in spark-sql, but new ones does. Author: Sean Owen <srowen@gmail.com> Closes#21808 from srowen/SPARK-21261.
Closesapache#17422Closesapache#17619Closesapache#18034Closesapache#18229Closesapache#18268Closesapache#17973Closesapache#18125Closesapache#18918Closesapache#19274Closesapache#19456Closesapache#19510Closesapache#19420Closesapache#20090Closesapache#20177Closesapache#20304Closesapache#20319Closesapache#20543Closesapache#20437Closesapache#21261Closesapache#21726Closesapache#14653Closesapache#13143Closesapache#17894Closesapache#19758Closesapache#12951Closesapache#17092Closesapache#21240Closesapache#16910Closesapache#12904Closesapache#21731Closesapache#21095 Added: Closesapache#19233Closesapache#20100Closesapache#21453Closesapache#21455Closesapache#18477 Added: Closesapache#21812Closesapache#21787 Author: hyukjinkwon <gurwls223@apache.org> Closesapache#21781 from HyukjinKwon/closing-prs.
SQL regex docs change:
SELECT _FUNC_('100-200', '(\d+)', 'num') => SELECT _FUNC_('100-200', '(\\d+)', 'num')