Files
connect/examples/insert_examples.py
zeekay 999d38466d rename: this fork is ours — datastore_connect
Imports read `datastore_connect`, distribution `datastore-connect`, so nothing has to
import another product's name to talk to Hanzo Datastore.

Cut from upstream 0.8.17 — the version this stack already ran — rather than from
upstream HEAD, so it is a PURE RENAME with no behaviour change. Forking from
HEAD would have dragged versions along with the name (connect alone jumped
0.8.17 -> 1.6.0, a major), which is a different and much riskier change wearing
a rename's clothes.

X-ClickHouse-* headers are untouched: they are wire protocol the server sends
and expects, and a different token shape from the module name.
2026-07-26 11:01:30 -07:00

82 lines
2.5 KiB
Python

import datastore_connect
client: datastore_connect.driver.Client
def inserted_nested_flat():
client.command('DROP TABLE IF EXISTS test_nested_flat')
client.command('SET flatten_nested = 1')
client.command(
"""
CREATE TABLE test_nested_flat
(
`key` UInt32,
`value` Nested(str String, int32 Int32)
)
ENGINE = MergeTree
ORDER BY key
""")
result = client.query('DESCRIBE TABLE test_nested_flat')
print(result.column_names[0:2])
print(result.result_columns[0:2])
# Note the Nested 'value' column is inserted as two parallel arrays of values
# into their own columns of the form `col_name.key_name` with Array data types
data = [[1, ['string_1', 'string_2'], [20, 30]],
[2, ['string_3', 'string_4'], [40, 50]]
]
client.insert('test_nested_flat', data,
column_names=['key', 'value.str', 'value.int32'],
column_type_names=['UInt32', 'Array(String)', 'Array(Int32)'])
result = client.query('SELECT * FROM test_nested_flat')
print(result.column_names)
print(result.result_columns)
client.command('DROP TABLE test_nested_flat')
def insert_nested_not_flat():
client.command('DROP TABLE IF EXISTS test_nested_not_flat')
client.command('SET flatten_nested = 0')
client.command(
"""
CREATE TABLE test_nested_not_flat
(
`key` UInt32,
`value` Nested(str String, int32 Int32)
)
ENGINE = MergeTree
ORDER BY key
""")
result = client.query('DESCRIBE TABLE test_nested_not_flat')
print (result.column_names[0:2])
print (result.result_columns[0:2])
# Note the Nested 'value' column is inserted as a list of dictionaries for each row
data = [[1, [{'str': 'nested_string_1', 'int32': 20},
{'str': 'nested_string_2', 'int32': 30}]],
[2, [{'str': 'nested_string_3', 'int32': 40},
{'str': 'nested_string_4', 'int32': 50}]]
]
client.insert('test_nested_not_flat', data,
column_names=['key', 'value'],
column_type_names=['UInt32', 'Nested(str String, int32 Int32)'])
result = client.query('SELECT * FROM test_nested_not_flat')
print(result.column_names)
print(result.result_columns)
client.command('DROP TABLE test_nested_not_flat')
def main():
global client # pylint: disable=global-statement
client = datastore_connect.get_client()
print ('Nested example flatten_nested = 1 (Default)')
inserted_nested_flat()
print('\n\nNested example flatten_nested = 0')
insert_nested_not_flat()
if __name__ == '__main__':
main()