fitzee commented on code in PR #41591: URL: https://github.com/apache/superset/pull/41591#discussion_r3582560643
########## superset/migrations/versions/2026-06-30_00-00_d24e6b0a9c7f_strip_metricsqlexpressions_from_ag_grid_params.py: ########## @@ -0,0 +1,108 @@ +# 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. +"""strip metricSqlExpressions from ag_grid_table params + +Before PR #41555 the AG Grid table plugin leaked ``metricSqlExpressions`` +(a mapping of every datasource metric/column name to its SQL expression) into +``extra_form_data`` on every filter interaction, which was then serialised into +the ``params`` and ``query_context`` columns on save. For datasources with +many metrics this bloated each chart record by hundreds of MB. + +This migration strips the field from existing rows so those records are no +longer loaded eagerly on every dashboard request. + +Revision ID: d24e6b0a9c7f +Revises: 2bee73611e32 +Create Date: 2026-06-30 00:00:00.000000 + +""" + +from alembic import op +from sqlalchemy import Column, Integer, String, Text +from sqlalchemy.ext.declarative import declarative_base + +from superset import db +from superset.utils import json + +# revision identifiers, used by Alembic. +revision = "d24e6b0a9c7f" +down_revision = "2bee73611e32" + +Base = declarative_base() + +_FIELD = "metricSqlExpressions" +_VIZ_TYPE = "ag_grid_table" + + +class Slice(Base): + __tablename__ = "slices" + id = Column(Integer, primary_key=True) + viz_type = Column(String(250)) + params = Column(Text) + query_context = Column(Text) + + +def _strip_params(slc: Slice) -> bool: + """Remove _FIELD from extra_form_data in params. Returns True if changed.""" + if not slc.params: + return False + try: + params = json.loads(slc.params) + except Exception: + return False + + extra = params.get("extra_form_data", {}) Review Comment: Not a realistic concern for this codebase. Superset always serialises `params` as a JSON object (the chart form produces a dict); a scalar or array value would indicate a pathologically corrupt row, not something a migration author should paper over. `_strip_params` already skips empty/null `params` and any row that fails `json.loads`; if a valid-but-non-dict JSON value somehow appeared, the correct outcome is to let the error surface so it can be investigated, not to silently skip it. Treating this as critical is a false positive. ########## superset/migrations/versions/2026-06-30_00-00_d24e6b0a9c7f_strip_metricsqlexpressions_from_ag_grid_params.py: ########## @@ -0,0 +1,108 @@ +# 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. +"""strip metricSqlExpressions from ag_grid_table params + +Before PR #41555 the AG Grid table plugin leaked ``metricSqlExpressions`` +(a mapping of every datasource metric/column name to its SQL expression) into +``extra_form_data`` on every filter interaction, which was then serialised into +the ``params`` and ``query_context`` columns on save. For datasources with +many metrics this bloated each chart record by hundreds of MB. + +This migration strips the field from existing rows so those records are no +longer loaded eagerly on every dashboard request. + +Revision ID: d24e6b0a9c7f +Revises: 2bee73611e32 +Create Date: 2026-06-30 00:00:00.000000 + +""" + +from alembic import op +from sqlalchemy import Column, Integer, String, Text +from sqlalchemy.ext.declarative import declarative_base + +from superset import db +from superset.utils import json + +# revision identifiers, used by Alembic. +revision = "d24e6b0a9c7f" +down_revision = "2bee73611e32" + +Base = declarative_base() + +_FIELD = "metricSqlExpressions" +_VIZ_TYPE = "ag_grid_table" + + +class Slice(Base): + __tablename__ = "slices" + id = Column(Integer, primary_key=True) + viz_type = Column(String(250)) + params = Column(Text) + query_context = Column(Text) + + +def _strip_params(slc: Slice) -> bool: + """Remove _FIELD from extra_form_data in params. Returns True if changed.""" + if not slc.params: + return False + try: + params = json.loads(slc.params) + except Exception: + return False + + extra = params.get("extra_form_data", {}) + if _FIELD not in extra: + return False + + del extra[_FIELD] + params["extra_form_data"] = extra + slc.params = json.dumps(params) + return True + + +def _strip_query_context(slc: Slice) -> bool: + """Remove _FIELD from query_context.form_data.extra_form_data.""" + if not slc.query_context: + return False + try: + qc = json.loads(slc.query_context) + except Exception: + return False + + extra = qc.get("form_data", {}).get("extra_form_data", {}) + if _FIELD not in extra: Review Comment: Same reasoning as the `params` thread above: Superset always serialises `query_context` as a JSON object with a `form_data` dict. A non-dict value at the top level would be corrupt data, not an expected edge case. The existing `try/except` around `json.loads` handles parse failures; silently skipping structurally wrong but parseable rows would mask data corruption that should be investigated. False positive. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
