You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jo...@apache.org on 2022/01/12 01:05:58 UTC

[superset] branch master updated: fix: Workaround for sqlparse issue #652 (#17995)

This is an automated email from the ASF dual-hosted git repository.

johnbodley pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 63ca09e  fix: Workaround for sqlparse issue #652 (#17995)
63ca09e is described below

commit 63ca09e345ef8a474625d7baae4f0c4f0a44c0d1
Author: John Bodley <45...@users.noreply.github.com>
AuthorDate: Wed Jan 12 14:03:56 2022 +1300

    fix: Workaround for sqlparse issue #652 (#17995)
    
    * fix: Workaround for sqlparse issue #652
    
    * Update superset/sql_parse.py
    
    Co-authored-by: Ville Brofeldt <33...@users.noreply.github.com>
    
    * Update sql_parse.py
    
    Co-authored-by: John Bodley <jo...@airbnb.com>
    Co-authored-by: Ville Brofeldt <33...@users.noreply.github.com>
---
 superset/sql_parse.py               | 11 +++++++++++
 tests/unit_tests/sql_parse_tests.py |  6 ++++++
 2 files changed, 17 insertions(+)

diff --git a/superset/sql_parse.py b/superset/sql_parse.py
index 1130763..26b4bfd 100644
--- a/superset/sql_parse.py
+++ b/superset/sql_parse.py
@@ -15,6 +15,7 @@
 # specific language governing permissions and limitations
 # under the License.
 import logging
+import re
 from dataclasses import dataclass
 from enum import Enum
 from typing import List, Optional, Set
@@ -41,6 +42,16 @@ CTE_PREFIX = "CTE__"
 logger = logging.getLogger(__name__)
 
 
+# TODO: Workaround for https://github.com/andialbrecht/sqlparse/issues/652.
+sqlparse.keywords.SQL_REGEX.insert(
+    0,
+    (
+        re.compile(r"'(''|\\\\|\\|[^'])*'", sqlparse.keywords.FLAGS).match,
+        sqlparse.tokens.String.Single,
+    ),
+)
+
+
 class CtasMethod(str, Enum):
     TABLE = "TABLE"
     VIEW = "VIEW"
diff --git a/tests/unit_tests/sql_parse_tests.py b/tests/unit_tests/sql_parse_tests.py
index f405b9f..92917df 100644
--- a/tests/unit_tests/sql_parse_tests.py
+++ b/tests/unit_tests/sql_parse_tests.py
@@ -1199,3 +1199,9 @@ def test_validate_filter_clause_comment():
 def test_validate_filter_clause_subquery_comment():
     with pytest.raises(QueryClauseValidationException):
         validate_filter_clause("(1 = 1 -- comment\n)")
+
+
+def test_sqlparse_issue_652():
+    stmt = sqlparse.parse(r"foo = '\' AND bar = 'baz'")[0]
+    assert len(stmt.tokens) == 5
+    assert str(stmt.tokens[0]) == "foo = '\\'"