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

Added a parameter to set query execution settings #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1-clickhouse_client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- clickhouse_client - added the ``set_settings`` argument (https://github.com/ansible-collections/community.clickhouse/pull/63).
2 changes: 0 additions & 2 deletions changelogs/fragments/1-client.yml

This file was deleted.

8 changes: 0 additions & 8 deletions plugins/doc_fragments/client_inst_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ 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
11 changes: 9 additions & 2 deletions plugins/module_utils/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ 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 Expand Up @@ -92,16 +91,24 @@ def connect_to_db_via_client(module, main_conn_kwargs, client_kwargs):
return client


def execute_query(module, client, query, execute_kwargs=None):
def execute_query(module, client, query, execute_kwargs=None, set_settings=None):
"""Execute query.

Returns rows returned in response.

set_settings - The list of settings that need to be set before executing the request.
"""
# Some modules do not pass this argument
if execute_kwargs is None:
execute_kwargs = {}

if set_settings is None:
set_settings = {}

try:
if len(set_settings) != 0:
for setting in set_settings:
client.execute("SET %s = '%s'" % (setting, set_settings[setting]))
result = client.execute(query, **execute_kwargs)
except Exception as e:
if "Not enough privileges" in to_native(e):
Expand Down
21 changes: 15 additions & 6 deletions plugins/modules/clickhouse_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

author:
- Andrew Klychkov (@Andersson007)
- Aleks Vagachev (@aleksvagachev)

extends_documentation_fragment:
- community.clickhouse.client_inst_opts
Expand All @@ -44,6 +45,13 @@
through the I(execute) argument.
type: dict
default: {}

set_settings:
description:
- The dict of settings that need to be set in the session before executing the request.
type: dict
default: {}
version_added: '0.5.0'
'''

EXAMPLES = r'''
Expand All @@ -64,6 +72,10 @@
register: result
community.clickhouse.clickhouse_client:
execute: CREATE TABLE test_table_1 (x String) ENGINE = Memory
set_settings:
flatten_nested: 0
short_circuit_function_evaluation: 'disable'


- name: Insert into test table using named parameters
register: result
Expand Down Expand Up @@ -248,6 +260,7 @@ def main():
argument_spec.update(
execute=dict(type='str', required=True),
execute_kwargs=dict(type='dict', default={}),
set_settings=dict(type='dict', default={})
)

# Instantiate an object of module class
Expand All @@ -260,7 +273,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']
set_settings = module.params['set_settings']
# 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,12 +296,8 @@ def main():
# Substitute query params if needed for future return
substituted_query = get_substituted_query(module, client, query, execute_kwargs)

# If support of arbitrary levels of nesting is needed when executing the main query
if flatten_nested == 0:
execute_query(module, client, "SET flatten_nested = 0", execute_kwargs)

# Execute query
result = execute_query(module, client, query, execute_kwargs)
result = execute_query(module, client, query, execute_kwargs, set_settings)

# Convert values not supported by ansible-core
if result:
Expand Down
17 changes: 16 additions & 1 deletion tests/integration/targets/clickhouse_client/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
# https://github.com/ansible-collections/community.clickhouse/pull/63/
- name: Load a schema with nested fields
community.clickhouse.clickhouse_client:
flatten_nested: 0
set_settings:
flatten_nested: 0
execute: "CREATE TABLE foo.nested (`coordinates` Nested(x int, y int )) ENGINE = Memory"

- name: Load nested data
Expand All @@ -94,3 +95,17 @@
- nested_result.result[0][0][0][1] == 20
- nested_result.result[0][0][1][0] == 11
- nested_result.result[0][0][1][1] == 21


- name: Check that the settings are applied
register: result
community.clickhouse.clickhouse_client:
set_settings:
flatten_nested: 0
execute: "SELECT value, changed FROM system.settings WHERE name='flatten_nested'"

- name: Check result
ansible.builtin.assert:
that:
- result.result[0][0] == "0"
- result.result[0][1] == 1
3 changes: 1 addition & 2 deletions tests/unit/plugins/module_utils/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def test_client_common_argument_spec():
'login_user': {'type': 'str', 'default': None},
'login_host': {'type': 'str', 'default': 'localhost'},
'login_password': {'type': 'str', 'default': None, 'no_log': True},
'client_kwargs': {'type': 'dict', 'default': {}},
'flatten_nested': {'type': 'int', 'choices': [0, 1]},
'client_kwargs': {'type': 'dict', 'default': {}}
}

assert client_common_argument_spec() == EXPECTED
Expand Down
Loading