Skip to content

[SPARK-21261][DOCS]SQL Regex document fix - #18477

Closed
visaxin wants to merge 1 commit into
apache:masterfrom
visaxin:FixSQLDocuments
Closed

[SPARK-21261][DOCS]SQL Regex document fix#18477
visaxin wants to merge 1 commit into
apache:masterfrom
visaxin:FixSQLDocuments

Conversation

@visaxin

@visaxinvisaxin commented Jun 30, 2017

Copy link
Copy Markdown

SQL regex docs change:
SELECT _FUNC_('100-200', '(\d+)', 'num') => SELECT _FUNC_('100-200', '(\\d+)', 'num')

@gf53520

Copy link
Copy Markdown
Contributor

test this please

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is another example that needs the same change near the end of the file too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to fix this? I remember in the doc, we use unescaped characters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 regexp can 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the better fix to make it clear that this example uses unescaped style @viirya ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, if we can.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I add spark-sql and scala to make it clear.

@gatorsmile

Copy link
Copy Markdown
Member

@visaxin Could you address the comment?

@visaxin

Copy link
Copy Markdown
Author

@gatorsmile Done

spark-sql> SELECT _FUNC_('100-200', '(\\d+)-(\\d+)', 1);
100

scala> SELECT _FUNC_('100-200', '(\\\\d+)-(\\\\d+)', 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

scala> spark.sql("SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1)").collect()

100

scala> SELECT _FUNC_('100-200', '(\\\\d+)-(\\\\d+)', 1);
100

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Array([100])

num-num

scala> SELECT _FUNC_('100-200', '(\\\\d+)', 'num');
num-num

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

scala> spark.sql("SELECT regexp_replace('100-200', '(\\d+)', 'num')").collect()
Array([num-num])

@gatorsmile

Copy link
Copy Markdown
Member

LGTM except the above three comments.

@srowen

Copy link
Copy Markdown
Member

@visaxin this one is old but could you update it per the last review comments?

@AmplabJenkins

Copy link
Copy Markdown

Can one of the admins verify this patch?

@srowen

Copy link
Copy Markdown
Member

Ping @visaxin

@HyukjinKwon

Copy link
Copy Markdown
Member

Ping @visaxin

@srowen

Copy link
Copy Markdown
Member

I took this over at #21808
I don't think this change is even right as it introduces scala-shell examples.

@srowensrowen mentioned this pull request Jul 18, 2018
asfgit pushed a commit that referenced this pull request Jul 18, 2018
## 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.
zifeif2 pushed a commit to zifeif2/spark that referenced this pull request Nov 22, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@visaxin@gf53520@gatorsmile@srowen@AmplabJenkins@HyukjinKwon@viirya