michael-s-molina commented on code in PR #36529:
URL: https://github.com/apache/superset/pull/36529#discussion_r2619537403


##########
superset/sql/execution/celery_task.py:
##########
@@ -0,0 +1,470 @@
+# 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.
+"""
+Celery task for async SQL execution.
+
+This module provides the Celery task for executing SQL queries asynchronously.
+It is used by SQLExecutor.execute_async() to run queries in the background.
+"""
+
+from __future__ import annotations
+
+import dataclasses
+import logging
+import uuid
+from sys import getsizeof
+from typing import Any, TYPE_CHECKING
+
+import msgpack
+from celery.exceptions import SoftTimeLimitExceeded
+from flask import current_app as app, has_app_context
+from flask_babel import gettext as __
+
+from superset import (
+    db,
+    results_backend,
+    security_manager,
+)
+from superset.common.db_query_status import QueryStatus
+from superset.constants import QUERY_CANCEL_KEY
+from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
+from superset.exceptions import (
+    SupersetErrorException,
+    SupersetErrorsException,
+)
+from superset.extensions import celery_app
+from superset.models.sql_lab import Query
+from superset.result_set import SupersetResultSet
+from superset.sql.execution.executor import execute_sql_with_cursor
+from superset.sql.parse import SQLScript
+from superset.sqllab.utils import write_ipc_buffer
+from superset.utils import json
+from superset.utils.core import override_user, zlib_compress
+from superset.utils.dates import now_as_float
+from superset.utils.decorators import stats_timing
+
+if TYPE_CHECKING:
+    pass
+
+logger = logging.getLogger(__name__)
+
+BYTES_IN_MB = 1024 * 1024
+
+
+def _get_query(query_id: int) -> Query:
+    """Get the query by ID."""
+    return db.session.query(Query).filter_by(id=query_id).one()
+
+
+def _handle_query_error(
+    ex: Exception,
+    query: Query,
+    payload: dict[str, Any] | None = None,
+    prefix_message: str = "",
+) -> dict[str, Any]:
+    """Handle error while processing the SQL query."""
+    payload = payload or {}
+    msg = f"{prefix_message} {str(ex)}".strip()
+    query.error_message = msg
+    query.tmp_table_name = None
+    query.status = QueryStatus.FAILED
+
+    if not query.end_time:
+        query.end_time = now_as_float()
+
+    # Extract DB-specific errors
+    if isinstance(ex, SupersetErrorException):
+        errors = [ex.error]
+    elif isinstance(ex, SupersetErrorsException):
+        errors = ex.errors
+    else:
+        errors = query.database.db_engine_spec.extract_errors(
+            str(ex), database_name=query.database.unique_name
+        )
+
+    errors_payload = [dataclasses.asdict(error) for error in errors]
+    if errors:
+        query.set_extra_json_key("errors", errors_payload)
+
+    db.session.commit()  # pylint: disable=consider-using-transaction
+    payload.update({"status": query.status, "error": msg, "errors": 
errors_payload})
+    if troubleshooting_link := app.config.get("TROUBLESHOOTING_LINK"):
+        payload["link"] = troubleshooting_link
+    return payload
+
+
+def _serialize_payload(payload: dict[Any, Any]) -> bytes:
+    """Serialize payload for storage based on RESULTS_BACKEND_USE_MSGPACK 
config."""
+    from superset import results_backend_use_msgpack
+
+    if results_backend_use_msgpack:
+        return msgpack.dumps(payload, default=json.json_iso_dttm_ser, 
use_bin_type=True)
+    return json.dumps(payload, default=json.json_iso_dttm_ser, 
ignore_nan=True).encode(
+        "utf-8"
+    )
+
+
+def _prepare_statement_blocks(
+    rendered_query: str,
+    db_engine_spec: Any,
+) -> tuple[SQLScript, list[str]]:
+    """
+    Parse SQL and build statement blocks for execution.
+
+    Note: RLS, security checks, and other preprocessing are handled by
+    SQLExecutor before the query reaches this task.
+    """
+    parsed_script = SQLScript(rendered_query, engine=db_engine_spec.engine)
+
+    # Build statement blocks for execution
+    if db_engine_spec.run_multiple_statements_as_one:
+        blocks = 
[parsed_script.format(comments=db_engine_spec.allows_sql_comments)]
+    else:
+        blocks = [
+            statement.format(comments=db_engine_spec.allows_sql_comments)
+            for statement in parsed_script.statements
+        ]
+
+    return parsed_script, blocks
+
+
+def _finalize_successful_query(
+    query: Query,
+    execution_results: list[tuple[str, SupersetResultSet | None, float, int]],
+    payload: dict[str, Any],
+    total_execution_time_ms: float,
+) -> None:
+    """Update query metadata and payload after successful execution."""
+    # Calculate total rows across all statements
+    total_rows = 0
+    statements_data: list[dict[str, Any]] = []
+
+    for stmt_sql, result_set, exec_time, rowcount in execution_results:
+        if result_set is not None:
+            # SELECT statement
+            total_rows += result_set.size
+            data, columns = _serialize_result_set(result_set)
+            statements_data.append(
+                {
+                    "statement": stmt_sql,
+                    "data": data,
+                    "columns": columns,
+                    "row_count": result_set.size,
+                    "execution_time_ms": exec_time,
+                }
+            )
+        else:
+            # DML statement - no data, just row count
+            statements_data.append(
+                {
+                    "statement": stmt_sql,
+                    "data": None,
+                    "columns": [],
+                    "row_count": rowcount,
+                    "execution_time_ms": exec_time,
+                }
+            )
+
+    query.rows = total_rows
+    query.progress = 100
+    query.set_extra_json_key("progress", None)
+    # Store columns from last statement (for compatibility)
+    if execution_results and execution_results[-1][1] is not None:
+        query.set_extra_json_key("columns", execution_results[-1][1].columns)
+    query.end_time = now_as_float()
+
+    payload.update(
+        {
+            "status": QueryStatus.SUCCESS,
+            "statements": statements_data,
+            "total_execution_time_ms": total_execution_time_ms,
+            "query": query.to_dict(),
+        }
+    )
+    payload["query"]["state"] = QueryStatus.SUCCESS
+
+
+def _store_results_in_backend(
+    query: Query,
+    payload: dict[str, Any],
+    database: Any,
+) -> None:
+    """Store query results in the results backend."""
+    key = str(uuid.uuid4())
+    payload["query"]["resultsKey"] = key
+    logger.info(
+        "Query %s: Storing results in results backend, key: %s",
+        str(query.id),
+        key,
+    )
+    stats_logger = app.config["STATS_LOGGER"]
+    with stats_timing("sqllab.query.results_backend_write", stats_logger):
+        with stats_timing(
+            "sqllab.query.results_backend_write_serialization", stats_logger
+        ):
+            serialized_payload = _serialize_payload(payload)
+
+            # Check payload size limit
+            if sql_lab_payload_max_mb := 
app.config.get("SQLLAB_PAYLOAD_MAX_MB"):
+                serialized_payload_size = len(serialized_payload)

Review Comment:
   - Copilot misunderstands the purpose: We're checking serialized byte size for
    storage/transmission limits, NOT Python object memory footprint
    - len(serialized_payload) returns the byte length of the serialized bytes 
object - exactly
    what we need
    - getsizeof(serialized_payload) would include Python object overhead (type 
info, reference
    counts, etc.) which is irrelevant for:
      - Results backend storage limits (Redis, S3)
      - Network transmission size
      - Cache size management
    - The config SQLLAB_PAYLOAD_MAX_MB explicitly refers to serialized payload 
size, not Python
    memory
    - len() is the correct method when working with serialized byte strings



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

Reply via email to