def find_required_tables_to_join(elements, base_table): """ Collect all the tables required for a given list of dataset elements. This looks through the definition and display_definition attributes of all elements and This looks through the metrics, dimensions, and filter included in this dataset query. It also checks both the definition field of each element as well as the display definition for Unique Dimensions. :return: A collection of tables required to execute a query, """ return ordered_distinct_list( [ table for element in elements # Need extra for-loop to incl. the `display_definition` from `UniqueDimension` for attr in [getattr(element, "definition", None)] # ... but then filter Nones since most elements do not have `display_definition` if attr is not None for table in attr.tables_ # Omit the base table from this list if base_table != table ] )
def find_required_tables_to_join(elements, base_table): """ Collect all the tables required for a given list of slicer elements. This looks through the definition and display_definition attributes of all elements and This looks through the metrics, dimensions, and filter included in this slicer query. It also checks both the definition field of each element as well as the display definition for Unique Dimensions. :return: A collection of tables required to execute a query, """ return ordered_distinct_list([table for element in elements # Need extra for-loop to incl. the `display_definition` from `UniqueDimension` for attr in [getattr(element, 'definition', None), getattr(element, 'display_definition', None)] # ... but then filter Nones since most elements do not have `display_definition` if attr is not None for table in attr.tables_ # Omit the base table from this list if base_table != table])
def find_and_group_references_for_dimensions(references): """ Finds all of the references for dimensions and groups them by dimension, interval unit, number of intervals. This structure reflects how the references need to be joined to the slicer query. References of the same type (WoW, WoW.delta, WoW.delta_percent) can share a join query. :param references: :return: An `OrderedDict` where the keys are 3-item tuples consisting of "Dimension, interval unit, # of intervals. .. code-block:: python Example { (Dimension(date_1), 'weeks', 1): [WoW, WoW.delta], (Dimension(date_1), 'years', 1): [YoY], (Dimension(date_7), 'days', 1): [DoD, DoD.delta_percent], } """ def get_dimension_time_unit_and_interval(reference): return reference.dimension, reference.time_unit, reference.interval distinct_references = ordered_distinct_list(references) return groupby(distinct_references, get_dimension_time_unit_and_interval)
def find_and_group_references_for_dimensions(dimensions, references): """ Finds all of the references for dimensions and groups them by dimension, interval unit, number of intervals. This structure reflects how the references need to be joined to the dataset query. References of the same type (WoW, WoW.delta, WoW.delta_percent) can share a join query. :param dimensions: :param references: :return: An `OrderedDict` where the keys are 3-item tuples consisting of "Dimension, interval unit, # of intervals. .. code-block:: python Example { (Dimension(date_1), 'weeks', 1): [WoW, WoW.delta], (Dimension(date_1), 'years', 1): [YoY], (Dimension(date_7), 'days', 1): [DoD, DoD.delta_percent], } """ align_weekdays = ( dimensions and isinstance(dimensions[0], DatetimeInterval) and -1 < DATETIME_INTERVALS.index(dimensions[0].interval_key) < 3 ) def get_dimension_time_unit_and_interval(reference): defaults = (reference.time_unit, 1) time_unit, interval_muliplier = ( interval_weekdays.get(reference.time_unit, defaults) if align_weekdays else defaults ) field = find_field_in_modified_field(reference.field) return field, time_unit, interval_muliplier * reference.interval distinct_references = ordered_distinct_list(references) return groupby(distinct_references, get_dimension_time_unit_and_interval)