def _set_query_final(self, query: Query, final: bool) -> None: """ Set the 'final' clause of a Query. A query set as final will force ClickHouse to perform a merge on the results of the query. This is very performance heavy and should be avoided whenever possible. """ query.set_from_clause(replace(query.get_from_clause(), final=final))
def process_query(self, query: Query, request_settings: RequestSettings) -> None: if request_settings.get_turbo(): return project_ids = get_project_ids_in_query_ast(query, self.__project_column) set_final = False if project_ids: final, exclude_group_ids = get_projects_query_flags( list(project_ids), self.__replacer_state_name, ) if final: metrics.increment("final", tags={"cause": "final_flag"}) if not final and exclude_group_ids: # If the number of groups to exclude exceeds our limit, the query # should just use final instead of the exclusion set. max_group_ids_exclude = get_config( "max_group_ids_exclude", settings.REPLACER_MAX_GROUP_IDS_TO_EXCLUDE) assert isinstance(max_group_ids_exclude, int) if len(exclude_group_ids) > max_group_ids_exclude: metrics.increment("final", tags={"cause": "max_groups"}) set_final = True else: query.add_condition_to_ast( not_in_condition( FunctionCall(None, "assumeNotNull", (Column(None, None, "group_id"), )), [Literal(None, p) for p in exclude_group_ids], )) else: set_final = final query.set_from_clause(replace(query.get_from_clause(), final=set_final))
def process_query(self, query: Query, query_settings: QuerySettings) -> None: query.set_from_clause(replace(query.get_from_clause(), final=True))