def build_and_rank_plans( self, query: LogicalQuery, settings: RequestSettings ) -> Sequence[ClickhouseQueryPlan]: with sentry_sdk.start_span( op="build_plan.selected_storage", description="select_storage" ): storage, mappers = self.__selector.select_storage(query, settings) with sentry_sdk.start_span( op="build_plan.selected_storage", description="translate" ): # The QueryTranslator class should be instantiated once for each call to build_plan, # to avoid cache conflicts. clickhouse_query = QueryTranslator(mappers).translate(query) with sentry_sdk.start_span( op="build_plan.selected_storage", description="set_from_clause" ): clickhouse_query.set_from_clause( get_query_data_source( storage.get_schema().get_data_source(), final=query.get_final(), sampling_rate=query.get_sample(), ) ) cluster = storage.get_cluster() db_query_processors = [ *storage.get_query_processors(), *self.__post_processors, MandatoryConditionApplier(), ] return [ ClickhouseQueryPlan( query=clickhouse_query, plan_query_processors=[], db_query_processors=db_query_processors, storage_set_key=storage.get_storage_set_key(), execution_strategy=SimpleQueryPlanExecutionStrategy( cluster=cluster, db_query_processors=db_query_processors, splitters=storage.get_query_splitters(), ), ) ]
def build_plan(self, request: Request) -> ClickhouseQueryPlan: clickhouse_query = QueryTranslator(self.__mappers).translate(request.query) clickhouse_query.set_from_clause(self.__storage.get_schema().get_data_source()) cluster = self.__storage.get_cluster() return ClickhouseQueryPlan( query=clickhouse_query, plan_processors=[], execution_strategy=SimpleQueryPlanExecutionStrategy( cluster=cluster, db_query_processors=[ *self.__storage.get_query_processors(), *self.__post_processors, MandatoryConditionApplier(), ], splitters=self.__storage.get_query_splitters(), ), )
def test_translation(mappers: TranslationMappers, query: SnubaQuery, expected: ClickhouseQuery) -> None: translated = QueryTranslator(mappers).translate(query) # TODO: consider providing an __eq__ method to the Query class. Or turn it into # a dataclass. assert expected.get_selected_columns() == translated.get_selected_columns() assert expected.get_groupby() == translated.get_groupby() assert expected.get_condition() == translated.get_condition() assert expected.get_arrayjoin() == translated.get_arrayjoin() assert expected.get_having() == translated.get_having() assert expected.get_orderby() == translated.get_orderby()