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

Issue:
error running query : ** '>' is not supported between instance of NoneType and 'int'
Issue Summary:
Database = Oracle 12c

`select count(*) from table `

throwing the following error

`error running query : ** '>' is not supported between instance of NoneType and 'int'`

Redash v9.0.0-alpha(dev)





Discussion:

Resolving patch:
diff --git a/redash/query_runner/oracle.py b/redash/query_runner/oracle.py
--- a/redash/query_runner/oracle.py
+++ b/redash/query_runner/oracle.py
@@ -35,7 +35,11 @@ class Oracle(BaseSQLQueryRunner):
     @classmethod
     def get_col_type(cls, col_type, scale):
         if col_type == cx_Oracle.NUMBER:
-            return TYPE_FLOAT if scale > 0 else TYPE_INTEGER
+            if scale is None:
+                return TYPE_INTEGER
+            if scale > 0:
+                return TYPE_FLOAT
+            return TYPE_INTEGER
         else:
             return TYPES_MAP.get(col_type, None)
 


Test patch:
Repository: getredash/redash

Issue:
Clickhouse column name encoding problem
<!--

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

Alias column name contains non 'latin-1' characters return encoding  error.
For example : select count(*) as 'כמות'…

**Error message**: 'latin-1' codec can't encode characters in position 285-288: Body ('כמות') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

BTW, This works fine with other data source like mysql.


### Steps to Reproduce

1. This is the first step
Create a query with ClickHouse as the data source

2. This is the second step, etc.
Add a column alias in utf8 chars like : select colum1 as 'ש'

Any other info e.g. Why do you consider this to be a bug? What did you expect to happen instead?

I expected to see the column alias like I do with mysql data source .
No problem with the 'utf-8' data,  so probably column names should support 'utf-8' charset as well.
This used to work with the older version (v8)
### Technical details:

* Redash Version:
9.0.0-alpha
* Browser/OS:
 Chrome
* How did you install Redash:
Docker Based Developer Installation


Discussion:
I think that #4627 might fix this. What commit are you on?
Im on 9.0.0-alpha
#4627 looks very similar but its more about the email then the actual data.
Probably, the fix should be similar as well  (but  here encode the sql data that is going to Clickhouse).


Resolving patch:
diff --git a/redash/query_runner/clickhouse.py b/redash/query_runner/clickhouse.py
--- a/redash/query_runner/clickhouse.py
+++ b/redash/query_runner/clickhouse.py
@@ -68,7 +68,7 @@ def _send_query(self, data, stream=False):
             verify = self.configuration.get("verify", True)
             r = requests.post(
                 url,
-                data=data,
+                data=data.encode("utf-8","ignore"),
                 stream=stream,
                 timeout=self.configuration.get("timeout", 30),
                 params={


Test patch:
Repository: getredash/redash

Issue:
Bug: Redshift data source unable to refresh schema
Redash version: 8.0.0+b32245 (a16f551e)
Data Source: Redshift
Redshift cluster version: 1.0.14436 

Error:
![image](https://user-images.githubusercontent.com/14027470/78871303-6983e000-7a3f-11ea-86e0-43e91ed8334d.png)
API: `/api/data_sources/1/schema?refresh=true`
Response: `{"error": {"message": "Error retrieving schema.", "code": 2}}`

Diagnosis:
Manually running the following query: https://github.com/getredash/redash/blob/master/redash/query_runner/pg.py#L323 returns error 
`Error running query: schema "pg_temp_16" does not exist`

Solution:
Update https://github.com/getredash/redash/blob/master/redash/query_runner/pg.py#L330
to include `and table_schema NOT LIKE 'pg_%'`


Discussion:

Resolving patch:
diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py
--- a/redash/query_runner/pg.py
+++ b/redash/query_runner/pg.py
@@ -328,6 +328,7 @@ def _get_tables(self, schema):
                             ordinal_position AS pos
             FROM svv_columns
             WHERE table_schema NOT IN ('pg_internal','pg_catalog','information_schema')
+            AND table_schema NOT LIKE 'pg_temp_%'
         )
         SELECT table_name, table_schema, column_name
         FROM tables


Test patch:
Repository: getredash/redash

Issue:
Admin shows n/a for started value in manager column
<!--

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

When going to the admin section you can see in the manager column that the "Started" value is always "n/a".

<img width="630" alt="CleanShot 2020-05-15 at 01 58 09@2x" src="https://user-images.githubusercontent.com/1610/81997320-b31fa600-964f-11ea-90c0-e0c2988fdcce.png">

### Steps to Reproduce

I **think** this a regression from first https://github.com/getredash/redash/commit/26f0ce0749c1f683ba3b8f0b1d2a9e70cb6ea0e2#diff-5ca0dc869c9c3218d8a95ba7f99f5f4cL460 where it removed the QueryTaskTracker and by that the ability to store the `started_at` time and then later when Celery was removed, where this code path became no-op.

Originally this value was shown in v6.0.0 here: https://github.com/getredash/redash/blob/4780bd9c5ef212dd4c38bafb525abc991812d59b/client/app/pages/admin/status/status.html#L35

Now it's pulled in from the `/status.json` api in https://github.com/getredash/redash/blob/8010781f0d3d14260523e135a5abef0847714b9e/client/app/pages/admin/SystemStatus.jsx#L47 but the value behind is not set anymore in https://github.com/getredash/redash/blob/8010781f0d3d14260523e135a5abef0847714b9e/redash/tasks/queries/maintenance.py#L95-L101.

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: 8010781f0d3d14260523e135a5abef0847714b9e
* Browser/OS: Firefox macOS
* How did you install Redash: Docker



Discussion:

Resolving patch:
diff --git a/redash/tasks/queries/maintenance.py b/redash/tasks/queries/maintenance.py
--- a/redash/tasks/queries/maintenance.py
+++ b/redash/tasks/queries/maintenance.py
@@ -81,6 +81,7 @@ def _apply_auto_limit(query_text, query):
 
 
 def refresh_queries():
+    started_at = time.time()
     logger.info("Refreshing queries...")
     enqueued = []
     for query in models.Query.outdated_queries():
@@ -105,6 +106,7 @@ def refresh_queries():
             sentry.capture_exception(error)
 
     status = {
+        "started_at": started_at,
         "outdated_queries_count": len(enqueued),
         "last_refresh_at": time.time(),
         "query_ids": json_dumps([q.id for q in enqueued]),


Test patch:
Repository: getredash/redash

Issue:
Show a helpful error message when trying to execute queries detached from a data source
When a data source gets deleted, it's queries are still accessible (detailed explanation can be found in #4336) but they error out due to [`data_source` being None](https://github.com/getredash/redash/blob/master/redash/handlers/query_results.py#L65).

We should provide a helpful error message there (perhaps by raising `QueryDetachedFromDataSourceError`).


Discussion:

Resolving patch:
diff --git a/redash/handlers/query_results.py b/redash/handlers/query_results.py
--- a/redash/handlers/query_results.py
+++ b/redash/handlers/query_results.py
@@ -51,10 +51,14 @@ def error_response(message, http_status=400):
     ),
     "no_permission": error_response("You do not have permission to run queries with this data source.", 403),
     "select_data_source": error_response("Please select data source to run this query.", 401),
+    "no_data_source": error_response("Target data source not available.", 401),
 }
 
 
 def run_query(query, parameters, data_source, query_id, should_apply_auto_limit, max_age=0):
+    if not data_source:
+        return error_messages["no_data_source"]
+
     if data_source.paused:
         if data_source.pause_reason:
             message = "{} is paused ({}). Please try later.".format(data_source.name, data_source.pause_reason)


Test patch:
diff --git a/tests/handlers/test_query_results.py b/tests/handlers/test_query_results.py
--- a/tests/handlers/test_query_results.py
+++ b/tests/handlers/test_query_results.py
@@ -1,9 +1,16 @@
-from redash.handlers.query_results import error_messages
+from redash.handlers.query_results import error_messages, run_query
 from redash.models import db
 from redash.utils import json_dumps
 from tests import BaseTestCase
 
 
+class TestRunQuery(BaseTestCase):
+    def test_run_query_with_no_data_source(self):
+        response, status = run_query(None, None, None, None, None)
+        self.assertDictEqual(response, error_messages["no_data_source"][0])
+        self.assertEqual(status, error_messages["no_data_source"][1])
+
+
 class TestQueryResultsCacheHeaders(BaseTestCase):
     def test_uses_cache_headers_for_specific_result(self):
         query_result = self.factory.create_query_result()
Repository: getredash/redash

Issue:
Can't query private addresses again
Hello!
I use Redash docker image on my own server to getting query from my API.
Since version 9.0.0 alpha I don't have issues with this, because of  REDASH_ENFORCE_PRIVATE_IP_BLOCK flag set to false.
But after upgrading to version beta, this issues came back! What should I do?



Discussion:

Resolving patch:
diff --git a/redash/query_runner/__init__.py b/redash/query_runner/__init__.py
--- a/redash/query_runner/__init__.py
+++ b/redash/query_runner/__init__.py
@@ -258,7 +258,7 @@ def get_auth(self):
             return None
 
     def get_response(self, url, auth=None, http_method="get", **kwargs):
-        if is_private_address(url):
+        if is_private_address(url) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK:
             raise Exception("Can't query private addresses.")
 
         # Get authentication values if not given


Test patch:
Repository: getredash/redash

Issue:
Minor Salesforce runner fix
<!--

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

A Security Token isn't required in all SFDC environments - depending on configuration. See [here](https://help.salesforce.com/articleView?id=000331668&type=1&mode=1) for more information.

### Steps to Reproduce

1. Add Salesforce as a data source where a token isn't required (and cannot be generated)
2. Cannot proceed without required field

### Technical details:

https://github.com/getredash/redash/blob/be56035bd6d9856361edc6b23d30a38c8f2d2be2/redash/query_runner/salesforce.py#L81

Just remove `token` from the `required` list. Seemed like it'd be faster to create an issue than submit a PR for such a small change


Discussion:
Might be worth noting that I'm running a version with the change applied and it works 👌 

Resolving patch:
diff --git a/redash/query_runner/salesforce.py b/redash/query_runner/salesforce.py
--- a/redash/query_runner/salesforce.py
+++ b/redash/query_runner/salesforce.py
@@ -81,7 +81,7 @@ def configuration_schema(cls):
                     "default": DEFAULT_API_VERSION,
                 },
             },
-            "required": ["username", "password", "token"],
+            "required": ["username", "password"],
             "secret": ["password", "token"],
         }
 


Test patch:
Repository: getredash/redash

Issue:
Loading schema for Sqlite DB with "Order" column name fails
### Issue Summary

I added a Sqlite Database which has an column with the name `Order`.
When I try to create a query, the error `Schema refresh failed.` comes up.

### Steps to Reproduce

1. Add an Sqlite Database which has a column with the name `Order`
2. Try to create a query
3. Get the error `Schema refresh failed.`


### Technical details:

* Redash Version: cloned from master
* Browser/OS: Brave Browser & Ubuntu 18.1
* How did you install Redash: built from source



Discussion:

Resolving patch:
diff --git a/redash/query_runner/sqlite.py b/redash/query_runner/sqlite.py
--- a/redash/query_runner/sqlite.py
+++ b/redash/query_runner/sqlite.py
@@ -29,7 +29,7 @@ def __init__(self, configuration):
 
     def _get_tables(self, schema):
         query_table = "select tbl_name from sqlite_master where type='table'"
-        query_columns = "PRAGMA table_info(%s)"
+        query_columns = "PRAGMA table_info(\"%s\")"
 
         results, error = self.run_query(query_table, None)
 


Test patch:
Repository: getredash/redash

Issue:
Error to load MongoDB collections
### Issue Summary

When you create a Mongodb data source using a mongodb user which has access to a databse but doesn't have privileges to find records in a specific collection under the database, redash can't refresh the schema as it tried to get a data sample even though the user doesn't have access to the collection, that probably happens because the command list_collections returns a list of all collections regardless if the user has access to its data or not.

### Steps to Reproduce

1. Create a role in mongodb and give access only to certain collections.
2. Create a user in mongodb and assign the previous role to it.
3. Create a data source in redash.
4. Try selecting the newly created data source in the query page.

### Technical details:

* Redash Version: 10.1.0
* Browser/OS: Any
* How did you install Redash: Tried with Helm chart and aws market place



Discussion:

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
@@ -221,15 +221,21 @@ def _get_collection_fields(self, db, collection_name):
         # document written.
         collection_is_a_view = self._is_collection_a_view(db, collection_name)
         documents_sample = []
-        if collection_is_a_view:
-            for d in db[collection_name].find().limit(2):
-                documents_sample.append(d)
-        else:
-            for d in db[collection_name].find().sort([("$natural", 1)]).limit(1):
-                documents_sample.append(d)
-
-            for d in db[collection_name].find().sort([("$natural", -1)]).limit(1):
-                documents_sample.append(d)
+        try:
+            if collection_is_a_view:
+                for d in db[collection_name].find().limit(2):
+                    documents_sample.append(d)
+            else:
+                for d in db[collection_name].find().sort([("$natural", 1)]).limit(1):
+                    documents_sample.append(d)
+
+                for d in db[collection_name].find().sort([("$natural", -1)]).limit(1):
+                    documents_sample.append(d)
+        except Exception as ex:
+            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
+            message = template.format(type(ex).__name__, ex.args)
+            logger.error(message)
+            return []
         columns = []
         for d in documents_sample:
             self._merge_property_names(columns, d)
@@ -242,10 +248,11 @@ def get_schema(self, get_stats=False):
             if collection_name.startswith("system."):
                 continue
             columns = self._get_collection_fields(db, collection_name)
-            schema[collection_name] = {
-                "name": collection_name,
-                "columns": sorted(columns),
-            }
+            if columns:
+                schema[collection_name] = {
+                    "name": collection_name,
+                    "columns": sorted(columns),
+                }
 
         return list(schema.values())
 


Test patch:
Repository: getredash/redash

Issue:
Timing out when connecting to a MSSQL database on non-default port using ODBC driver
I had to use "Microsoft SQL Server (ODBC)" data source because the "Microsoft SQL Server" one does not currently support using SSL. However, when trying to connect to my server on a port different than 1433, connection timed out.

After a bit of digging, I found this:
> Microsoft's ODBC drivers for SQL Server do not use a PORT= parameter. The port number, if any, is appended to the server name/IP with a comma

source: https://stackoverflow.com/a/50051708/1277401


Discussion:

Resolving patch:
diff --git a/redash/query_runner/mssql_odbc.py b/redash/query_runner/mssql_odbc.py
--- a/redash/query_runner/mssql_odbc.py
+++ b/redash/query_runner/mssql_odbc.py
@@ -114,9 +114,9 @@ def run_query(self, query, user):
             port = self.configuration.get("port", 1433)
             charset = self.configuration.get("charset", "UTF-8")
 
-            connection_string_fmt = "DRIVER={{ODBC Driver 17 for SQL Server}};PORT={};SERVER={};DATABASE={};UID={};PWD={}"
+            connection_string_fmt = "DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={},{};DATABASE={};UID={};PWD={}"
             connection_string = connection_string_fmt.format(
-                port, server, db, user, password
+                server, port, db, user, password
             )
 
             if self.configuration.get("use_ssl", False):


Test patch:
Repository: google/flax

Issue:
VAE example uses deprecated `optimizer.optimize()`



Discussion:
@makora9143 if you look at the console output when you run your example you'll see a warning. Can you please replace with `jax.grad()` or `jax.value_and_grad()` then `optimizer.apply_gradient()`?
@avital Thank you for your comment.
Unfortunately, I didn't find the warning at my console when I execute my vae example:
```bash
03/23/20 22:01:54 $ python main.py
~/.pyenv/versions/miniconda3-latest/envs/jax/lib/python3.7/site-packages/jax/lib/xla_bridge.py:123: UserWarning: No GPU/TPU found, falling back to CPU.
  warnings.warn('No GPU/TPU found, falling back to CPU.')
I0323 22:01:59.797530 4402519488 dataset_builder.py:193] Overwrite dataset info from restored data version.
I0323 22:01:59.799996 4402519488 dataset_builder.py:273] Reusing dataset mnist (~/tensorflow_datasets/mnist/1.0.0)
I0323 22:01:59.800137 4402519488 dataset_builder.py:434] Constructing tf.data.Dataset for split train, from ~/tensorflow_datasets/mnist/1.0.0
I0323 22:01:59.974323 4402519488 dataset_builder.py:193] Overwrite dataset info from restored data version.
I0323 22:01:59.975799 4402519488 dataset_builder.py:273] Reusing dataset mnist (~/tensorflow_datasets/mnist/1.0.0)
I0323 22:01:59.975924 4402519488 dataset_builder.py:434] Constructing tf.data.Dataset for split test, from ~/tensorflow_datasets/mnist/1.0.0
eval epoch: 1, loss: 121.4550, BCE: 98.3277, KLD: 23.1273

```

I use :
- `jax=0.1.62`
- `flax (pip upgrade at a few minutes ago)`

on macOS.
Which version that outputs deprecated warning? 

By the way,  I have confirmed that using `jax.value_and_grad()` and `optimizer.apply_gradient()` is no problem.
Do I need to create a new PR?

Thank you for your support!
Hmm, does the latest push to pip not have this change? 

https://github.com/google/flax/blob/prerelease/flax/optim.py#L289
Yes, please file a new PR. Thanks for /your/ support!

Resolving patch:
diff --git a/examples/vae/main.py b/examples/vae/main.py
--- a/examples/vae/main.py
+++ b/examples/vae/main.py
@@ -120,7 +120,9 @@ def loss_fn(model):
     kld_loss = kl_divergence(mean, logvar)
     loss = jnp.mean(bce_loss + kld_loss)
     return loss, recon_x
-  optimizer, _, _ = optimizer.optimize(loss_fn)
+  grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
+  _, grad = grad_fn(optimizer.target)
+  optimizer = optimizer.apply_gradient(grad)
   return optimizer
 
 


Test patch:
Repository: google/flax

Issue:
`typing._ClassVar` cannot be accessed in the iPython shell – dataclasses package seems to mess up on Python 3.7
### Problem you have encountered:

I just installed flax and tried to import it from the iPython shell. But it raises an `AttributeError`.

```
In [1]: import flax

*snip*

~/.virtualenvs/flax2/lib/python3.7/site-packages/dataclasses.py in _is_classvar(a_type, typing)
    548     # This test uses a typing internal class, but it's the best way to
    549     # test if this is a ClassVar.
--> 550     return type(a_type) is typing._ClassVar
    551
    552

AttributeError: module 'typing' has no attribute '_ClassVar'
```
This does not happen in the normal interpreter, where everything goes fine. 

### What you expected to happen:

I expected the import to work the same in iPython and the normal python shell.

### Logs, error messages, etc:

Full traceback in this gist: https://gist.github.com/bayerj/96f096c7fb09a7c9b758dabdbca32671

### Steps to reproduce:

On Mac OS X with Python 3.7.6, not anaconda, virtuelenvwrapper installed.

```
❯❯❯ mkvirtualenv flax2
❯❯❯ pip install jaxlib
*snip*
❯❯❯ pip install flax
*snip*
❯❯❯ ipython

*snip*

In [1]: import flax
```

### Workaround

The problem seems to be in the `dataclasses` package–not python's own one–from PyPI. If I uninstall it...

```
❯❯❯ pip uninstall dataclasses
Found existing installation: dataclasses 0.6
Uninstalling dataclasses-0.6:
  Would remove:
    /Users/bayerj/.virtualenvs/debug2/lib/python3.7/site-packages/dataclasses-0.6.dist-info/*
    /Users/bayerj/.virtualenvs/debug2/lib/python3.7/site-packages/dataclasses.py
Proceed (y/n)? y
  Successfully uninstalled dataclasses-0.6
❯❯❯ ipython
/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py:931: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.7.6 (default, Dec 30 2019, 19:38:28)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import flax
```
... this goes fine.






Discussion:
This is my fault, I thought that the `requires_python` directive in the backported dataclasses pypi package would prevent installation on >=3.7, but this is clearly not the case.  I believe the correct approach is using the pep508 `python_version` environment marker in our setup.py file.

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -30,7 +30,7 @@
     "numpy>=1.12",
     "jax>=0.1.59",
     "matplotlib",  # only needed for tensorboard export
-    "dataclasses",  # will only install on py3.6
+    "dataclasses;python_version<'3.7'", # will only install on py3.6
     "msgpack",
 ]
 


Test patch: