chdb-io / chdb

chDB is an in-process OLAP SQL Engine 🚀 powered by ClickHouse
https://clickhouse.com/chdb
Apache License 2.0
2.13k stars 75 forks source link

dbapi return int as string #117

Closed mofei920 closed 10 months ago

mofei920 commented 1 year ago

chdb driver version: 0.14.2 chdb version: 23.6.1.1 Python version: 3.9.6 macos catalina: 10.15.7

How to reproduce: create test table.

CREATE TABLE t1
(
    `id` Int64,
    c1 String 
)
ENGINE = ReplacingMergeTree
ORDER BY id;

insert into t1 values
(1,'a'),
(2,'b'),
(3,'c');

python code, just read data from table t1.

from chdb import dbapi

conn1 = dbapi.connect()
cur1 = conn1.cursor()

cur1.execute("select id, c1 from remote('host:9000','db','table','user','password') final")
result = cur1.fetchall()
print(result)

cur1.close()
conn1.close()

when i run the python code as above, i got the below result, the int id return as string, and the result returned as tuple, is it possible dbapi return result same as clickhouse_driver in python?

(('1', 'a'), ('2', 'b'), ('3', 'c'))
yunyu commented 10 months ago

I was able to reproduce this with Int64 but not Int32:

from chdb import dbapi

conn = dbapi.connect()
cur = conn.cursor()
cur.execute("CREATE DATABASE IF NOT EXISTS test_db ENGINE = Atomic")
cur.execute("USE test_db")
cur.execute("""
CREATE TABLE t1
(
    `id` Int32,
    c1 String 
)
ENGINE = ReplacingMergeTree
ORDER BY id;
""")

cur.executemany("insert into t1 values (%s, %s)", [(1, "a"), (2, "b"), (3, "c")])

# Test fetchall
cur.execute("select id, c1 from t1")
rows = cur.fetchall()
print(rows)

# Clean up
cur.close()
conn.close()

According to clickhouse docs (https://clickhouse.com/docs/en/interfaces/formats#json):

For compatibility with JavaScript, Int64 and UInt64 integers are enclosed in double quotes by default. To remove the quotes, you can set the configuration parameter output_format_json_quote_64bit_integers to 0.

Seems like all we have to do is set this configuration parameter.