Repository: getredash/redash

Issue:
Query execution fails if user name has unicode characters
```
[2016-06-08 21:16:08,491: ERROR/MainProcess] Task redash.tasks.execute_query[82e3c23b-ceb7-4ed1-81c2-98f8601a6b14] raised unexpected: UnicodeEncodeError('ascii', u'/* Username: \u4f0a\u85e4\u76f4\u4e5f, Task ID: 82e3c23b-ceb7-4ed1-81c2-98f8601a6b14, Query ID: 6, Queue: queries, Query Hash: f1fc50d12a0b5cd0279b42db51130dbe */ SELECT TOP 100 *\nFROM CurrentDB.dbo.Booking', 13, 17, 'ordinal not in range(128)')
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/opt/redash/redash.0.11.0.b1903/redash/tasks/base.py", line 13, in __call__
    return super(BaseTask, self).__call__(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 437, in __protected_call__
    return self.run(*args, **kwargs)
  File "/opt/redash/redash.0.11.0.b1903/redash/tasks/queries.py", line 445, in execute_query
    return QueryExecutor(self, query, data_source_id, metadata).run()
  File "/opt/redash/redash.0.11.0.b1903/redash/tasks/queries.py", line 395, in run
    data, error = query_runner.run_query(annotated_query)
  File "/opt/redash/redash.0.11.0.b1903/redash/query_runner/mssql.py", line 125, in run_query
    cursor.execute(query)
  File "pymssql.pyx", line 445, in pymssql.Cursor.execute (pymssql.c:6242)
  File "_mssql.pyx", line 998, in _mssql.MSSQLConnection.execute_query (_mssql.c:10085)
  File "_mssql.pyx", line 1029, in _mssql.MSSQLConnection.execute_query (_mssql.c:9964)
  File "_mssql.pyx", line 1149, in _mssql.MSSQLConnection.format_and_run_query (_mssql.c:11030)
  File "_mssql.pyx", line 200, in _mssql.ensure_bytes (_mssql.c:2544)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-16: ordinal not in range(128)
```
### Steps to Reproduce
- My user name is "伊藤直也"
- Using mssql query runner
- Querying "select top 100 \* from sometable"

When using BigQuery query runner, This error doesn't happen.
### Technical details:
- Redash Version: v0.10.1.b1834,  0.11.0 - RC
- Browser/OS: Google Chrome/OSX / Server: ubuntu Linux 16.04 LTS
- How did you install Redash: used `setup/ubuntu/bootstrap.sh`



Discussion:

Resolving patch:
diff --git a/redash/query_runner/mssql.py b/redash/query_runner/mssql.py
--- a/redash/query_runner/mssql.py
+++ b/redash/query_runner/mssql.py
@@ -54,6 +54,11 @@ def configuration_schema(cls):
                     "default": "7.0",
                     "title": "TDS Version"
                 },
+                "charset": {
+                    "type": "string",
+                    "default": "UTF-8",
+                    "title": "Character Set"
+                },
                 "db": {
                     "type": "string",
                     "title": "Database Name"
@@ -120,11 +125,16 @@ def run_query(self, query):
             db = self.configuration['db']
             port = self.configuration.get('port', 1433)
             tds_version = self.configuration.get('tds_version', '7.0')
+            charset = self.configuration.get('charset', 'UTF-8')
 
             if port != 1433:
                 server = server + ':' + str(port)
 
-            connection = pymssql.connect(server=server, user=user, password=password, database=db, tds_version=tds_version)
+            connection = pymssql.connect(server=server, user=user, password=password, database=db, tds_version=tds_version, charset=charset)
+
+            if isinstance(query, unicode):
+                query = query.encode(charset)
+
             cursor = connection.cursor()
             logger.debug("SqlServer running query: %s", query)
 


Test patch: