Chapter 2: Advantages & Disadvantages of Using a Coding Companion
Introduces the productivity benefits of LLM coding companions in data engineering while emphasizing that plausible-looking output requires human validation.
After this chapter you will be able to
- Coding companion productivity benefits. Explain how LLM coding companions can help with SQL queries, data transformations, repetitive tasks, and rapid prototyping by generating code and speeding up iteration.
- Hallucinations and human validation. Describe hallucinations as confident but incorrect AI output, give a data-engineering example, and explain why generated code must be checked against the actual schema and business context, then refined through feedback rather than treated as production-ready.
- Companion and workspace roles. Describe how the data engineer prompts an AI coding companion, receives code or suggestions, and then uses a coding environment to inspect, run, and test code against real systems.
- Purpose of keeping tools separate. Explain why the early chapters keep the companion separate from the execution environment: the visible prompt-response process helps readers improve prompts, evaluate generated code before running it, and spot hallucinations or misunderstandings. Distinguish this learning setup from tools that integrate AI into an IDE and from the book's later use of the OpenAI API.
- Benefits for complex coding. Explain why an LLM coding companion is more valuable for intricate code than for simple SQL queries or Python functions: complex work calls for rapid iteration, fewer errors, and refined logic.
- Iterative prompt feedback loop. Describe how to use the companion's output as feedback, revising prompts over successive passes to steer it toward a more accurate or elegant solution.
- Product order normalization. Describe how to turn nested customer-order JSON into a product_order DataFrame: make one row per product, retain its order information, extract product details from its description, and apply the specified status rules.
- Conversational code generation. Explain how to ask ChatGPT for transformation code by supplying sample data and explicit output requirements, then refine the prompt when requirements change. Describe how this can reduce boilerplate and let the engineer focus on higher-level architecture.
- Missing project context. Explain that an LLM responds to information available in the current prompt, not automatically to the full codebase, business rules, naming conventions, or data lineage. Describe why generalized advice may therefore be wrong for a specific environment.
- Context window limits. Explain that a context window holds only a finite number of tokens, so long schemas, code, and instructions can crowd out earlier information. Describe how losing that information can cause missed constraints or relationships, incomplete reasoning, or fabricated code.
- Pagila schema and purpose. Explain that Pagila is a fictional DVD-rental database with interconnected tables such as rental, film, inventory, and customer, and that its foreign-key relationships make it useful for testing AI-generated queries involving joins, aggregations, and business logic.
- Rental count as popularity. Explain why SQL that joins films to rentals and counts rentals per film may look correct yet fail to answer what the user meant by the most popular film.
- Testing and refining generated SQL. Describe how to run ChatGPT’s SQL for the Pagila question in pgAdmin, compare the result with expectations, and revise the prompt with a qualification such as recent rentals or by category to see how the output changes.
What it builds on
- Installing Jupyter Lab for Python Work (chapter 1) · Covers setting up JupyterLab for Python work.
- Installing PostgreSQL and pgAdmin (chapter 1) · Setting up PostgreSQL and pgAdmin.
Section by section
Introduction
Introduces the productivity benefits of LLM coding companions in data engineering while emphasizing that plausible-looking output requires human validation.
- Coding companion productivity benefits: Explain how LLM coding companions can help with SQL queries, data transformations, repetitive tasks, and rapid prototyping by generating code and speeding up iteration.
Why it matters: These benefits explain why a data engineer might use a coding companion in the first place. - Hallucinations and human validation: Describe hallucinations as confident but incorrect AI output, give a data-engineering example, and explain why generated code must be checked against the actual schema and business context, then refined through feedback rather than treated as production-ready.
Why it matters: Plausible code can still be wrong, so human review is essential before relying on it.
Key terms: LLM, coding companion, rapid prototyping, iterative development, hallucinations, schema, business context, prompt feedback loops, human oversight
2.1 Mental Model: The Data Engineer and the Coding Companion
This section presents the data engineer as the link between an AI coding companion and a separate coding environment, and explains why that separation helps readers learn to use AI-generated code critically.
- Companion and workspace roles: Describe how the data engineer prompts an AI coding companion, receives code or suggestions, and then uses a coding environment to inspect, run, and test code against real systems.
Why it matters: The companion can suggest code, but you remain responsible for what runs in your environment. - Purpose of keeping tools separate: Explain why the early chapters keep the companion separate from the execution environment: the visible prompt-response process helps readers improve prompts, evaluate generated code before running it, and spot hallucinations or misunderstandings. Distinguish this learning setup from tools that integrate AI into an IDE and from the book's later use of the OpenAI API.
Why it matters: Seeing each step makes it easier to judge the AI's output rather than treating it as automatically correct.
Key terms: data engineer, AI coding companion, prompts, responses, coding environment, prompt-response dance, hallucinate, IDE, OpenAI API
2.2 Advantages of Using an AI/LLM Coding Companion
This section explains why coding companions become more useful as code grows complex and how revising prompts can improve their output.
- Benefits for complex coding: Explain why an LLM coding companion is more valuable for intricate code than for simple SQL queries or Python functions: complex work calls for rapid iteration, fewer errors, and refined logic.
Why it matters: The benefit is clearest when the coding task is difficult enough that speed and accuracy matter. - Iterative prompt feedback loop: Describe how to use the companion's output as feedback, revising prompts over successive passes to steer it toward a more accurate or elegant solution.
Why it matters: The first output need not be final; each response can guide a better prompt.
Key terms: LLMs, coding companion, rapid iteration, error reduction, refined logic, iterative feedback loop, revise prompts
2.2.1 Rapid Code Generation for Data Engineering Tasks
This section shows how a conversational AI coding companion can generate Python for a nested-data transformation while emphasizing testing and hands-on understanding.
- Product order normalization: Describe how to turn nested customer-order JSON into a product_order DataFrame: make one row per product, retain its order information, extract product details from its description, and apply the specified status rules.
Why it matters: The desired output is the concrete test of whether the generated code solves the data engineering task. - Conversational code generation: Explain how to ask ChatGPT for transformation code by supplying sample data and explicit output requirements, then refine the prompt when requirements change. Describe how this can reduce boilerplate and let the engineer focus on higher-level architecture.
Why it matters: A specific, revisable request is what makes the coding companion useful for a multi-step task. - Manual implementation and testing challenges: Explain why implementing the example manually takes care: order–product traversal must preserve relationships, regex must cope with inconsistent descriptions, and growing status rules and edge cases require testing.
Why it matters: These difficulties explain the appeal of generated code without making correctness automatic. - Notebook trial and tool separation: Describe the exercise of generating code in ChatGPT, running it in a Jupyter notebook, and comparing the result with the example table. Explain why the book initially keeps the companion separate from the IDE despite integrated tools being available.
Why it matters: Running and inspecting generated code supports the coding and debugging intuition the book wants readers to develop.
Key terms: nested JSON, normalize, product_order DataFrame, regular expressions, price_with_tax, AI coding companion, ChatGPT, conversational interface, prompt, boilerplate coding
2.3 Disadvantages of Using a Coding Companion
This section explains why coding companions can give misleading data-engineering answers when they lack project context or run out of context-window space, and how to work around those limits.
- Missing project context: Explain that an LLM responds to information available in the current prompt, not automatically to the full codebase, business rules, naming conventions, or data lineage. Describe why generalized advice may therefore be wrong for a specific environment.
Why it matters: A plausible answer is not necessarily an answer grounded in your system. - Context window limits: Explain that a context window holds only a finite number of tokens, so long schemas, code, and instructions can crowd out earlier information. Describe how losing that information can cause missed constraints or relationships, incomplete reasoning, or fabricated code.
Why it matters: Large data-engineering inputs can make the companion lose the very details it needs to produce correct code. - Grounding and validation strategies: Describe ways to reduce context-related errors: provide relevant schema documentation, DDL, or ERDs; use smaller focused interactions or restate relationships; explore a schema progressively; and validate generated code against known definitions. Explain why the section uses the well-documented, widely available Pagila schema: it provides manageable shared context.
Why it matters: Giving the companion relevant context and checking its output makes AI-assisted work more reliable.
Key terms: current prompt, business logic, naming conventions, data lineage, context window, tokens, schema, hallucinated functions, schema docs, DDL scripts
2.3.1 Introduction to the Pagila Dataset
Introduces the Pagila DVD-rental database as a realistic schema for examining how AI coding companions can misinterpret table relationships.
- Pagila schema and purpose: Explain that Pagila is a fictional DVD-rental database with interconnected tables such as rental, film, inventory, and customer, and that its foreign-key relationships make it useful for testing AI-generated queries involving joins, aggregations, and business logic.
Why it matters: Understanding the schema gives you a basis for spotting when an AI-generated query gets the relationships wrong. - Local Pagila setup files: Describe obtaining the Pagila schema and seed-data files from the book’s GitHub repository and importing them into a local PostgreSQL environment using pgAdmin or another PostgreSQL client.
Why it matters: Loading the files lets you inspect the schema and run the example queries yourself.
Key terms: Pagila, transactional schema, foreign key relationships, ERD, joins, aggregations, schema file, seed data file, GitHub repository, PostgreSQL
2.3.2 Example: Asking a Simple Question
A simple Pagila SQL prompt shows that plausible AI-generated code can depend on unstated assumptions, so its results should be tested and the prompt refined.
- Rental count as popularity: Explain why SQL that joins films to rentals and counts rentals per film may look correct yet fail to answer what the user meant by the most popular film.
Why it matters: A plausible query is only useful if its definition of popularity matches the question you intended to ask. - Testing and refining generated SQL: Describe how to run ChatGPT’s SQL for the Pagila question in pgAdmin, compare the result with expectations, and revise the prompt with a qualification such as recent rentals or by category to see how the output changes.
Why it matters: Running and refining generated SQL reveals interpretations that are easy to miss by reading the code alone.
Key terms: Pagila, most popular film, COUNT(r.rental_id), rentals, ChatGPT, pgAdmin, recent rentals, by category
2.4 Lab
This lab practices prompting a coding companion for SQL and Python, then running and checking its generated code.
2.5 Lab Answers
This lab-answer section explains how to judge AI-generated solutions that differ from the examples.
Where this is used later
- Zero-Shot Prompting (chapter 3) · SQL prompting builds on the prompt-and-response interaction introduced here.
- Understanding the ChatGPT API and Chat Completions Endpoint (chapter 5) · The later API work moves toward embedding LLM interactions in the workflow, in contrast to the initially separate companion.
- Unnesting Complex JSON Objects with AI Companions & Python (chapter 4) · Applies the same idea of flattening nested data with an AI companion.
- (chapter intro) (chapter 3) · Introduces the Pagila dataset.
Concepts explained in this chapter
- data frameA pandas DataFrame is a two-dimensional labeled tabular data structure in the pandas library.
- regular expressionA regular expression is a pattern language for matching, extracting, or replacing text according to specified character and sequence rules.
- computational notebookJupyter is an open-source interactive computing project providing notebook-based tools for writing and executing code with explanatory content and outputs.
- natural language interfaceA natural language interface allows users to interact with software through ordinary human language rather than a specialized command syntax.
- normalized tableA normalized table is a flat tabular representation in which nested records are expanded into rows with their relevant fields as columns.
- prompt engineeringPrompt engineering is the deliberate design and refinement of inputs provided to an AI model to influence the quality, format, or behavior of its outputs.
- SQLA search query is a set of terms or structured input submitted to a search system to retrieve relevant results.
- language modelA large language model is a machine-learning model trained on large text corpora to understand and generate language and related sequences such as code.
- hallucinationAn AI hallucination is a confident but incorrect or misleading output produced by an artificial intelligence model.
- schemaA database schema is the formal organization of a database's tables, fields, relationships, constraints, and related definitions.
- context windowA context window is the finite amount of text or tokens that a language model can process in one interaction.
- edge caseAn edge case is an unusual, extreme, or boundary condition that can expose limitations or failures in a system or process.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
