From 6c03d38d8e631f23eb4a4d1cf6a3e299e379f915 Mon Sep 17 00:00:00 2001 From: VVM Date: Thu, 1 Jun 2023 21:28:17 +0100 Subject: [PATCH] 1. Fixed wrong position of brackets in extract_cellset_raw extract_cellset_metadata_raw 2. The name of the additional "tricky" parameter "top_rows" has been changed to "top_tuples" to avoid misunderstandings 3. A new test has been added to check execution of execute_mdx_raw with "top" parameter --- TM1py/Services/CellService.py | 16 ++++++++++++---- Tests/CellService_test.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/TM1py/Services/CellService.py b/TM1py/Services/CellService.py index f4b54301..e35c49d4 100644 --- a/TM1py/Services/CellService.py +++ b/TM1py/Services/CellService.py @@ -2689,13 +2689,17 @@ def extract_cellset_raw_response( else: expand_hierarchies = "" + # top_tuples parameter is used as an optimization trick: + # if top_cells is set to N => it will be sufficient to get only the first N tuples in Axes, top_tuples does this + # if skip_cells is used => trick not applicable, all tuples must be extracted + url = "/api/v1/Cellsets('{cellset_id}')?$expand=" \ "Cube($select=Name;$expand=Dimensions($select=Name))," \ "Axes({filter_axis}$expand={hierarchies}Tuples($expand=Members({select_member_properties}" \ - "{expand_elem_properties}{top_rows})))," \ + "{expand_elem_properties}){top_tuples}))," \ "Cells($select={cell_properties}{top_cells}{skip_cells}{filter_cells})" \ .format(cellset_id=cellset_id, - top_rows=f";$top={top}" if top and not skip else "", + top_tuples=f";$top={top}" if top and not skip else "", cell_properties=",".join(cell_properties), filter_axis=filter_axis, hierarchies=expand_hierarchies, @@ -2813,12 +2817,16 @@ def extract_cellset_metadata_raw( filter_axis = "$filter=Ordinal ne 2;" if skip_contexts else "" + # top_tuples parameter is used as an optimization trick: + # if top_cells is set to N => it will be sufficient to get only the first N tuples in Axes, top_tuples does this + # if skip_cells is used => trick not applicable, all tuples must be extracted + url = "/api/v1/Cellsets('{cellset_id}')?$expand=" \ "Cube($select=Name;$expand=Dimensions($select=Name))," \ "Axes({filter_axis}$expand={hierarchies}Tuples($expand=Members({select_member_properties}" \ - "{expand_elem_properties}{top_rows})))" \ + "{expand_elem_properties}){top_tuples}))" \ .format(cellset_id=cellset_id, - top_rows=f";$top={top}" if top and not skip else "", + top_tuples=f";$top={top}" if top and not skip else "", filter_axis=filter_axis, hierarchies=expand_hierarchies, select_member_properties=select_member_properties, diff --git a/Tests/CellService_test.py b/Tests/CellService_test.py index 450e0f7d..8b3296ff 100644 --- a/Tests/CellService_test.py +++ b/Tests/CellService_test.py @@ -1716,6 +1716,31 @@ def test_execute_mdx_raw_include_hierarchies(self): self.assertEqual(self.dimension_names[axis_counter], hierarchies[0]['Name']) self.assertEqual(self.dimension_names[axis_counter], hierarchies[0]['Dimension']['Name']) + def test_execute_mdx_raw_top(self): + # write cube content + self.tm1.cubes.cells.write_values(self.cube_name, self.cellset) + + # MDX Query that gets full cube content with zero suppression + mdx = MdxBuilder.from_cube(self.cube_name) \ + .rows_non_empty() \ + .add_hierarchy_set_to_row_axis( + MdxHierarchySet.all_members(self.dimension_names[0], self.dimension_names[0])) \ + .add_hierarchy_set_to_row_axis( + MdxHierarchySet.all_members(self.dimension_names[1], self.dimension_names[1])) \ + .add_hierarchy_set_to_column_axis( + MdxHierarchySet.all_members(self.dimension_names[2], self.dimension_names[2])) \ + .to_mdx() + + # MDX with top + raw_response = self.tm1.cubes.cells.execute_mdx_raw(mdx, top=5) + + # Check if the Axes length is equal to the "top" value + for axis in raw_response["Axes"]: + self.assertEqual(len(axis['Tuples']), 5) + + # Check if the Cells length is equal to the "top" value + self.assertEqual(len(raw_response["Cells"]), 5) + def test_execute_mdx_rows_and_values_one_cell(self): mdx = MdxBuilder.from_cube(self.cube_name) \ .add_hierarchy_set_to_axis(1, MdxHierarchySet.member(Member.of(self.dimension_names[0], "Element1"))) \