Create database tables from SQL (CTAS) or natural language descriptions
If the user selects "Cancel" at ANY point (any ask_user response), you MUST immediately stop ALL work. Do NOT:
Return immediately with:
{"table_name": "", "output": "Cancelled by user."}
Detect input mode:
The user's SQL already fully defines the output schema. Do NOT ask the user about table usage, purpose, or column selection — the SQL is the spec.
describe_table for each source table to understand column types.read_query with LIMIT 10 to validate the query output.wide_order_customer). If the user specified a name, use it.ask_user here. The DDL confirmation in Phase 2 is the only user interaction needed.Natural language is ambiguous, so clarification may be needed before generating DDL.
describe_table for any referenced existing tables to infer column types.ask_user to clarify. Only ask about genuinely missing information — do NOT ask about table usage or purpose if the user already described the schema.Generate the exact DDL SQL statement and present it to the user for confirmation.
Include the full DDL SQL inside the ask_user question text. This is required because when running as a sub-agent, all intermediate assistant messages are collapsed in the UI — the user can ONLY see the ask_user interaction widget.
Generate CTAS: CREATE TABLE {schema}.{table_name} AS ({select_sql})
Generate: CREATE TABLE {schema}.{table_name} ({column_defs})
Call ask_user with the complete DDL embedded in the question:
ask_user(questions=[{
"question": "Generated DDL:\n\nCREATE TABLE {schema}.{table_name} AS (\n SELECT ...\n);\n\nConfirm execution?",
"options": ["Execute", "Modify", "Cancel"]
}])
Formatting rules for the question text:
\n for line breaks to keep the SQL readableBased on user response:
ask_user again with the updated DDL{"table_name": "", "output": "Cancelled by user."}. Do NOT continue.execute_ddl(sql) with the confirmed DDL statement.read_query("SELECT COUNT(*) FROM {schema}.{table_name}") to confirm row countdescribe_table("{schema}.{table_name}") to confirm schema matchesdescribe_table("{schema}.{table_name}") to confirm the created schema.If DDL fails:
ask_user, and retry (up to 3 attempts)ask_userOutput a summary including:
task(type="gen_semantic_model", prompt="{table_name}")ask_user before executing any DDL — never create tables without user confirmationgen_semantic_model