@@ -326,7 +326,7 @@ async def generate_chart( # noqa: C901
326326
327327 # Persist the chart only when explicitly requested (save_chart=False by default)
328328 if request .save_chart :
329- await ctx .report_progress (2 , 5 , "Creating chart in database " )
329+ await ctx .report_progress (2 , 5 , "Validating chart query " )
330330 from superset .commands .chart .create import CreateChartCommand
331331
332332 # Find the dataset to get its numeric ID
@@ -414,6 +414,64 @@ async def generate_chart( # noqa: C901
414414 )
415415 await ctx .debug ("Chart name: chart_name=%s" % (chart_name ,))
416416
417+ # Compile before persisting. A failed query must not leave a broken
418+ # chart row behind and then rely on a later transaction to delete it.
419+ with event_logger .log_context (action = "mcp.generate_chart.compile_check" ):
420+ compile_result = _compile_chart (form_data , dataset .id )
421+ if not compile_result .success :
422+ logger .warning (
423+ "Compile check failed before chart creation: %s" ,
424+ compile_result .error ,
425+ )
426+ await ctx .warning (
427+ "Chart compile check failed: error=%s" % (compile_result .error ,)
428+ )
429+ from superset .mcp_service .common .error_schemas import (
430+ ChartGenerationError ,
431+ )
432+
433+ execution_time = int ((time .time () - start_time ) * 1000 )
434+ error = compile_result .error_obj or ChartGenerationError (
435+ error_type = "compile_error" ,
436+ message = "Chart query failed to execute. The chart was not saved." ,
437+ details = str (compile_result .error ) or "" ,
438+ suggestions = [
439+ "Check that all columns exist in the dataset" ,
440+ "Verify aggregate functions are compatible with column types" ,
441+ "Ensure filters reference valid columns" ,
442+ "Try simplifying the chart configuration" ,
443+ ],
444+ error_code = "CHART_COMPILE_FAILED" ,
445+ )
446+ return GenerateChartResponse .model_validate (
447+ {
448+ "chart" : None ,
449+ "error" : error .model_dump (),
450+ "form_data" : (
451+ _sanitize_generate_chart_form_data_for_llm_context (
452+ form_data
453+ )
454+ ),
455+ "performance" : {
456+ "query_duration_ms" : execution_time ,
457+ "cache_status" : "error" ,
458+ "optimization_suggestions" : [],
459+ },
460+ "warnings" : (
461+ sanitization_warnings
462+ + runtime_warnings
463+ + response_warnings
464+ + compile_result .warnings
465+ ),
466+ "success" : False ,
467+ "schema_version" : "2.0" ,
468+ "api_version" : "v1" ,
469+ }
470+ )
471+ response_warnings .extend (compile_result .warnings )
472+
473+ await ctx .report_progress (3 , 5 , "Creating chart in database" )
474+
417475 try :
418476 with event_logger .log_context (action = "mcp.generate_chart.db_write" ):
419477 command = CreateChartCommand (
@@ -489,66 +547,6 @@ async def generate_chart( # noqa: C901
489547 # Add any validation warnings (e.g., virtual dataset warnings)
490548 response_warnings .extend (dataset_check .warnings )
491549
492- # Compile check: execute the chart query to catch runtime errors
493- await ctx .report_progress (3 , 5 , "Running compile check (test query)" )
494- with event_logger .log_context (
495- action = "mcp.generate_chart.compile_check"
496- ):
497- compile_result = _compile_chart (form_data , dataset .id )
498- if not compile_result .success :
499- # Query failed — delete the broken chart and return an error
500- logger .warning (
501- "Compile check failed for chart %s: %s" ,
502- chart_id ,
503- compile_result .error ,
504- )
505- await ctx .warning (
506- "Chart compile check failed: error=%s" % (compile_result .error ,)
507- )
508- from superset .daos .chart import ChartDAO
509-
510- ChartDAO .delete ([chart ])
511- from superset .mcp_service .common .error_schemas import (
512- ChartGenerationError ,
513- )
514-
515- execution_time = int ((time .time () - start_time ) * 1000 )
516- error = compile_result .error_obj or ChartGenerationError (
517- error_type = "compile_error" ,
518- message = (
519- "Chart query failed to execute. The chart was not saved."
520- ),
521- details = str (compile_result .error ) or "" ,
522- suggestions = [
523- "Check that all columns exist in the dataset" ,
524- "Verify aggregate functions are compatible "
525- "with column types" ,
526- "Ensure filters reference valid columns" ,
527- "Try simplifying the chart configuration" ,
528- ],
529- error_code = "CHART_COMPILE_FAILED" ,
530- )
531- return GenerateChartResponse .model_validate (
532- {
533- "chart" : None ,
534- "error" : error .model_dump (),
535- "form_data" : (
536- _sanitize_generate_chart_form_data_for_llm_context (
537- form_data
538- )
539- ),
540- "performance" : {
541- "query_duration_ms" : execution_time ,
542- "cache_status" : "error" ,
543- "optimization_suggestions" : [],
544- },
545- "success" : False ,
546- "schema_version" : "2.0" ,
547- "api_version" : "v1" ,
548- }
549- )
550- response_warnings .extend (compile_result .warnings )
551-
552550 except CommandException as e :
553551 logger .error ("Chart creation failed: %s" , e )
554552 await ctx .error ("Chart creation failed: error=%s" % (str (e ),))
0 commit comments