Transformation Code Generation
Transformation Code Generation
Context
Once a source-to-target mapping (STM) has been reviewed for gaps and ambiguities (see the STM Analysis prompt), this prompt generates working SQL or PySpark transformation code from the cleaned-up specification. The AI implements each rule exactly as written, flags any remaining ambiguities as inline comments, and lists post-generation issues for review.
Run this prompt after completing the STM Analysis to ensure gaps and ambiguities are resolved first. Always review the generated code against the actual source schema before running — the AI works from the mapping description, not the live database.
The Prompt
Generate [SQL / PySpark] transformation code from the following source-to-target mapping.
For each target field, implement the specified transformation rule exactly. Where the rule is ambiguous, flag it with a comment and implement the most conservative interpretation.
Target table: [TARGET TABLE NAME]Source table(s): [SOURCE TABLE NAMES]Database / dialect: [e.g. Snowflake SQL / SparkSQL / PostgreSQL]
Mapping:| Target field | Source field | Transformation rule ||---|---|---|| [field] | [source] | [rule — e.g. "direct copy", "UPPER()", "lookup in ref table X"] || ... | ... | ... |
Additional rules:- [Any join conditions, filter criteria, or business rules not captured in the table above]
After generating the code, list:1. Any ambiguous rules you had to interpret2. Any source fields referenced but not found in the mapping3. Any target fields with no source mapping (will need a default or manual input)Usage Instructions
- Open build-cli in your terminal.
- Copy the prompt above.
- Fill in the SQL dialect or PySpark context (for PySpark, specify whether you’re using DataFrames or Spark SQL, and mention the cluster or AWS Glue version if relevant).
- Paste your STM as a table — the more structured the mapping, the better the generated code.
- Review the generated code against the actual source schema before running.
- For PySpark output: follow up with the Python Pipeline Review pattern before submitting for peer review.
Example Output
The AI will produce transformation code such as:
-- Snowflake SQL: stg_orders → mart_order_summaryINSERT INTO mart_order_summary ( order_id, customer_key, order_date, order_status, total_amount_usd)SELECT o.order_id, c.customer_key, TRY_TO_DATE(o.order_date_str, 'YYYY-MM-DD') AS order_date, -- VARCHAR → DATE UPPER(TRIM(o.status)) AS order_status, -- AMBIGUOUS: exchange rate table not specified; using fx_rates.daily_rate as of order_date o.total_amount * fx.daily_rate AS total_amount_usdFROM stg_orders oJOIN dim_customer c ON o.customer_id = c.source_customer_idLEFT JOIN fx_rates fx ON fx.currency = o.currency AND fx.rate_date = TRY_TO_DATE(o.order_date_str, 'YYYY-MM-DD')WHERE o.load_date = CURRENT_DATE;Post-generation issues:
- Exchange rate table ambiguity (see comment) — confirm with business which rate to use.
record_statustarget field has no source mapping — defaulting to NULL; add a default or mapping rule.