Skip to content

Commit

Permalink
added option to disable flatten nested (#63)
Browse files Browse the repository at this point in the history
* added support for flatten nested

* Update plugins/doc_fragments/client_inst_opts.py

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* Update plugins/modules/clickhouse_client.py

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* Update plugins/module_utils/clickhouse.py

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* Update plugins/module_utils/clickhouse.py

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

---------

Co-authored-by: tmahany <tmahany@tmahanyMBP23.local>
Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent 83c2766 commit d84a69d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plugins/doc_fragments/client_inst_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class ModuleDocFragment(object):
type: dict
default: {}
flatten_nested:
description:
- Sets the C(flatten_nested) setting on session before running the query.
type: int
choices: [0, 1]
version_added: '0.5.0'
requirements: [ 'clickhouse-driver' ]
notes:
Expand Down
1 change: 1 addition & 0 deletions plugins/module_utils/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def client_common_argument_spec():
login_user=dict(type='str', default=None),
login_password=dict(type='str', default=None, no_log=True),
client_kwargs=dict(type='dict', default={}),
flatten_nested=dict(type='int', choices=[0, 1]),
)


Expand Down
3 changes: 3 additions & 0 deletions plugins/modules/clickhouse_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def main():
client_kwargs = module.params['client_kwargs']
query = module.params['execute']
execute_kwargs = module.params['execute_kwargs']
flatten_nested = module.params['flatten_nested']
# The reason why these arguments are separate from client_kwargs
# is that we need to protect some sensitive data like passwords passed
# to the module from logging (see the arguments above with no_log=True);
Expand All @@ -283,6 +284,8 @@ def main():
substituted_query = get_substituted_query(module, client, query, execute_kwargs)

# Execute query
if flatten_nested == 0:
execute_query(module, client, "SET flatten_nested = 0", execute_kwargs)
result = execute_query(module, client, query, execute_kwargs)

# Convert values not supported by ansible-core
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/targets/clickhouse_client/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,25 @@
ansible.builtin.assert:
that:
- result.result == [["one"], ["two"], ["three"]]

- name: Load a Schema with Nested Fields
community.clickhouse.clickhouse_client:
flatten_nested: 0
execute: "CREATE TABLE foo.nested ( `coordinates` Nested(x int, y int )) ENGINE = Memory;"

- name: Load Nested Data
community.clickhouse.clickhouse_client:
execute: 'insert into foo.nested (coordinates) VALUES (([(10,20), (11,21)]));'

- name: Read Nested data
register: nested_result
community.clickhouse.clickhouse_client:
execute: 'select * from foo.nested;'

- name: Check Nested Data exists
ansible.builtin.assert:
that:
- nested_result.result[0][0][0][0] == 10
- nested_result.result[0][0][0][1] == 20
- nested_result.result[0][0][1][0] == 11
- nested_result.result[0][0][1][1] == 21

0 comments on commit d84a69d

Please sign in to comment.