superset-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <>
Subject [GitHub] [incubator-superset] bearcage commented on a change in pull request #7422: [WIP] Add `validate_sql_json` endpoint for checking that a given sql query is valid for the chosen database
Date Thu, 02 May 2019 17:06:04 GMT
bearcage commented on a change in pull request #7422: [WIP] Add `validate_sql_json` endpoint
for checking that a given sql query is valid for the chosen database

 File path: superset/views/
 @@ -2503,6 +2504,61 @@ def stop_query(self):
         return self.json_response('OK')
+    @has_access_api
+    @expose('/validate_sql_json/', methods=['POST', 'GET'])
+    @log_this
+    def validate_sql_json(self):
+        """Validates that arbitrary sql is acceptable for the given database.
+        Returns a list of error/warning annotations as json.
+        """
+        sql = request.form.get('sql')
+        database_id = request.form.get('database_id')
+        schema = request.form.get('schema') or None
+        template_params = json.loads(
+            request.form.get('templateParams') or '{}')
+        if len(template_params) > 0:
+            # TODO: factor the Database object out of template rendering
+            #       or provide it as mydb so we can render template params
+            #       without having to also persist a Query ORM object.
+            return json_error_response(
+                'SQL validation does not support template parameters')
+        session = db.session()
+        mydb = session.query(models.Database).filter_by(id=database_id).first()
+        if not mydb:
+            json_error_response(
+                'Database with id {} is missing.'.format(database_id))
+        spec = mydb.db_engine_spec
+        if not spec.engine in SQL_VALIDATORS_BY_ENGINE:
+            return json_error_response(
+                'no SQL validator is configured for {}'.format(spec.engine))
+        validator = SQL_VALIDATORS_BY_ENGINE[spec.engine]
+        try:
+            timeout = config.get('SQLLAB_VALIDATION_TIMEOUT')
+            timeout_msg = (
+                f'The query exceeded the {timeout} seconds timeout.')
+            with utils.timeout(seconds=timeout,
+                                error_message=timeout_msg):
+                errors = validator.validate(sql, schema, mydb)
+            payload = json.dumps(
+                [err.to_dict() for err in errors],
+                default=utils.pessimistic_json_iso_dttm_ser,
+                ignore_nan=True,
+                encoding=None,
+            )
+            return json_success(payload)
+        except Exception as e:
+            logging.exception(e)
+            msg = _(
+                'Failed to validate your SQL query text. Please check that '
+                f'you have configured the {} validator '
+                'correctly and that any services it depends on are up. '
+                f'Exception: {e}')
+            return json_error_response(f'{msg}')
 Review comment:
   This condition's raised whenever the validator excepts out for any reason we didn't predict
— it could be a bad request, but the most likely reason to end up on this codepath is that
the validator itself errored out. For presto that probably means the backing DB 5xx'd us.
   On the client side I'm showing this as a warning box in place of the control that normally
shows you what the error text is, saying `The server failed to validate your query using ${validator_name}`
— just to let you know "hey, the reason you're not seeing errors highlighted isn't that
your query is good, it's just that we don't know if it's good or bad"
   EDIT: I think 400 is good for the other currently-error returns, but this one should stay
a 5xx since it's legitimately a server error

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:

With regards,
Apache Git Services

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message