You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2020/05/14 12:12:51 UTC

[GitHub] [incubator-superset] villebro commented on a change in pull request #9752: fix(mssql): reverts #9644 and displays a better error msg

villebro commented on a change in pull request #9752:
URL: https://github.com/apache/incubator-superset/pull/9752#discussion_r425084363



##########
File path: superset/db_engine_specs/mssql.py
##########
@@ -85,6 +84,10 @@ def get_sqla_column_type(cls, type_: str) -> Optional[TypeEngine]:
         return None
 
     @classmethod
-    def apply_limit_to_sql(cls, sql: str, limit: int, database: "Database") -> str:
-        new_sql = ParsedQuery(sql).set_alias()
-        return super().apply_limit_to_sql(new_sql, limit, database)
+    def extract_error_message(cls, ex: Exception) -> str:
+        if str(ex).startswith("(8155"):
+            return (
+                f"{cls.engine} error: All your SQL functions need to "
+                "have alias on MSSQL. For example: SELECT COUNT(*) AS C1 FROM TABLE1"

Review comment:
       ```suggestion
                   "have an alias on MSSQL. For example: SELECT COUNT(*) AS C1 FROM TABLE1"
   ```

##########
File path: superset/db_engine_specs/mssql.py
##########
@@ -85,6 +84,10 @@ def get_sqla_column_type(cls, type_: str) -> Optional[TypeEngine]:
         return None
 
     @classmethod
-    def apply_limit_to_sql(cls, sql: str, limit: int, database: "Database") -> str:
-        new_sql = ParsedQuery(sql).set_alias()
-        return super().apply_limit_to_sql(new_sql, limit, database)
+    def extract_error_message(cls, ex: Exception) -> str:
+        if str(ex).startswith("(8155"):

Review comment:
       ```suggestion
           if str(ex).startswith("(8155,"):
   ```
   You never know if they introduce `^\(8155\d+,`..

##########
File path: tests/db_engine_specs/mssql_tests.py
##########
@@ -97,64 +94,28 @@ def test_convert_dttm(self):
         for actual, expected in test_cases:
             self.assertEqual(actual, expected)
 
-    def test_apply_limit(self):
-        def compile_sqla_query(qry: Select, schema: Optional[str] = None) -> str:
-            return str(
-                qry.compile(
-                    dialect=mssql.dialect(), compile_kwargs={"literal_binds": True}
-                )
-            )
-
-        database = Database(
-            database_name="mssql_test",
-            sqlalchemy_uri="mssql+pymssql://sa:Password_123@localhost:1433/msdb",
+    def test_extract_error_message(self):
+        test_mssql_exception = Exception(
+            "(8155, b\"No column name was specified for column 1 of 'inner_qry'."
+            "DB-Lib error message 20018, severity 16:\\nGeneral SQL Server error: "
+            'Check messages from the SQL Server\\n")'
         )
-        db.session.add(database)
-        db.session.commit()
-
-        with mock.patch.object(database, "compile_sqla_query", new=compile_sqla_query):
-            test_sql = "SELECT COUNT(*) FROM FOO_TABLE"
-
-            limited_sql = MssqlEngineSpec.apply_limit_to_sql(test_sql, 1000, database)
-
-            expected_sql = (
-                "SELECT TOP 1000 * \n"
-                "FROM (SELECT COUNT(*) AS COUNT_1 FROM FOO_TABLE) AS inner_qry"
-            )
-            self.assertEqual(expected_sql, limited_sql)
-
-            test_sql = "SELECT COUNT(*), SUM(id) FROM FOO_TABLE"
-            limited_sql = MssqlEngineSpec.apply_limit_to_sql(test_sql, 1000, database)
-
-            expected_sql = (
-                "SELECT TOP 1000 * \n"
-                "FROM (SELECT COUNT(*) AS COUNT_1, SUM(id) AS SUM_2 FROM FOO_TABLE) "
-                "AS inner_qry"
-            )
-            self.assertEqual(expected_sql, limited_sql)
-
-            test_sql = "SELECT COUNT(*), FOO_COL1 FROM FOO_TABLE GROUP BY FOO_COL1"
-            limited_sql = MssqlEngineSpec.apply_limit_to_sql(test_sql, 1000, database)
-
-            expected_sql = (
-                "SELECT TOP 1000 * \n"
-                "FROM (SELECT COUNT(*) AS COUNT_1, "
-                "FOO_COL1 FROM FOO_TABLE GROUP BY FOO_COL1)"
-                " AS inner_qry"
-            )
-            self.assertEqual(expected_sql, limited_sql)
-
-            test_sql = "SELECT COUNT(*), COUNT(*) FROM FOO_TABLE"
-            limited_sql = MssqlEngineSpec.apply_limit_to_sql(test_sql, 1000, database)
-            expected_sql = (
-                "SELECT TOP 1000 * \n"
-                "FROM (SELECT COUNT(*) AS COUNT_1, COUNT(*) AS COUNT_2 FROM FOO_TABLE)"
-                " AS inner_qry"
-            )
-            self.assertEqual(expected_sql, limited_sql)
+        error_message = MssqlEngineSpec.extract_error_message(test_mssql_exception)
+        expected_message = (
+            "mssql error: All your SQL functions need to "
+            "have alias on MSSQL. For example: SELECT COUNT(*) AS C1 FROM TABLE1"

Review comment:
       ```suggestion
               "have an alias on MSSQL. For example: SELECT COUNT(*) AS C1 FROM TABLE1"
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org