ClickHouse / clickhouse-connect

Python driver/sqlalchemy/superset connectors
Apache License 2.0
330 stars 64 forks source link

Httpclient error when executing INSERT INTO with LIMIT 0 #389

Closed treysp closed 1 month ago

treysp commented 3 months ago

Describe the bug

When I execute an INSERT INTO command using the httpclient, it returns an error if the insert query contains LIMIT 0.

Steps to reproduce

  1. Create a table in Clickhouse or Clickhouse cloud
  2. Use the httpclient to execute an INSERT INTO command whose query contains LIMIT 0

Expected behaviour

No error. (I realize that inserting the results of a query that returns no rows is pointless, but here we are.)

Code example

> self.execute("CREATE OR REPLACE TABLE test_insert (col1 Int8) order by ();")
> self.execute("INSERT INTO test_insert SELECT 1 LIMIT 0;")

Traceback (most recent call last):
  File ".venv/lib/python3.11/site-packages/clickhouse_connect/dbapi/cursor.py", line 56, in execute
    query_result = self.client.query(operation, parameters)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/clickhouse_connect/driver/client.py", line 209, in query
    return self._query_with_context(query_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/clickhouse_connect/driver/httpclient.py", line 210, in _query_with_context
    for col in json_result['meta']:
               ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'json_result' where it is not associated with a value

Executing the same INSERT INTO query without LIMIT 0 works as expected and nothing is printed to terminal:

> self.execute("INSERT INTO test_insert SELECT 1;")

Configuration

Environment

ClickHouse server