PNixx / clickhouse-activerecord

A Ruby database ActiveRecord driver for ClickHouse
MIT License
195 stars 97 forks source link

An ActiveRecord::ActiveRecordError is raised if a selected row contains the string 'DB::Exception' #164

Open kyrylo opened 1 week ago

kyrylo commented 1 week ago

My issue is with a table that has a name column. When I insert a string containing DB::Exception into the name column and select the row, I get an ActiveRecord::ActiveRecordError.

The raw ClickHouse query works fine, but selecting via the adapter triggers the error. The buggy code is here: https://github.com/PNixx/clickhouse-activerecord/blob/ddfa259bbec88aefa3eaf2942392b8473a477601/lib/active_record/connection_adapters/clickhouse/schema_statements.rb#L185-L187

It assumes that DB::Exception only appears during a real ClickHouse exception.

Thank you for the gem! It makes the impossible possible :)

PNixx commented 1 week ago

Please, write example request.

kyrylo commented 1 week ago

How to reproduce:

  1. Create a table:

    CREATE TABLE things
    (
        name String,
    )
    ENGINE = MergeTree
    ORDER BY name;
  2. Insert a record with the string DB::Exception:

    INSERT INTO things (name) VALUES ('DB::Exception');
  3. Create a model for this in the Rails app

    class Thing < ApplicationRecord
      establish_connection :clickhouse
    end
  4. Run any query that selects the inserted row:


irb(main):002> Thing.first
  Clickhouse Thing Load (4.1ms)  SELECT things.* FROM things ORDER BY things.name ASC LIMIT 1
(irb):2:in `<main>': Response code: 200: (ActiveRecord::ActiveRecordError)
["name"]
["String"]
["DB::Exception"]

Query: SELECT things.* FROM things ORDER BY things.name ASC LIMIT 1
kyrylo commented 1 week ago

This is the commit that introduced the bug: https://github.com/PNixx/clickhouse-activerecord/commit/df61a892da5fe13b685ae38cdcda91ecc4efd27b

Not sure why a DB::Exception happens when the response is 200. I couldn't find any relevant info online.

kyrylo commented 1 week ago

I created a PR with the possible fix. https://github.com/PNixx/clickhouse-activerecord/pull/165