You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/11/18 02:44:39 UTC

[GitHub] [superset] zhaoyongjie commented on a change in pull request #17466: refactor(QueryObject)

zhaoyongjie commented on a change in pull request #17466:
URL: https://github.com/apache/superset/pull/17466#discussion_r751862944



##########
File path: superset/common/query_object.py
##########
@@ -75,12 +76,75 @@ class DeprecatedField(NamedTuple):
 )
 
 
+class QueryObjectFactory:
+    def create(self,
+               result_type: ChartDataResultType,
+               datasource: Optional[DatasourceDict] = None,
+               extras: Optional[Dict[str, Any]] = None,
+               row_limit: Optional[int] = None,
+               time_range: Optional[str] = None,
+               time_shift: Optional[str] = None,
+               **kwargs) -> QueryObject:
+        datasource_model_instance = None
+        if datasource:
+            datasource_model_instance = self._convert_to_model(datasource)
+        extras = self._process_extras(extras)
+        row_limit = self._process_row_limit(row_limit, result_type)
+        from_dttm, to_dttm = self._get_dttms(time_range, time_shift, extras)
+        return QueryObject(result_type,
+                           datasource=datasource_model_instance,
+                           extras=extras,
+                           row_limit=row_limit,
+                           from_dttm=from_dttm,
+                           to_dttm=to_dttm,
+                           time_range=time_range,
+                           time_shift=time_shift,
+                           **kwargs)

Review comment:
       ```python
       @classmethod
       def create(cls,
                  result_type: ChartDataResultType,
                  datasource: Optional[DatasourceDict] = None,
                  extras: Optional[Dict[str, Any]] = None,
                  row_limit: Optional[int] = None,
                  time_range: Optional[str] = None,
                  time_shift: Optional[str] = None,
                  **kwargs) -> QueryObject:
           query_object_instance = cls()
           datasource_model_instance = None
           if datasource:
               datasource_model_instance = query_object_instance._convert_to_model(datasource)
           extras = query_object_instance._process_extras(extras)
           row_limit = query_object_instance._process_row_limit(row_limit, result_type)
           from_dttm, to_dttm = query_object_instance._get_dttms(time_range, time_shift, extras)
           return QueryObject(result_type,
                              datasource=datasource_model_instance,
                              extras=extras,
                              row_limit=row_limit,
                              from_dttm=from_dttm,
                              to_dttm=to_dttm,
                              time_range=time_range,
                              time_shift=time_shift,
                              **kwargs)
   ```

##########
File path: superset/common/query_context.py
##########
@@ -92,19 +92,22 @@ def __init__(
         self,
         datasource: DatasourceDict,
         queries: List[Dict[str, Any]],
-        force: bool = False,
-        custom_cache_timeout: Optional[int] = None,
         result_type: Optional[ChartDataResultType] = None,
         result_format: Optional[ChartDataResultFormat] = None,
+        force: bool = False,
+        custom_cache_timeout: Optional[int] = None,
     ) -> None:
         self.datasource = ConnectorRegistry.get_datasource(
             str(datasource["type"]), int(datasource["id"]), db.session
         )
-        self.force = force
-        self.custom_cache_timeout = custom_cache_timeout
         self.result_type = result_type or ChartDataResultType.FULL
         self.result_format = result_format or ChartDataResultFormat.JSON
-        self.queries = [QueryObject(self, **query_obj) for query_obj in queries]
+        for qd in queries:
+            qd.setdefault("result_type", result_type)
+        query_object_factory = QueryObjectFactory()
+        self.queries = [query_object_factory.create(**query_obj) for query_obj in queries]

Review comment:
       ```Python
          self.queries = [QueryObjectFactory.create(**query_obj) for query_obj in queries]
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org