You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by hu...@apache.org on 2022/10/06 18:27:25 UTC

[superset] branch fix-logs-sqllab created (now c85e9415df)

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

hugh pushed a change to branch fix-logs-sqllab
in repository https://gitbox.apache.org/repos/asf/superset.git


      at c85e9415df yerp

This branch includes the following new commits:

     new be9874f68f save
     new 1699d6e0f9 yerp
     new c85e9415df yerp

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[superset] 02/03: yerp

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch fix-logs-sqllab
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 1699d6e0f99e752d87a876e85025e2bd4ea27003
Author: hughhhh <hu...@gmail.com>
AuthorDate: Thu Oct 6 14:26:47 2022 -0400

    yerp
---
 superset/sql_lab.py              |  8 ++++----
 tests/unit_tests/sql_lab_test.py | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/superset/sql_lab.py b/superset/sql_lab.py
index ed78673c23..3aa0d88fdb 100644
--- a/superset/sql_lab.py
+++ b/superset/sql_lab.py
@@ -209,7 +209,6 @@ def execute_sql_statement(  # pylint: disable=too-many-arguments,too-many-statem
     database: Database = query.database
     db_engine_spec = database.db_engine_spec
 
-    print('in 1')
     parsed_query = ParsedQuery(sql_statement)
     if is_feature_enabled("RLS_IN_SQLLAB"):
         # Insert any applicable RLS predicates
@@ -312,7 +311,8 @@ def execute_sql_statement(  # pylint: disable=too-many-arguments,too-many-statem
                 level=ErrorLevel.ERROR,
             )
         ) from ex
-    except SqlUserError as ex:
+    # todo(hughhhh): cleaner way to group all user exceptions here
+    except SyntaxError as ex:
         raise SupersetErrorException(
             SupersetError(
                 message=ex,
@@ -508,7 +508,7 @@ def execute_sql_statements(  # pylint: disable=too-many-arguments, too-many-loca
             query.set_extra_json_key("progress", msg)
             session.commit()
             try:
-                print('calling execute_sql_statement')
+                print("calling execute_sql_statement")
                 result_set = execute_sql_statement(
                     statement,
                     query,
@@ -521,7 +521,7 @@ def execute_sql_statements(  # pylint: disable=too-many-arguments, too-many-loca
                 payload.update({"status": QueryStatus.STOPPED})
                 return payload
             except Exception as ex:  # pylint: disable=broad-except
-                print('in exc')
+                print("in exc")
                 print(ex)
                 raise ex
                 msg = str(ex)
diff --git a/tests/unit_tests/sql_lab_test.py b/tests/unit_tests/sql_lab_test.py
index 29f45eab68..0bb785d3d5 100644
--- a/tests/unit_tests/sql_lab_test.py
+++ b/tests/unit_tests/sql_lab_test.py
@@ -16,10 +16,13 @@
 # under the License.
 # pylint: disable=import-outside-toplevel, invalid-name, unused-argument, too-many-locals
 
+import pytest
 import sqlparse
 from pytest_mock import MockerFixture
 from sqlalchemy.orm.session import Session
 
+from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
+from superset.exceptions import SupersetErrorException
 from superset.utils.core import override_user
 
 
@@ -216,3 +219,44 @@ def test_sql_lab_insert_rls(
 |  3 |   9 |""".strip()
     )
     assert query.executed_sql == "SELECT c FROM t WHERE (t.c > 5)\nLIMIT 6"
+
+
+def test_execute_sql_statement_sql_user_error(mocker: MockerFixture, app: None) -> None:
+    """
+    Simple test for `execute_sql_statement`.
+    """
+    from superset.sql_lab import execute_sql_statement
+
+    sql_statement = "SELECT 42 AS answer"
+
+    query = mocker.MagicMock()
+    query.limit = 1
+    query.select_as_cta_used = False
+    database = query.database
+    database.allow_dml = False
+    database.apply_limit_to_sql.return_value = "SELECT 42 AS answer LIMIT 2"
+    db_engine_spec = database.db_engine_spec
+
+    from psycopg2.errors import SyntaxError
+
+    db_engine_spec.is_select_query.return_value = True
+    db_engine_spec.fetch_data.side_effect = SyntaxError("foo")
+
+    session = mocker.MagicMock()
+    cursor = mocker.MagicMock()
+    SupersetResultSet = mocker.patch("superset.sql_lab.SupersetResultSet")
+
+    with pytest.raises(SupersetErrorException) as excinfo:
+        execute_sql_statement(
+            sql_statement,
+            query,
+            session=session,
+            cursor=cursor,
+            log_params={},
+            apply_ctas=False,
+        )
+        assert excinfo.value.error == SupersetError(
+            message="foo",
+            error_type=SupersetErrorType.SYNTAX_ERROR,
+            level=ErrorLevel.ERROR,
+        )


[superset] 01/03: save

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch fix-logs-sqllab
in repository https://gitbox.apache.org/repos/asf/superset.git

commit be9874f68ff49fccfe625f35d87845c998fc99ca
Author: hughhhh <hu...@gmail.com>
AuthorDate: Wed Oct 5 15:30:27 2022 -0400

    save
---
 superset/sql_lab.py                     | 18 ++++++++++
 tests/integration_tests/sqllab_tests.py | 64 +++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/superset/sql_lab.py b/superset/sql_lab.py
index ea91133270..ed78673c23 100644
--- a/superset/sql_lab.py
+++ b/superset/sql_lab.py
@@ -29,6 +29,7 @@ import simplejson as json
 from celery import Task
 from celery.exceptions import SoftTimeLimitExceeded
 from flask_babel import gettext as __
+from psycopg2.errors import SyntaxError
 from sqlalchemy.orm import Session
 
 from superset import (
@@ -84,6 +85,10 @@ class SqlLabQueryStoppedException(SqlLabException):
     pass
 
 
+class SqlUserError(SyntaxError):
+    pass
+
+
 def handle_query_error(
     ex: Exception,
     query: Query,
@@ -204,6 +209,7 @@ def execute_sql_statement(  # pylint: disable=too-many-arguments,too-many-statem
     database: Database = query.database
     db_engine_spec = database.db_engine_spec
 
+    print('in 1')
     parsed_query = ParsedQuery(sql_statement)
     if is_feature_enabled("RLS_IN_SQLLAB"):
         # Insert any applicable RLS predicates
@@ -306,6 +312,14 @@ def execute_sql_statement(  # pylint: disable=too-many-arguments,too-many-statem
                 level=ErrorLevel.ERROR,
             )
         ) from ex
+    except SqlUserError as ex:
+        raise SupersetErrorException(
+            SupersetError(
+                message=ex,
+                error_type=SupersetErrorType.SYNTAX_ERROR,
+                level=ErrorLevel.ERROR,
+            )
+        ) from ex
     except Exception as ex:
         # query is stopped in another thread/worker
         # stopping raises expected exceptions which we should skip
@@ -494,6 +508,7 @@ def execute_sql_statements(  # pylint: disable=too-many-arguments, too-many-loca
             query.set_extra_json_key("progress", msg)
             session.commit()
             try:
+                print('calling execute_sql_statement')
                 result_set = execute_sql_statement(
                     statement,
                     query,
@@ -506,6 +521,9 @@ def execute_sql_statements(  # pylint: disable=too-many-arguments, too-many-loca
                 payload.update({"status": QueryStatus.STOPPED})
                 return payload
             except Exception as ex:  # pylint: disable=broad-except
+                print('in exc')
+                print(ex)
+                raise ex
                 msg = str(ex)
                 prefix_message = (
                     f"[Statement {i+1} out of {statement_count}]"
diff --git a/tests/integration_tests/sqllab_tests.py b/tests/integration_tests/sqllab_tests.py
index 0c4019e7d9..b78b33d04e 100644
--- a/tests/integration_tests/sqllab_tests.py
+++ b/tests/integration_tests/sqllab_tests.py
@@ -821,6 +821,70 @@ class TestSqlLab(SupersetTestCase):
             },
         )
 
+    @mock.patch("superset.sql_lab.get_query")
+    @mock.patch("superset.sql_lab.execute_sql_statement")
+    def test_execute_sql_statements_syntax_error(
+        self, mock_execute_sql_statement, mock_get_query
+    ):
+        sql = """
+            -- comment
+            SET @value = 42;
+            SELECT @value AS foo;
+            -- comment
+        """
+        mock_session = mock.MagicMock()
+        mock_query = mock.MagicMock()
+        mock_query.database.allow_run_async = False
+        mock_cursor = mock.MagicMock()
+        mock_query.database.get_sqla_engine.return_value.raw_connection.return_value.cursor.return_value = (
+            mock_cursor
+        )
+        mock_query.database.db_engine_spec.run_multiple_statements_as_one = False
+        mock_get_query.return_value = mock_query
+
+        s2_error = SupersetError(
+                message="foo",
+                error_type=SupersetErrorType.SYNTAX_ERROR,
+                level=ErrorLevel.ERROR,
+            )
+        mock_execute_sql_statement.side_effect = SupersetErrorException(s2_error) 
+        with pytest.raises(SupersetErrorException) as excinfo:
+            execute_sql_statements(
+                query_id=1,
+                rendered_query=sql,
+                return_results=True,
+                store_results=False,
+                session=mock_session,
+                start_time=None,
+                expand_data=False,
+                log_params=None,
+            )
+
+            assert excinfo.value.error == s2_error
+        assert False is True
+
+
+        # mock_execute_sql_statement.assert_has_calls(
+        #     [
+        #         mock.call(
+        #             "SET @value = 42",
+        #             mock_query,
+        #             mock_session,
+        #             mock_cursor,
+        #             None,
+        #             False,
+        #         ),
+        #         mock.call(
+        #             "SELECT @value AS foo",
+        #             mock_query,
+        #             mock_session,
+        #             mock_cursor,
+        #             None,
+        #             False,
+        #         ),
+        #     ]
+        # )
+
     @mock.patch("superset.sql_lab.get_query")
     @mock.patch("superset.sql_lab.execute_sql_statement")
     def test_execute_sql_statements_ctas(


[superset] 03/03: yerp

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch fix-logs-sqllab
in repository https://gitbox.apache.org/repos/asf/superset.git

commit c85e9415df3548abf421b51c198f91e0d1b92389
Author: hughhhh <hu...@gmail.com>
AuthorDate: Thu Oct 6 14:27:07 2022 -0400

    yerp
---
 tests/integration_tests/sqllab_tests.py | 64 ---------------------------------
 1 file changed, 64 deletions(-)

diff --git a/tests/integration_tests/sqllab_tests.py b/tests/integration_tests/sqllab_tests.py
index b78b33d04e..0c4019e7d9 100644
--- a/tests/integration_tests/sqllab_tests.py
+++ b/tests/integration_tests/sqllab_tests.py
@@ -821,70 +821,6 @@ class TestSqlLab(SupersetTestCase):
             },
         )
 
-    @mock.patch("superset.sql_lab.get_query")
-    @mock.patch("superset.sql_lab.execute_sql_statement")
-    def test_execute_sql_statements_syntax_error(
-        self, mock_execute_sql_statement, mock_get_query
-    ):
-        sql = """
-            -- comment
-            SET @value = 42;
-            SELECT @value AS foo;
-            -- comment
-        """
-        mock_session = mock.MagicMock()
-        mock_query = mock.MagicMock()
-        mock_query.database.allow_run_async = False
-        mock_cursor = mock.MagicMock()
-        mock_query.database.get_sqla_engine.return_value.raw_connection.return_value.cursor.return_value = (
-            mock_cursor
-        )
-        mock_query.database.db_engine_spec.run_multiple_statements_as_one = False
-        mock_get_query.return_value = mock_query
-
-        s2_error = SupersetError(
-                message="foo",
-                error_type=SupersetErrorType.SYNTAX_ERROR,
-                level=ErrorLevel.ERROR,
-            )
-        mock_execute_sql_statement.side_effect = SupersetErrorException(s2_error) 
-        with pytest.raises(SupersetErrorException) as excinfo:
-            execute_sql_statements(
-                query_id=1,
-                rendered_query=sql,
-                return_results=True,
-                store_results=False,
-                session=mock_session,
-                start_time=None,
-                expand_data=False,
-                log_params=None,
-            )
-
-            assert excinfo.value.error == s2_error
-        assert False is True
-
-
-        # mock_execute_sql_statement.assert_has_calls(
-        #     [
-        #         mock.call(
-        #             "SET @value = 42",
-        #             mock_query,
-        #             mock_session,
-        #             mock_cursor,
-        #             None,
-        #             False,
-        #         ),
-        #         mock.call(
-        #             "SELECT @value AS foo",
-        #             mock_query,
-        #             mock_session,
-        #             mock_cursor,
-        #             None,
-        #             False,
-        #         ),
-        #     ]
-        # )
-
     @mock.patch("superset.sql_lab.get_query")
     @mock.patch("superset.sql_lab.execute_sql_statement")
     def test_execute_sql_statements_ctas(