ktmud commented on a change in pull request #12960:
URL: https://github.com/apache/superset/pull/12960#discussion_r570850468



##########
File path: 
superset/migrations/versions/070c043f2fdb_add_granularity_to_charts_where_missing.py
##########
@@ -0,0 +1,126 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""add granularity to charts where missing
+
+Revision ID: 070c043f2fdb
+Revises: e11ccdd12658
+Create Date: 2021-02-04 09:34:13.608891
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "070c043f2fdb"
+down_revision = "e11ccdd12658"
+
+import json
+
+from alembic import op
+from sqlalchemy import Boolean, Column, Integer, String, Text
+from sqlalchemy.ext.declarative import declarative_base
+
+from superset import db
+
+Base = declarative_base()
+
+
+class Slice(Base):
+    __tablename__ = "slices"
+
+    id = Column(Integer, primary_key=True)
+    params = Column(Text)
+    datasource_id = Column(Integer)
+    datasource_type = Column(String(200))
+
+
+class SqlaTable(Base):
+    __tablename__ = "tables"
+
+    id = Column(Integer, primary_key=True)
+    main_dttm_col = Column(String(250))
+
+
+class TableColumn(Base):
+    __tablename__ = "table_columns"
+
+    id = Column(Integer, primary_key=True)
+    table_id = Column(Integer)
+    is_dttm = Column(Boolean)
+    column_name = Column(String(255))
+
+
+def upgrade():
+    """
+    Adds the granularity param to charts without it populated. This is 
required for
+    time range filtering to work properly. Uses the following approach:
+
+    - Find all charts without a granularity or granularity_sqla param.
+    - Get the dataset that backs the chart.
+    - If the dataset has the main dttm column set, use it.
+    - Otherwise, find all the dttm columns in the dataset and use the first 
one (this
+      matches the behavior of Explore view on the frontend)
+    - If no dttm columns exist in the dataset, don't change the chart.
+    """
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    slices_changed = 0
+
+    for slc in session.query(Slice).filter(Slice.datasource_type == 
"table").all():

Review comment:
       
   This query will fetch all chart slices with SQLA datasource and run JSON 
parse in Python. Could we filter out only those didn't have `granularity` or 
`granularity_sqla` instead?
   
   ```suggestion
       for slc in (
           session.query(Slice)
               .filter(and_(
                   Slice.datasource_type == "table",
                   not Slice.params.like('%"granularity%'))
                ))
               .yield_per(500)
       ):
           
   ```
   
   Not that matters in practicality, but I'd also try to stream things whenever 
I try to fetch an unknown number of all results (`.yield_per(500)`).




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to