internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: getredash/redash

Issue:
schemas queue is missing in default Docker configurations
Because there are existing deployments without it, we will change the queue the schema refresh job is using to the default queue (`celery`) & make it configurable so whoever wants can change it to `schemas` again.
schemas queue is missing in default Docker configurations
Because there are existing deployments without it, we will change the queue the schema refresh job is using to the default queue (`celery`) & make it configurable so whoever wants can change it to `schemas` again.


Discussion:

Resolving patch:
diff --git a/redash/settings/__init__.py b/redash/settings/__init__.py
--- a/redash/settings/__init__.py
+++ b/redash/settings/__init__.py
@@ -47,6 +47,7 @@ def all_settings():
 QUERY_RESULTS_CLEANUP_MAX_AGE = int(os.environ.get("REDASH_QUERY_RESULTS_CLEANUP_MAX_AGE", "7"))
 
 SCHEMAS_REFRESH_SCHEDULE = int(os.environ.get("REDASH_SCHEMAS_REFRESH_SCHEDULE", 30))
+SCHEMAS_REFRESH_QUEUE = os.environ.get("REDASH_SCHEMAS_REFRESH_QUEUE", "celery")
 
 AUTH_TYPE = os.environ.get("REDASH_AUTH_TYPE", "api_key")
 ENFORCE_HTTPS = parse_boolean(os.environ.get("REDASH_ENFORCE_HTTPS", "false"))
diff --git a/redash/tasks/queries.py b/redash/tasks/queries.py
--- a/redash/tasks/queries.py
+++ b/redash/tasks/queries.py
@@ -409,7 +409,7 @@ def refresh_schemas():
         elif ds.org.is_disabled:
             logger.info(u"task=refresh_schema state=skip ds_id=%s reason=org_disabled", ds.id)
         else:
-            refresh_schema.apply_async(args=(ds.id,), queue="schemas")
+            refresh_schema.apply_async(args=(ds.id,), queue=settings.SCHEMAS_REFRESH_QUEUE)
 
     logger.info(u"task=refresh_schemas state=finish total_runtime=%.2f", time.time() - global_start_time)
 


Test patch:
diff --git a/tests/tasks/test_refresh_schemas.py b/tests/tasks/test_refresh_schemas.py
--- a/tests/tasks/test_refresh_schemas.py
+++ b/tests/tasks/test_refresh_schemas.py
@@ -1,6 +1,4 @@
-import datetime
-
-from mock import ANY, call, patch
+from mock import patch
 from tests import BaseTestCase
 
 from redash.tasks import refresh_schemas
@@ -8,7 +6,7 @@
 
 class TestRefreshSchemas(BaseTestCase):
     def test_calls_refresh_of_all_data_sources(self):
-        self.factory.data_source # trigger creation
+        self.factory.data_source  # trigger creation
         with patch('redash.tasks.queries.refresh_schema.apply_async') as refresh_job:
             refresh_schemas()
             refresh_job.assert_called()
Repository: getredash/redash

Issue:
Issues with Query Based Dropdown when the value is numeric
When the values the query returns for the dropdown are numeric, we have two issues:

1. We don’t load the correct value because the value from the URL passed as string, while the actual value is a number.
2. We don’t load results at all, because they fail parameter schema validation (the string doesn't match the values the dropdown has, which are numbers).


Discussion:

Resolving patch:
diff --git a/redash/utils/parameterized_query.py b/redash/utils/parameterized_query.py
--- a/redash/utils/parameterized_query.py
+++ b/redash/utils/parameterized_query.py
@@ -13,7 +13,7 @@ def _pluck_name_and_value(default_column, row):
     name_column = "name" if "name" in row.keys() else default_column.lower()
     value_column = "value" if "value" in row.keys() else default_column.lower()
 
-    return {"name": row[name_column], "value": row[value_column]}
+    return {"name": row[name_column], "value": unicode(row[value_column])}
 
 
 def _load_result(query_id):


Test patch:
diff --git a/tests/utils/test_parameterized_query.py b/tests/utils/test_parameterized_query.py
--- a/tests/utils/test_parameterized_query.py
+++ b/tests/utils/test_parameterized_query.py
@@ -196,11 +196,11 @@ def test_dropdown_values_prefers_name_and_value_columns(self, _):
         "rows": [{"fish": "Clown", "id": 5, "poultry": "Hen"}]})
     def test_dropdown_values_compromises_for_first_column(self, _):
         values = dropdown_values(1)
-        self.assertEquals(values, [{"name": 5, "value": 5}])
+        self.assertEquals(values, [{"name": 5, "value": "5"}])
 
     @patch('redash.utils.parameterized_query._load_result', return_value={
         "columns": [{"name": "ID"}, {"name": "fish"}, {"name": "poultry"}],
         "rows": [{"fish": "Clown", "ID": 5, "poultry": "Hen"}]})
     def test_dropdown_supports_upper_cased_columns(self, _):
         values = dropdown_values(1)
-        self.assertEquals(values, [{"name": 5, "value": 5}])
+        self.assertEquals(values, [{"name": 5, "value": "5"}])
Repository: getredash/redash

Issue:
Support for Presto username and password
Currently the Presto query runner supports username only. We should support password as well.

This probably requires upgrading the PyHive library.


Discussion:

Resolving patch:
diff --git a/redash/query_runner/presto.py b/redash/query_runner/presto.py
--- a/redash/query_runner/presto.py
+++ b/redash/query_runner/presto.py
@@ -56,8 +56,11 @@ def configuration_schema(cls):
                 'username': {
                     'type': 'string'
                 },
+                'password': {
+                    'type': 'string'
+                },
             },
-            'order': ['host', 'protocol', 'port', 'username', 'schema', 'catalog'],
+            'order': ['host', 'protocol', 'port', 'username', 'password', 'schema', 'catalog'],
             'required': ['host']
         }
 
@@ -100,6 +103,7 @@ def run_query(self, query, user):
                 port=self.configuration.get('port', 8080),
                 protocol=self.configuration.get('protocol', 'http'),
                 username=self.configuration.get('username', 'redash'),
+                password=self.configuration.get('password', ''),
                 catalog=self.configuration.get('catalog', 'hive'),
                 schema=self.configuration.get('schema', 'default'))
 


Test patch:
Repository: getredash/redash

Issue:
Error running query: Decimal128('0') is not JSON serializable
ANY mongo query for table with Decimal128 field type fails.
Every mongo query must be aggregated and projected to not return Decimal128 fields.
We are using Decimal128 to work with e.g. transaction amounts so its critical to us.

### Technical details:

* Redash Version: Redash 6.0.0+b8537 (4780bd9c)
* Browser/OS: Safari
* How did you install Redash: Cloud


Discussion:
Need to extend the [JSON encoder](https://github.com/getredash/redash/blob/e9c88ea176387cad6b5188ec7c6061eb78d765fd/redash/query_runner/mongodb.py#L35-L42) to support this object type as well.

Resolving patch:
diff --git a/redash/query_runner/mongodb.py b/redash/query_runner/mongodb.py
--- a/redash/query_runner/mongodb.py
+++ b/redash/query_runner/mongodb.py
@@ -13,6 +13,7 @@
     import pymongo
     from bson.objectid import ObjectId
     from bson.timestamp import Timestamp
+    from bson.decimal128 import Decimal128
     from bson.son import SON
     from bson.json_util import object_hook as bson_object_hook
     enabled = True
@@ -38,7 +39,8 @@ def default(self, o):
             return str(o)
         elif isinstance(o, Timestamp):
             return super(MongoDBJSONEncoder, self).default(o.as_datetime())
-
+        elif isinstance(o, Decimal128):
+            return o.to_decimal()
         return super(MongoDBJSONEncoder, self).default(o)
 
 


Test patch:
Repository: getredash/redash

Issue:
Data Source Lifecycle Events
1. Add event when someone opens the new data source dialog.
2. Add event when someone edits a data source (on the backend).
3. Update Test Connection event to include result of test.


Discussion:

Resolving patch:
diff --git a/redash/handlers/data_sources.py b/redash/handlers/data_sources.py
--- a/redash/handlers/data_sources.py
+++ b/redash/handlers/data_sources.py
@@ -60,6 +60,12 @@ def post(self, data_source_id):
 
             abort(400)
 
+        self.record_event({
+            'action': 'edit',
+            'object_id': data_source.id,
+            'object_type': 'datasource',
+        })
+
         return data_source.to_dict(all=True)
 
     @require_admin
@@ -202,15 +208,18 @@ class DataSourceTestResource(BaseResource):
     def post(self, data_source_id):
         data_source = get_object_or_404(models.DataSource.get_by_id_and_org, data_source_id, self.current_org)
 
+        response = {}
+        try:
+            data_source.query_runner.test_connection()
+        except Exception as e:
+            response = {"message": text_type(e), "ok": False}
+        else:
+            response = {"message": "success", "ok": True}
+
         self.record_event({
             'action': 'test',
             'object_id': data_source_id,
             'object_type': 'datasource',
+            'result': response,
         })
-
-        try:
-            data_source.query_runner.test_connection()
-        except Exception as e:
-            return {"message": text_type(e), "ok": False}
-        else:
-            return {"message": "success", "ok": True}
+        return response


Test patch:
Repository: getredash/redash

Issue:
API Key authentication should take precedence over cookies
Currently when you're logged in as a user, you can't use embeds or shared dashboards for which you do not have access -- even if you provide the API key. This is because the current user is being defined by the session cookie and the API key is being ignored.

We need to change this behavior so the API key takes precedence. From a quick look at [Flask-Login](https://flask-login.readthedocs.io)'s documentation, it seems that it calls `user_loader` first and only if this fails will try the `requets_loader`.

Unless this is configurable, we might need to change `user_loader` to always return `None` and implement our own logic in `request_loader`.


Discussion:

Resolving patch:
diff --git a/redash/authentication/__init__.py b/redash/authentication/__init__.py
--- a/redash/authentication/__init__.py
+++ b/redash/authentication/__init__.py
@@ -41,6 +41,10 @@ def sign(key, path, expires):
 
 @login_manager.user_loader
 def load_user(user_id_with_identity):
+    user = api_key_load_user_from_request(request)
+    if user:
+        return user
+
     org = current_org._get_current_object()
 
     try:


Test patch:
diff --git a/tests/test_authentication.py b/tests/test_authentication.py
--- a/tests/test_authentication.py
+++ b/tests/test_authentication.py
@@ -127,6 +127,19 @@ def test_user_api_key(self):
             self.assertEqual(user.id, hmac_load_user_from_request(request).id)
 
 
+class TestSessionAuthentication(BaseTestCase):
+    def test_prefers_api_key_over_session_user_id(self):
+        user = self.factory.create_user()
+        query = self.factory.create_query(user=user)
+
+        other_org = self.factory.create_org()
+        other_user = self.factory.create_user(org=other_org)
+        models.db.session.flush()
+
+        rv = self.make_request('get', '/api/queries/{}?api_key={}'.format(query.id, query.api_key), user=other_user)
+        self.assertEqual(rv.status_code, 200)
+
+
 class TestCreateAndLoginUser(BaseTestCase):
     def test_logins_valid_user(self):
         user = self.factory.create_user(email=u'test@example.com')
Repository: getredash/redash

Issue:
JIRA setup: change password field name to "API Token"
While a password can be used there, it's not recommended and eventually will be deprecated. 


Discussion:
#3765

Resolving patch:
diff --git a/redash/query_runner/jql.py b/redash/query_runner/jql.py
--- a/redash/query_runner/jql.py
+++ b/redash/query_runner/jql.py
@@ -144,7 +144,7 @@ class JiraJQL(BaseHTTPQueryRunner):
     requires_authentication = True
     url_title = 'JIRA URL'
     username_title = 'Username'
-    password_title = 'Password'
+    password_title = 'API Token'
 
     @classmethod
     def name(cls):


Test patch:
Repository: getredash/redash

Issue:
Change PagerDuty's default summary text
Currently PagerDuty's Alert destination default summary text uses the query id and name. We should change it to use the alert name as it's usually better explains what the alert is.

While #4153 implements ability to customize the summary text, it's good to have a saner default regardless.

(If #4153 is not merged before implementing, should be implemented based on its branch)


Discussion:
Hi @arikfr 
I believe https://github.com/getredash/redash/blob/74beed80d20d858b51b5560e7984b20d5d2c874e/redash/destinations/pagerduty.py#L15 this needs to be changed. However, I could not find any accessors of `notify()` function (maybe a problem with my IDE), so I was unable to find the `alert` object properties. Can I assume I can get the alert name by `alert.name`?

And also, I would have to change https://github.com/getredash/redash/blob/74beed80d20d858b51b5560e7984b20d5d2c874e/redash/destinations/pagerduty.py#L49 to something like 
```
self.DESCRIPTION_STR.format(alert_name=alert.name)
```

So can I assume, `alert.name` will always be non-null or do I need to involve checks?
Thanks!
So, I dug more and found a few examples of `alert.name` in `redash/destination/`

Resolving patch:
diff --git a/redash/destinations/pagerduty.py b/redash/destinations/pagerduty.py
--- a/redash/destinations/pagerduty.py
+++ b/redash/destinations/pagerduty.py
@@ -12,7 +12,7 @@
 class PagerDuty(BaseDestination):
 
     KEY_STRING = '{alert_id}_{query_id}'
-    DESCRIPTION_STR = u'Alert - Redash Query #{query_id}: {query_name}'
+    DESCRIPTION_STR = u'Alert: {alert_name}'
 
     @classmethod
     def enabled(cls):
@@ -29,7 +29,7 @@ def configuration_schema(cls):
                 },
                 'description': {
                     'type': 'string',
-                    'title': 'Description for the event, defaults to query',
+                    'title': 'Description for the event, defaults to alert name',
                 }
             },
             "required": ["integration_key"]
@@ -46,7 +46,7 @@ def notify(self, alert, query, user, new_state, app, host, options):
         elif options.get('description'):
             default_desc = options.get('description')
         else:
-            default_desc = self.DESCRIPTION_STR.format(query_id=query.id, query_name=query.name)
+            default_desc = self.DESCRIPTION_STR.format(alert_name=alert.name)
 
         incident_key = self.KEY_STRING.format(alert_id=alert.id, query_id=query.id)
         data = {


Test patch:
Repository: getredash/redash

Issue:
Make Cypress tests work with Cypress@3.6.0
Running our tests with Cypress@3.5.0 doesn't work. Need to figure out what happened, until then pinning the version to 3.4.1 (#4284).


Discussion:

Resolving patch:
diff --git a/redash/app.py b/redash/app.py
--- a/redash/app.py
+++ b/redash/app.py
@@ -21,7 +21,6 @@ def __init__(self, *args, **kwargs):
 
 def create_app():
     from . import authentication, extensions, handlers, limiter, mail, migrate, security
-    from .handlers import chrome_logger
     from .handlers.webpack import configure_webpack
     from .metrics import request as request_metrics
     from .models import db, users
@@ -44,7 +43,6 @@ def create_app():
     handlers.init_app(app)
     configure_webpack(app)
     extensions.init_app(app)
-    chrome_logger.init_app(app)
     users.init_app(app)
 
     return app
diff --git a/redash/handlers/chrome_logger.py b/redash/handlers/chrome_logger.py
deleted file mode 100644
--- a/redash/handlers/chrome_logger.py
+++ /dev/null
@@ -1,54 +0,0 @@
-import time
-import chromelogger
-from flask import g, request
-from flask_sqlalchemy import get_debug_queries
-
-
-def log_queries():
-    total_duration = 0.0
-    queries_count = 0
-
-    chromelogger.group("SQL Queries")
-
-    for q in get_debug_queries():
-        total_duration += q.duration
-        queries_count += 1
-        chromelogger.info(q.statement % q.parameters)
-        chromelogger.info("Runtime: {:.2f}ms".format(1000 * q.duration))
-
-    chromelogger.info("{} queries executed in {:.2f}ms.".format(queries_count, total_duration*1000))
-
-    chromelogger.group_end("SQL Queries")
-
-
-def chrome_log(response):
-    request_duration = (time.time() - g.start_time) * 1000
-    queries_duration = g.get('queries_duration', 0.0)
-    queries_count = g.get('queries_count', 0)
-
-    group_name = '{} {} ({}, {:.2f}ms runtime, {} queries in {:.2f}ms)'.format(
-        request.method, request.path, response.status_code, request_duration, queries_count, queries_duration)
-
-    chromelogger.group_collapsed(group_name)
-
-    endpoint = (request.endpoint or 'unknown').replace('.', '_')
-    chromelogger.info('Endpoint: {}'.format(endpoint))
-    chromelogger.info('Content Type: {}'.format(response.content_type))
-    chromelogger.info('Content Length: {}'.format(response.content_length or -1))
-
-    log_queries()
-
-    chromelogger.group_end(group_name)
-
-    header = chromelogger.get_header()
-    if header is not None:
-        response.headers.add(*header)
-
-    return response
-
-
-def init_app(app):
-    if not app.debug:
-        return
-
-    app.after_request(chrome_log)


Test patch:
Repository: getredash/redash

Issue:
Password Auth enabling itself when using LDAP
### Issue Summary

When using LDAP for auth, the checkbox for "Password Login Enabled" in settings becomes greyed out. However, when changing any other setting on that page and clicking save, the "Password Login Enabled" gets enabled. I can't find any way to them disable it other than doing so manually in the Postgres 'organizations' table.




Discussion:

Resolving patch:
diff --git a/redash/handlers/authentication.py b/redash/handlers/authentication.py
--- a/redash/handlers/authentication.py
+++ b/redash/handlers/authentication.py
@@ -292,6 +292,7 @@ def client_config():
         "dashboardRefreshIntervals": settings.DASHBOARD_REFRESH_INTERVALS,
         "queryRefreshIntervals": settings.QUERY_REFRESH_INTERVALS,
         "googleLoginEnabled": settings.GOOGLE_OAUTH_ENABLED,
+        "ldapLoginEnabled": settings.LDAP_LOGIN_ENABLED,
         "pageSize": settings.PAGE_SIZE,
         "pageSizeOptions": settings.PAGE_SIZE_OPTIONS,
         "tableCellMaxJSONSize": settings.TABLE_CELL_MAX_JSON_SIZE,


Test patch:
Repository: getredash/redash

Issue:
TreasureData getSchema fails when setting non-default region
<!--
#####################################################################
#
# Need support? USE THE FORUM! https://discuss.redash.io/c/support.
#
# Don't have steps to reproduce and actually not sure it's a bug?
# Use the forum! https://discuss.redash.io/c/support.
#
#####################################################################

**Got an idea for a new feature?** Check if it isn't on the roadmap already: https://bit.ly/redash-roadmap and start a new discussion in the features category: https://discuss.redash.io/c/feature-requests 🌟.

Found a bug? Please fill out the sections below... thank you 👍

Found a security vulnerability? Please email security@redash.io to report any security vulnerabilities. We will acknowledge receipt of your vulnerability and strive to send you regular updates about our progress. If you're curious about the status of your disclosure please feel free to email us again. If you want to encrypt your disclosure email, you can use this PGP key.

-->

### Issue Summary

There are some regions in Treasure Data, but getSchema alsways fails when setting non-default region.

### Steps to Reproduce

1. Set datasource using non-default region (e.g. Tokyo region)
2. Push schema refresh then "Schema refresh failed" error occurs

### Technical details:

* Redash Version: confirmed v5.0.2
* Browser/OS: any Browsers/OSs
* How did you install Redash: from Amazon AMI

### Details

When accessing Treasure Data to get schema, always default region will be set because the parameter is not prepared.
https://github.com/getredash/redash/blob/6c364369bb0eb98e2191c2e502fed72abe5a74c7/redash/query_runner/treasuredata.py#L82


Discussion:
Treasure Data team will take care of this.
> Treasure Data team will take care of this.

Thanks!

Resolving patch:
diff --git a/redash/query_runner/treasuredata.py b/redash/query_runner/treasuredata.py
--- a/redash/query_runner/treasuredata.py
+++ b/redash/query_runner/treasuredata.py
@@ -68,7 +68,7 @@ def get_schema(self, get_stats=False):
         schema = {}
         if self.configuration.get("get_schema", False):
             try:
-                with tdclient.Client(self.configuration.get("apikey")) as client:
+                with tdclient.Client(self.configuration.get("apikey"),endpoint=self.configuration.get("endpoint")) as client:
                     for table in client.tables(self.configuration.get("db")):
                         table_name = "{}.{}".format(
                             self.configuration.get("db"), table.name


Test patch:
Repository: getredash/redash

Issue:
Databricks Data Source Broken
<!--

We use GitHub only for bug reports 🐛

Anything else should be posted to https://discuss.redash.io 👫

🚨For support, help & questions use https://discuss.redash.io/c/support
💡For feature requests & ideas use https://discuss.redash.io/c/feature-requests

**Found a security vulnerability?** Please email security@redash.io to report any security vulnerabilities. We will acknowledge receipt of your vulnerability and strive to send you regular updates about our progress. If you're curious about the status of your disclosure please feel free to email us again. If you want to encrypt your disclosure email, you can use this PGP key.

-->

### Issue Summary
Databricks data source does not work. The authorization token for Databricks data source needs to be converted into a byte string as it currently raises `TypeError: a bytes-like object is required, not 'str'`
Calling `.encode()` to transform to a byte string makes the data source work.

https://github.com/getredash/redash/blob/b089f5f0eff9b047c093dcc7abbd0ae5bfcf643c/redash/query_runner/databricks.py#L51-L52



### Steps to Reproduce

1. Create a Databricks data source
2. Run test connection and you will get the message: a bytes-like object is required, not 'str'

Any other info e.g. Why do you consider this to be a bug? What did you expect to happen instead?

### Technical details:

* Redash Version: master
* Browser/OS: Chrome
* How did you install Redash: Docker development environment



Discussion:

Resolving patch:
diff --git a/redash/query_runner/databricks.py b/redash/query_runner/databricks.py
--- a/redash/query_runner/databricks.py
+++ b/redash/query_runner/databricks.py
@@ -48,8 +48,8 @@ def _get_connection(self):
         transport = THttpClient.THttpClient(http_uri)
 
         password = self.configuration.get("http_password", "")
-        auth = base64.b64encode("token:" + password)
-        transport.setCustomHeaders({"Authorization": "Basic " + auth})
+        auth = base64.b64encode(b"token:" + password.encode("ascii"))
+        transport.setCustomHeaders({"Authorization": "Basic " + auth.decode()})
 
         connection = hive.connect(thrift_transport=transport)
         return connection
diff --git a/redash/query_runner/hive_ds.py b/redash/query_runner/hive_ds.py
--- a/redash/query_runner/hive_ds.py
+++ b/redash/query_runner/hive_ds.py
@@ -223,8 +223,8 @@ def _get_connection(self):
         username = self.configuration.get("username", "")
         password = self.configuration.get("http_password", "")
         if username or password:
-            auth = base64.b64encode(username + ":" + password)
-            transport.setCustomHeaders({"Authorization": "Basic " + auth})
+            auth = base64.b64encode(username.encode("ascii") + b":" + password.encode("ascii"))
+            transport.setCustomHeaders({"Authorization": "Basic " + auth.decode()})
 
         # create connection
         connection = hive.connect(thrift_transport=transport)


Test patch: