Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue inserting and fetching tuple array with certain string format #123

Open
akira opened this issue Dec 19, 2024 · 0 comments
Open

Issue inserting and fetching tuple array with certain string format #123

akira opened this issue Dec 19, 2024 · 0 comments

Comments

@akira
Copy link

akira commented Dec 19, 2024

There's an issue when inserting and trying to fetch a tuple array with certain string formatting - in this case parens:

[("key1", "value1"), ("key2", "this is an invalid pair(with some parens) and this, let's see if it works")]

When it is fetched, it comes back as:

[('key1', 'value1'), ('key2', "'this is an invalid pair(with some parens) and thi"), ("let's see if it works'",)]

Which is not a valid tuple - this cannot be reinserted to Clickhouse.

See code below, this is the entire result fetched:

[{'a': [('key1', 'value1'), ('key2', "'this is an invalid pair(with some parens) and thi"), ("let's see if it works'",)]}, {'a': [('key3', 'value3'), ('key4', 'value4')]}, {'a': [('key5', 'value5'), ('key6', 'value6')]}, {'a': [('key7', 'value7'), ('key8', 'value8')]}, {'a': [('key11', ''), ('key12', "''")]}, {'a': [('key9', 'value9'), ('key10', 'value10')]}]

If trying to reinsert this record as is, we get the error:

aiochclient.exceptions.ChClientError: Code: 386. DB::Exception: There is no supertype for types Tuple(String, String), Tuple(String, String), String because some of them are Tuple and some of them are not: While processing [('key1', 'value1'), ('key2', '\'this is an invalid pair(with some parens) and thi'), 'let\'s see if it works\'']: While executing ValuesBlockInputFormat. (NO_COMMON_TYPE) (version 24.2.3.70 (official build))

But it works without the line including the parens.

import asyncio

from aiohttp import ClientSession

from aiochclient import ChClient


async def some_query(client: ChClient, a: list[tuple[str,str]]):
    result = await client.execute(
        "INSERT INTO t VALUES", [a]
    )


async def main():
    async with ClientSession() as s:
        client = ChClient(s, url="http://localhost:8123")
        # preparing database        
        await client.execute("DROP TABLE IF EXISTS t")
        await client.execute("CREATE TABLE IF NOT EXISTS t (a  Array(Tuple(String, String))) ENGINE = Memory")        
        # making queries in parallel
        await asyncio.gather(
            some_query(client, [("key1", "value1"), ("key2", "this is an invalid pair(with some parens) and this, let's see if it works")]),
            some_query(client, [("key3", "value3"), ("key4", "value4")]),
            some_query(client, [("key11", ""), ("key12", "''")]),
        )
        print("Fetch results:")
        results = await client.fetch("SELECT * FROM t")
        print([{**r} for r in results])
        for result in results:
            await client.execute("INSERT INTO t VALUES", [result['a']])


if __name__ == "__main__":
    # if >=3.7:
    asyncio.run(main())
@akira akira changed the title Issue inserting and fetching tuple with certain string format Issue inserting and fetching tuple array with certain string format Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant