<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ivan N</title>
    <description>The latest articles on DEV Community by Ivan N (@ivansparq).</description>
    <link>https://dev.to/ivansparq</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1017595%2F147d60a8-0a22-47ab-b769-b9a007e772ef.jpeg</url>
      <title>DEV Community: Ivan N</title>
      <link>https://dev.to/ivansparq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ivansparq"/>
    <language>en</language>
    <item>
      <title>Managing UDFs in dbt</title>
      <dc:creator>Ivan N</dc:creator>
      <pubDate>Fri, 04 Aug 2023 11:53:15 +0000</pubDate>
      <link>https://dev.to/ivansparq/managing-udfs-in-dbt-2h8h</link>
      <guid>https://dev.to/ivansparq/managing-udfs-in-dbt-2h8h</guid>
      <description>&lt;p&gt;TLDR; &lt;br&gt;
I used dbt's &lt;code&gt;macro&lt;/code&gt; and &lt;code&gt;on-run-end&lt;/code&gt; hook to create UDFs in BigQuery.&lt;/p&gt;

&lt;p&gt;I rencetly had a uses case where we were migrating a customer warehouse to dbt. UDFs were widely used and we wanted to preserve them to make the migration easier. &lt;/p&gt;

&lt;p&gt;However dbt does not have any special way of managing UDFs. This mean that you need to write the DDL (data definition language) to&lt;code&gt;CREATE&lt;/code&gt; and&lt;code&gt;DROP&lt;/code&gt; the UDFs yourself 😱&lt;/p&gt;

&lt;p&gt;Here is a solution that I borrowed and adjusted from dbt's &lt;a href="https://discourse.getdbt.com/t/using-dbt-to-manage-user-defined-functions/18"&gt;forum&lt;/a&gt;:&lt;/p&gt;
&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;dbt/macros/udfs/clean_dates.sql&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;macro&lt;/span&gt; &lt;span class="n"&gt;create_clean_dates&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="k"&gt;REPLACE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; 
    &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;schema&lt;/span&gt; &lt;span class="p"&gt;}}.&lt;/span&gt;&lt;span class="n"&gt;clean_dates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;STRING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;SAFE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PARSE_DATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"%Y/%m/%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;REGEXP_REPLACE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="nv"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\-&lt;/span&gt;&lt;span class="nv"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endmacro&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;one file per UDF makes it easier for a developer to quickly find the code when they need to make changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;dbt/macros/create_udfs.sql&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;macro&lt;/span&gt; &lt;span class="n"&gt;create_udfs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;schema&lt;/span&gt; &lt;span class="n"&gt;if&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;create_clean_dates&lt;/span&gt;&lt;span class="p"&gt;()}};&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endmacro&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;I use PyCharm's &lt;code&gt;Edit-&amp;gt; Sort Lines&lt;/code&gt; to keep UDFs sorted alphabetically&lt;/li&gt;
&lt;li&gt;I add the semicolon &lt;code&gt;;&lt;/code&gt; here but you can do that in the macro as well, doesn't matter, as long as you are consistent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;dbt/dbt_project.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;...&lt;/span&gt;
&lt;span class="na"&gt;on-run-start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;create_udfs()&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;These two lines make sure that the UDFs are created before any dbt models are run/built. The only downside is that it adds some time to your dbt build. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Naming conventions
&lt;/h2&gt;

&lt;p&gt;We have implemented the following naming conventions to make support easier:&lt;/p&gt;

&lt;p&gt;For the example in our UDF &lt;code&gt;clean_dates&lt;/code&gt; we do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;File has the same name as the UDF: &lt;code&gt;clean_dates.sql&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;The macro is called &lt;code&gt;create_clean_dates&lt;/code&gt; as it makes it easier to distinguish the UDF from the macro&lt;/li&gt;
&lt;li&gt;All UDFs are created in the same dataset &lt;code&gt;target.schema&lt;/code&gt;, which will default to &lt;code&gt;dbt&lt;/code&gt;. This makes it easier to &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://discourse.getdbt.com/t/using-dbt-to-manage-user-defined-functions/18"&gt;https://discourse.getdbt.com/t/using-dbt-to-manage-user-defined-functions/18&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dbt</category>
      <category>sql</category>
      <category>bigquery</category>
      <category>gcp</category>
    </item>
    <item>
      <title>Batch rename BigQuery tables with SQL</title>
      <dc:creator>Ivan N</dc:creator>
      <pubDate>Thu, 03 Aug 2023 10:13:58 +0000</pubDate>
      <link>https://dev.to/ivansparq/batch-rename-bigquery-tables-with-sql-2dgl</link>
      <guid>https://dev.to/ivansparq/batch-rename-bigquery-tables-with-sql-2dgl</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;; &lt;br&gt;
Recently I had a situation where we had 400+ wildcard tables that were not named correctly. I used a single SQL statement to programatically fetch all the table names and rename them based on a regex. &lt;/p&gt;
&lt;h2&gt;
  
  
  What are wildcard tables and what can you do with them
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Wildcard tables are individual tables that have the same preffix e.g.: &lt;code&gt;sales_20220101&lt;/code&gt;, &lt;code&gt;sales_20220102&lt;/code&gt;, etc.&lt;br&gt;
For more details see the &lt;a href="https://cloud.google.com/bigquery/docs/querying-wildcard-tables"&gt;official documentation.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;BigQuery has a filter function &lt;code&gt;_TABLE_SUFFIX&lt;/code&gt; that allows you to do some clever stuff with wildcard tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
&lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;
  &lt;span class="nv"&gt;`bigquery-public-data.noaa_gsod.gsod19*`&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_TABLE_SUFFIX&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'29'&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="s1"&gt;'35'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will select all the tables &lt;code&gt;1929, 1930 ... 1935&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I personally prefer partitioned tables, but wildcard have their uses. Namely when you load data from a 3rd party and you expect that they might change their schema at some point in the future. &lt;br&gt;
More on that in another article.&lt;/p&gt;
&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Tables were annoyingly named &lt;code&gt;ABC_20220101_0700_BLA&lt;/code&gt; which made using &lt;code&gt;_TABLE_SUFFIX&lt;/code&gt; impossible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Luckily with a simple string subset I can easily get to the correct naming schema &lt;code&gt;ABC_20220101&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However the biggest issue was the volume of tables (over 400), so anything manual was out of the question. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;My first thought was to use the BigQuery Python API, but I decided to challenge myself and use just SQL.&lt;br&gt;
There are several features in BQ SQL that I used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data definition language (DDL) - you know this from &lt;code&gt;CREATE TABLE&lt;/code&gt; or &lt;code&gt;DROP TABLE&lt;/code&gt;, But did you know there is &lt;br&gt;
&lt;a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_table_rename_to_statement"&gt;ALTER TABLE&lt;/a&gt; as well&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Procedural language - Most SQL users will be familiar with &lt;code&gt;DECLARE&lt;/code&gt; and &lt;code&gt;SET&lt;/code&gt; keywords. But BigQuery SQL allows you add complex logic like &lt;code&gt;IF&lt;/code&gt; and &lt;code&gt;WHILE&lt;/code&gt; statements. In this problem I used &lt;a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#for-in"&gt;FOR IN&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt; is a special "dataset" that has several views which give you a lot of metadata about your warehouse (datastes, tables, views, etc.).&lt;br&gt;
Read more about it &lt;a href="https://cloud.google.com/bigquery/docs/information-schema-intro"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;SELECT&lt;/span&gt; 
        &lt;span class="n"&gt;table_id&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;old_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;regexp_extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="nv"&gt;"ABC_&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="nv"&gt;{8}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;new_name&lt;/span&gt;
      &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;my_dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__TABLES__&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;DO&lt;/span&gt;
&lt;span class="k"&gt;EXECUTE&lt;/span&gt; &lt;span class="k"&gt;IMMEDIATE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"ALTER TABLE my_dataset."&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;old_name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nv"&gt;" RENAME TO "&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new_name&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="c1"&gt;-- some query that we loop over&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;DO&lt;/span&gt;
  &lt;span class="c1"&gt;-- run some other query &lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;FOR&lt;/code&gt; loop allows us to itterate over each row of any query
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;table_id&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;old_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;my_dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__TABLES__&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;my_dataset.__TABLES__&lt;/code&gt; wil return a table that has a lot of information about each table in this dataset. 
I only cared about the name of the tables: &lt;code&gt;table_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="n"&gt;regexp_extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="nv"&gt;"ABC_&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="nv"&gt;{8}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;new_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The regular expression only takes the literal string &lt;code&gt;ABC_&lt;/code&gt; and then 8 digits
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXECUTE&lt;/span&gt; &lt;span class="k"&gt;IMMEDIATE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"ALTER TABLE my_dataset."&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;old_name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nv"&gt;" RENAME TO "&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new_name&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ALTER TABLE my_dataset.x RENAME TO y&lt;/code&gt; does exactly what it says on the tin - renames a table. (Don't forget to add the dataset) &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXECUTE IMMEDIATE&lt;/code&gt; allows us to run a string as a query. Think of it as &lt;code&gt;eval&lt;/code&gt; in Python.&lt;/li&gt;
&lt;li&gt;BigQuery SQL syntax will not let us to &lt;code&gt;SELECT ... FROM my_dataset.some_var&lt;/code&gt; because it will treat &lt;code&gt;some_var&lt;/code&gt; as literal rather than replacing it with its value. &lt;/li&gt;
&lt;li&gt;That is why we need to first build a string of the query. And that's what the &lt;code&gt;||&lt;/code&gt; chars are for. They just concatenate strings. &lt;/li&gt;
&lt;li&gt;Here is what the evaluated string looks like:
&lt;code&gt;"ALTER TABLE my_dataset.ABC_123_BLA  RENAME TO ABC_123_BLA "&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final thoughts:
&lt;/h2&gt;

&lt;p&gt;I was getting some weird error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Not found: Dataset my-project:my_dataset was not found in location US at [3:7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out that using the bacticks "`" around my table and dataset names was causing this - so I removed them and it all worked 🤷&lt;/p&gt;

</description>
      <category>bigquery</category>
      <category>gcp</category>
      <category>sql</category>
      <category>regex</category>
    </item>
  </channel>
</rss>
