[ETR #14] Build Your Staging Tables Faster/Safer


Extract. Transform. Read.

A newsletter from Pipeline: Your Data Engineering Resource

Presented by Basejump AI

Use natural language prompts to chat with your database in Basejump’s intuitive interface, or embed it directly in your application. Book your demo here.

Hi past, present or future data professional!

One thing that makes my work day easier is when I’m Google-ing (as all software developers do) a problem and I come across the holy grail of solutions: A one-line implementation.

Like anything, however, a one-liner that is too complex can become a bad thing. Think: Chained Pandas expressions that become unreadable. Or cramming a multi-line query inside of a BigQuery client method.

My favorite one line (at least in recent memory) is a clause used with SQL’s ALTER TABLE statement: RENAME TO. You may find renaming a table as compelling as schema creation. But this simple clause can be especially useful in lieu of a more dangerous phrase: CREATE OR REPLACE.

The RENAME command allows you to rename a table without having to completely recreate its contents–and risk a SQL statement failing and losing some or all of your data.

Specifically, I use RENAME TO when I want to convert a copy table with some change, like an updated schema, to a production table. I do so using these steps:

  • Create/backfill a staging table I’ll ultimately convert to prod
  • Use ALTER TABLE `dataset.production_table` RENAME TO `production_table_original`
  • Use ALTER TABLE `dataset.staging_table` RENAME TO `production_table`
  • Double-check all partitions, clustering specifications and metadata descriptions are identical between the tables

The best part is that this is a true one-liner. No chains–or headaches–involved.

To save you a headache, here are this week’s links:

If you want to read more about this method, I cover the process in more detail here.

Questions? You know where to find me: zach@pipelinetode.com.

Until next time–thanks for ingesting,

-Zach Quinn

Extract. Transform. Read.

Reaching 20k+ readers on Medium and over 3k learners by email, I draw on my 4 years of experience as a Senior Data Engineer to demystify data science, cloud and programming concepts while sharing job hunt strategies so you can land and excel in data-driven roles. Subscribe for 500 words of actionable advice every Thursday.

Read more from Extract. Transform. Read.

Hi fellow data professional! I had a very adult weekend after baby bedtime. I uncorked a bottle of wine, cracked open my laptop and… stayed up late making a dashboard tracking my assets and debts. While I learned some important high-level insights that will help me make financial decisions as I renovate and prepare to move into my home, I realized a basic development truth that could help anyone knee-deep in their own project. If your goal is commit or ship then the simplest implementation...

Hi fellow data professional! Next time you think you’ve tried everything in your job search, remember: I once worked with a guy who got hired at a national broadcast network on the strength of his parody rap. The intern, Jake, didn’t have a network or elite contacts, but he wanted an internship at The Tonight Show, a competitive role with over 10,000 applicants per semester. So, he recreated a shot-for-shot parody of a song previously performed on the show, rewritten with lyrics specifically...

Hi fellow data professional! For the past month I’ve been working on my most ambitious personal project: Purchasing and renovating a house. The first major upgrade? Replacing 70+-year-old windows. And while there’s probably a tech work comparison to gutting a legacy system to build anew, what I want to focus on is the deal I brokered and how you can use similar leverage in your interviews. Because I got $1200 off a house’s worth of windows as a result of market research, due diligence and a...