gcbibek3353 opened a new pull request, #45057:
URL: https://github.com/apache/superset/pull/45057

   ### Summary
   
   Fixes a bug where **Download to CSV in SQL Lab produces an empty file** when 
the query uses MySQL-style session variables (`SET @var = ...; SELECT ... WHERE 
col = @var`).
   
   ---
   
   ### Root Cause
   
   In `SqlResultExportCommand.run()` (the code path triggered by the Download 
CSV button), when there is no cached result in the results backend, the code 
re-runs the query on a fresh DB connection:
   
   ```python
   # Before (broken)
   sql = self._query.executed_sql
   ```
   
   `query.executed_sql` only holds the **last statement executed** — because 
`sql_lab.py` overwrites it on every loop iteration in 
`execute_sql_statements()`:
   
   ```python
   # superset/sql_lab.py
   for i, block in enumerate(blocks):
       query.executed_sql = database.mutate_sql_based_on_config(block)  # 
overwritten each time
       result_set = execute_query(query, cursor, log_params)
   ```
   
   So after the loop, `executed_sql` = only the final `SELECT` statement. The 
`SET @var = ...` statements are gone.
   
   When the CSV export re-runs just the `SELECT` on a **new connection**, the 
session variables are `NULL`. Any `WHERE col = @var` condition evaluates to 
`UNKNOWN`, matching zero rows → **empty CSV**.
   
   The "Run" button works correctly because all statements share a single 
connection and cursor throughout execution.
   
   ---
   
   ### Fix
   
   Use `self._query.sql` instead — this field holds the **full original SQL** 
as typed by the user, including all `SET` statements.
   
   ```python
   # After (fixed)
   sql = self._query.sql
   ```
   
   This ensures all statements are re-executed in order on the new connection, 
so session variables are defined before the `SELECT` runs.
   
   ---
   
   ### Reproduction Steps
   
   1. Open SQL Lab connected to a MySQL database
   2. Run the following query:
   ```sql
   SET @my_val = 'some_value';
   SELECT * FROM my_table WHERE my_column = @my_val;
   ```
   3. Results display correctly in SQL Lab ✅
   4. Click **Download to CSV** → CSV file is empty ❌
   5. Apply this fix → CSV file contains the correct rows ✅
   
   To reproduce without a specific table, use `information_schema`:
   ```sql
   SET @target_schema = 'information_schema';
   SELECT table_name, table_type FROM information_schema.TABLES
   WHERE table_schema = @target_schema
   ORDER BY table_name;
   ```
   
   ---
   
   ### Impact
   
   - Only affects the **no-results-backend / cache-miss CSV export path**
   - If `results_backend` has a cached result (the `blob` path), this code is 
not reached and was already working correctly
   - The fix is a single-line change with no side effects
   
   ---
   
   ### Testing
   
   - Ran the reproduction query above before and after the fix
   - Confirmed CSV is empty before the fix and contains correct data after
   - Queries without session variables are unaffected (same `query.sql` = 
`query.executed_sql` for single-statement 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: [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