AI-Written SQL: A Compilable Spec Breaks the Hallucination

Picture4png
AI writes SQL fast, but would you ship it straight to production?
The goal is “find the credit-card customers whose transaction amount rose for three months in a row”. AI instantly hands back a chunk of CTEs with LAG. The syntax is fine and it runs, but is the logic right? Nobody dares to say “I understand it”.
This is the awkward part of AI-written SQL: correct syntax does not mean correct semantics. Even at 90% accuracy, how do you know this one is not in the 10%? Data decisions are not a lottery; a beautiful result that answers the wrong question is more dangerous than an error. Reviewing AI’s SQL means pulling the CTEs apart and running them one by one, which is redoing AI’s work by hand.
The problem is not whether AI is capable, but how you use it. AI coding cannot get around specs: a person writes the spec, AI produces the code, a person reviews it. The clearer the spec, the less it hallucinates, but no spec clears out the guessing: AI looks for the most likely answer in a probability space, it does not reason logically.

Stop letting AI guess SQL. Let SQLazy compile the spec into SQL, and the hallucination is gone.
Then what does AI do? It turns the spoken spec into a formal SQLazy spec:

ccard: sort customer_id asc, cmonth asc
segment amount; down; partition customer_id; as noRising
summarize count noRising as ccount; group customer_id, noRising
filter (ccount>=3)

This is a spec you can compile and execute. Every step is still plain language, intermediate results can be inspected step by step, and mistakes are stopped before execution instead of being debugged after the run.
Then compile it into SQL and you are done:

WITH t4 AS (
  SELECT customer_id, noRising, COUNT(noRising) AS ccount
  FROM (
    SELECT customer_id, amount, cmonth
      , SUM(CASE
        WHEN amount < col__1 THEN 1
        ELSE 0
      END) OVER (PARTITION BY customer_id
        ORDER BY customer_id ASC, cmonth ASC
        ROWS UNBOUNDED PRECEDING) + 1 AS noRising
    FROM (
      SELECT customer_id, amount, cmonth
        , LAG(amount, 1) OVER (PARTITION BY customer_id
          ORDER BY customer_id ASC, cmonth ASC) AS col__1
      FROM ccard t1
    ) t_2
  ) t2
  GROUP BY customer_id, noRising
  HAVING (COUNT(noRising) >= 3)
)
SELECT customer_id, noRising, COUNT(noRising) AS ccount
FROM (
  SELECT customer_id, amount, cmonth
    , SUM(CASE
      WHEN amount < col__1 THEN 1
      ELSE 0
    END) OVER (PARTITION BY customer_id
      ORDER BY customer_id ASC, cmonth ASC
      ROWS UNBOUNDED PRECEDING) + 1 AS noRising
  FROM (
    SELECT customer_id, amount, cmonth
      , LAG(amount, 1) OVER (PARTITION BY customer_id
        ORDER BY customer_id ASC, cmonth ASC) AS col__1
    FROM ccard t1
  ) t_2
) t2
GROUP BY customer_id, noRising
HAVING (COUNT(noRising) >= 3)
ORDER BY CASE
  WHEN customer_id IS NULL THEN 1
  ELSE 0
END, customer_id ASC

Same logic: however many times you compile it, the result is identical, with no probability and no hallucination.
Need another database? Switch an option and the dialect adapts to MySQL or Oracle, with no rewrite.
AI is no longer the decision maker guessing at the business; it is the translator turning a spoken spec into a formal one. SQLazy is a plain, readable structured data computation language: clear steps, deterministic output and easy auditing. It lets data development get back to thinking.