diff --git a/changelogs/fragments/1-clickhouse_client.yml b/changelogs/fragments/1-clickhouse_client.yml new file mode 100644 index 0000000..aaf36bc --- /dev/null +++ b/changelogs/fragments/1-clickhouse_client.yml @@ -0,0 +1,2 @@ +minor_changes: +- clickhouse_client - added the ``set_settings`` argument (https://github.com/ansible-collections/community.clickhouse/pull/63). \ No newline at end of file diff --git a/changelogs/fragments/1-client.yml b/changelogs/fragments/1-client.yml deleted file mode 100644 index a1c9ef1..0000000 --- a/changelogs/fragments/1-client.yml +++ /dev/null @@ -1,2 +0,0 @@ -minor_changes: -- clickhouse_client - added the ``flatten_tested`` argument (https://github.com/ansible-collections/community.clickhouse/pull/63). diff --git a/plugins/doc_fragments/client_inst_opts.py b/plugins/doc_fragments/client_inst_opts.py index 34b0cf9..40a70b3 100644 --- a/plugins/doc_fragments/client_inst_opts.py +++ b/plugins/doc_fragments/client_inst_opts.py @@ -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: diff --git a/plugins/module_utils/clickhouse.py b/plugins/module_utils/clickhouse.py index 4d8f4f4..d058008 100644 --- a/plugins/module_utils/clickhouse.py +++ b/plugins/module_utils/clickhouse.py @@ -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]), ) @@ -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): diff --git a/plugins/modules/clickhouse_client.py b/plugins/modules/clickhouse_client.py index 6642e5d..8e05cc2 100644 --- a/plugins/modules/clickhouse_client.py +++ b/plugins/modules/clickhouse_client.py @@ -22,6 +22,7 @@ author: - Andrew Klychkov (@Andersson007) + - Aleks Vagachev (@aleksvagachev) extends_documentation_fragment: - community.clickhouse.client_inst_opts @@ -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''' @@ -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 @@ -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 @@ -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); @@ -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: diff --git a/tests/integration/targets/clickhouse_client/tasks/initial.yml b/tests/integration/targets/clickhouse_client/tasks/initial.yml index 754c3e1..075c4f1 100644 --- a/tests/integration/targets/clickhouse_client/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_client/tasks/initial.yml @@ -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 @@ -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 diff --git a/tests/unit/plugins/module_utils/test_clickhouse.py b/tests/unit/plugins/module_utils/test_clickhouse.py index 11a8b3d..8a580bf 100644 --- a/tests/unit/plugins/module_utils/test_clickhouse.py +++ b/tests/unit/plugins/module_utils/test_clickhouse.py @@ -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