ipython-sql icon indicating copy to clipboard operation
ipython-sql copied to clipboard

ClickHouse connection fails

Open feriat opened this issue 8 years ago • 0 comments

I have a problem using ipython-sql with ClickHouse. I use https://github.com/cloudflare/sqlalchemy-clickhouse connector, with the following code:

%load_ext sql

%%sql clickhouse://username:password@host:8123/db
SELECT count() FROM system.numbers LIMIT 5

and get the following traceback

ExceptionTraceback (most recent call last)
<ipython-input-3-eceaed765029> in <module>()
----> 1 get_ipython().run_cell_magic(u'sql', u'clickhouse://*****:8123/p66', u'SELECT * FROM system.numbers LIMIT 5')

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2113             magic_arg_s = self.var_expand(line, stack_depth)
   2114             with self.builtin_trap:
-> 2115                 result = fn(magic_arg_s, cell)
   2116             return result
   2117 

<decorator-gen-124> in execute(self, line, cell, local_ns)

/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

<decorator-gen-123> in execute(self, line, cell, local_ns)

/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/usr/local/lib/python2.7/dist-packages/sql/magic.pyc in execute(self, line, cell, local_ns)
     85 
     86         try:
---> 87             result = sql.run.run(conn, parsed['sql'], self, user_ns)
     88 
     89             if result and ~isinstance(result, str) and self.column_local_vars:

/usr/local/lib/python2.7/dist-packages/sql/run.pyc in run(conn, sql, config, user_namespace)
    278                 # mssql has autocommit
    279                 if 'mssql' not in str(conn.dialect):
--> 280                     conn.session.execute('commit')
    281             except sqlalchemy.exc.OperationalError:
    282                 pass # not all engines can commit

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/base.pyc in execute(self, object, *multiparams, **params)
    937         """
    938         if isinstance(object, util.string_types[0]):
--> 939             return self._execute_text(object, multiparams, params)
    940         try:
    941             meth = object._execute_on_connection

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_text(self, statement, multiparams, params)
   1095             statement,
   1096             parameters,
-> 1097             statement, parameters
   1098         )
   1099         if self._has_events or self.engine._has_events:

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_context(self, dialect, constructor, statement, parameters, *args)
   1187                 parameters,
   1188                 cursor,
-> 1189                 context)
   1190 
   1191         if self._has_events or self.engine._has_events:

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/base.pyc in _handle_dbapi_exception(self, e, statement, parameters, cursor, context)
   1403                 )
   1404             else:
-> 1405                 util.reraise(*exc_info)
   1406 
   1407         finally:

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/base.pyc in _execute_context(self, dialect, constructor, statement, parameters, *args)
   1180                         statement,
   1181                         parameters,
-> 1182                         context)
   1183         except BaseException as e:
   1184             self._handle_dbapi_exception(

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy/engine/default.pyc in do_execute(self, cursor, statement, parameters, context)
    468 
    469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
    471 
    472     def do_execute_no_params(self, cursor, statement, context=None):

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy_clickhouse/connector.pyc in execute(self, operation, parameters, is_response)
    198         if is_response:
    199             response = self._db.select(sql, settings={'query_id': self._uuid})
--> 200             self._process_response(response)
    201         else:
    202             self._db.raw(sql)

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy_clickhouse/connector.pyc in _process_response(self, response)
    329         cols = None
    330         data = []
--> 331         for r in response:
    332             if not cols:
    333                 cols = [(f[0], f[1].db_type) for f in r._fields]

/home/ipython/mypy/lib/python2.7/site-packages/infi/clickhouse_orm/database.pyc in select(self, query, model_class, settings)
    156         query += ' FORMAT TabSeparatedWithNamesAndTypes'
    157         query = self._substitute(query, model_class)
--> 158         r = self._send(query, settings, True)
    159         lines = r.iter_lines()
    160         field_names = parse_tsv(next(lines))

/home/ipython/mypy/local/lib/python2.7/site-packages/sqlalchemy_clickhouse/connector.pyc in _send(self, data, settings, stream)
     91     r = requests.post(self.db_url, params=params, data=data, stream=stream)
     92     if r.status_code != 200:
---> 93         raise Exception(r.text)
     94     return r
     95 Database._send = _send

Exception: Code: 62, e.displayText() = DB::Exception: Syntax error: failed at position 1: commit FORMAT TabSeparatedWithNamesAndTypes. Expected one of: OPTIMIZE query, OPTIMIZE TABLE, INSERT query, INSERT INTO, SELECT, WITH, USE query, USE, Query with output, SHOW PROCESSLIST query, SHOW PROCESSLIST, DESC, DESCRIBE, SET query, KILL QUERY query, KILL QUERY, SHOW TABLES|DATABASES query, SHOW, ALTER query, ALTER TABLE, RENAME query, RENAME TABLE, CHECK TABLE, CREATE TABLE or ATTACH TABLE query, CREATE, ATTACH, DROP query, DROP, DETACH, EXISTS, SELECT query, EXISTS, SHOW CREATE or DESCRIBE query, Query, SET, e.what() = DB::Exception

My guess is that it adds commit after the end of the query, thus violating clickhouse API. %config SqlMagic.autocommit=False is not recognized by the API?

Any thoughts how can I use ClickHouse with Jupyter?

feriat avatar Aug 29 '17 18:08 feriat