Skip to content

Failure passing string filters in additional_table_filters settings param when calling .query #501

@ryftrus

Description

@ryftrus

Describe the bug

Passing the additional_table_filters with a string filter, fails to handle the escaping of single quotes correctly, and crashes instead, making it impossible to pass query filters that contain strings.

Steps to reproduce

  1. Setup a Clickhouse machine
  2. Add a table with a string field
  3. Perform a query passing additional_table_filters for the string with a filter

Expected behaviour

The query will work with the filter applied

Code example

from clickhouse_connect import get_client
ch = get_client(...)
ch.query("CREATE OR REPLACE TABLE my_table (
      a String
    )")
ch.query("INSERT INTO my_table (a) VALUES ('some_value');")

ch.query(
    "SELECT a from my_table", settings={"additional_table_filters": {"a":"'some_value'"} 
)

clickhouse-connect and/or ClickHouse server logs

self = <clickhouse_connect.driver.httpclient.HttpClient object at 0x1220c6180>, response = <urllib3.response.HTTPResponse object at 0x12395f970>, retried = False

def _error_handler(self, response: HTTPResponse, retried: bool = False) -> None:
    if self.show_clickhouse_errors:
        try:
            err_content = get_response_data(response)
        except Exception:  # pylint: disable=broad-except
            err_content = None
        finally:
            response.close()

        err_str = f'HTTPDriver for {self.url} returned response code {response.status}'
        err_code = response.headers.get(ex_header)
        if err_code:
            err_str = f'HTTPDriver for {self.url} received ClickHouse error code {err_code}'
        if err_content:
            err_msg = common.format_error(err_content.decode(errors='backslashreplace'))
            if err_msg.startswith('Code'):
                err_str = f'{err_str}\n {err_msg}'
    else:
        err_str = 'The ClickHouse server returned an error.'
  raise OperationalError(err_str) if retried else DatabaseError(err_str) from None

E clickhouse_connect.driver.exceptions.DatabaseError: HTTPDriver for http://localhost:8123 received ClickHouse error code 26
E Code: 26. DB::Exception: Cannot parse quoted string: expected opening quote ''', got '"'. (CANNOT_PARSE_QUOTED_STRING) (version 25.4.1.2934 (official build))

../.venv/lib/python3.12/site-packages/clickhouse_connect/driver/httpclient.py:395: DatabaseError

Configuration

Environment

  • clickhouse-connect version: 0.8.15
  • Python version: 3.12.11
  • Operating system: MacOS Sonoma 14.5

ClickHouse server

  • ClickHouse Server version: 25.4.1
  • ClickHouse Server non-default settings, if any:
  • CREATE TABLE statements for tables involved:
  • Sample data for these tables, use clickhouse-obfuscator if necessary
    Any table with string field.
    CREATE OR REPLACE TABLE my_table (
    a String
    )
    INSERT INTO my_table (a) VALUES ('some_value');

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions