<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>DailyDatabricks Tips</title>
<link>DailyDatabricks.Tips/</link>
<atom:link href="DailyDatabricks.Tips/index.xml" rel="self" type="application/rss+xml"/>
<description>Explore DailyDatabricks for a treasure trove of resources packed with insightful tips, tricks, and innovative hacks surrounding Databricks. Whether you&#39;re a beginner looking to navigate through Databricks, or a seasoned professional keen on enhancing your data engineering skills, DailyDatabricks is your go-to resource. Discover actionable advice, learn from real-world examples, and stay updated on the latest Databricks features and updates to streamline your data analytics journey</description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Sat, 14 Dec 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>Easy Download Links from Databricks Notebooks using FileStore</title>
  <link>DailyDatabricks.Tips/tips/Utility/DownloadLinksInFileStore.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-important">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p><a href="https://docs.databricks.com/en/archive/legacy/filestore.html#filestore">Filestore is not actively recommended and considered legacy by Databricks</a></p>
</div>
</div>
</div>
<section id="summary" class="level3">
<h3 class="anchored" data-anchor-id="summary">Summary</h3>
<ul>
<li>Databricks FileStore allows you to store files that are accessible via download links</li>
<li>You can programmatically generate download URLs for files in FileStore</li>
<li>While FileStore works, using external cloud storage like Azure Blob Storage or AWS S3 is generally recommended for file sharing</li>
</ul>
</section>
<section id="detail" class="level3">
<h3 class="anchored" data-anchor-id="detail">Detail</h3>
<p>Databricks FileStore provides a convenient way to store files that you want to make available for download. By copying files into the special <code>/FileStore</code> directory in DBFS, you can easily generate URLs that allow users to download those files directly from rendered notebooks.</p>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This code snippet assumes you have already generated a file, like an Excel spreadsheet, and saved it to a temporary location like <code>/tmp</code>. Adjust the code based on your specific use case.</p>
</div>
</div>
</div>
<p>Here’s how you can create a download link for a file using FileStore in a Python notebook:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># File paths</span></span>
<span id="cb1-2">temp_file_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/tmp/output_data.xlsx"</span></span>
<span id="cb1-3">filestore_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/FileStore/output_data.xlsx"</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Code to generate the Excel file and save to temp_file_path </span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># goes here...</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy the file from temp directory to FileStore</span></span>
<span id="cb1-9">dbutils.fs.cp(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"file:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>temp_file_path<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, filestore_path)</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the URL parameters </span></span>
<span id="cb1-12">workspace_url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.conf.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.workspaceUrl"</span>)</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate the download URL</span></span>
<span id="cb1-15">download_url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"https://</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>workspace_url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/files/output_data.xlsx"</span></span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Render the download link using displayHTML</span></span>
<span id="cb1-18">displayHTML(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"&lt;a href='</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>download_url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'&gt;Click here to download the Excel file&lt;/a&gt;"</span>)</span></code></pre></div></div>
<p>The key steps are:</p>
<ol type="1">
<li>Copy the file into FileStore using <code>dbutils.fs.cp</code></li>
<li>Retrieve the workspace URL from the Spark context</li>
<li>Construct the download URL</li>
<li>Render the download link using <code>displayHTML</code></li>
</ol>
<p>When a user clicks the rendered download link, it will prompt their browser to download the file directly from FileStore.</p>
</section>
<section id="reference" class="level2">
<h2 class="anchored" data-anchor-id="reference">Reference</h2>
<p>For more details on the FileStore and other Databricks file system concepts, check out:</p>
<ul>
<li><p><a href="https://docs.databricks.com/data/databricks-file-system.html">Databricks File System Overview</a></p></li>
<li><p><a href="https://docs.databricks.com/data/filestore.html">The Databricks FileStore</a></p></li>
<li><p><a href="https://stackoverflow.com/questions/73682957/how-to-programmatically-retrieve-the-workspace-url-and-clusterowneruserid">Download File Store StackOverflow</a></p></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Databricks</category>
  <category>Python</category>
  <category>Tips</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Utility/DownloadLinksInFileStore.html</guid>
  <pubDate>Sat, 14 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Unlocking the Power of Databricks Magic Commands</title>
  <link>DailyDatabricks.Tips/tips/Notebook/moremagics.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note" title="">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This tip is part of the <a href="../../#category=Decembricks">Decembricks 2024 series</a> to teach a new tip everyday of december.</p>
</div>
</div>
</div>
<section id="unlocking-the-power-of-databricks-magic-commands" class="level1">
<h1>Unlocking the Power of Databricks Magic Commands</h1>
<p>Databricks notebooks offer a powerful and flexible environment for data exploration, analysis, and collaboration. One of the key features that makes Databricks notebooks so productivity-enhancing is the availability of built-in IPython magic commands. These magic commands allow you to interact with the notebook environment, control code execution, and extend functionality.</p>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Magic commands are special commands in Databricks notebooks that extend functionality</li>
<li>Line magics start with <code>%</code> and operate on a single line</li>
<li>Cell magics start with <code>%%</code> and operate on the entire cell</li>
<li>You can list all available magics with <code>%lsmagic</code></li>
<li>Magics like <code>%time</code>, <code>%%timeit</code> are useful for timing code execution</li>
<li>Magics like <code>%logstart</code>, <code>%logstop</code>, <code>%logstate</code> help with logging notebook activities</li>
<li>Magics like <code>%sh</code> and <code>%perl</code> allow running shell scripts and code in other languages</li>
</ul>
</section>
<section id="exploring-available-magics" class="level2">
<h2 class="anchored" data-anchor-id="exploring-available-magics">Exploring Available Magics</h2>
<p>To see a list of all available line and cell magic commands, you can use the <code>%lsmagic</code> command:</p>
<pre><code>%lsmagic</code></pre>
<p>This will output an extensive list of the built-in magic commands, separated into line magics and cell magics. Take some time to scan through the list and make note of any that seem particularly useful for your workflow.</p>
<p>Some of the most commonly used magic commands include:</p>
<ul>
<li><code>%matplotlib</code> - enable Matplotlib integration for inline plotting</li>
<li><code>%time</code> - time execution of a single statement</li>
<li><code>%%timeit</code> - time execution of the whole cell, averaging over multiple runs</li>
<li><code>%%capture</code> - capture the stdout/stderr of the cell</li>
<li><code>%%html</code> - render the cell as HTML</li>
<li><code>%%sql</code> - execute contents of cell as SQL in a temporary context</li>
</ul>
<p><strong>::: {.callout-note title=“Familiar Magics” appearance=“simple”}</strong> You’re likely already familiar with magics for common languages like <code>%python</code>, <code>%sql</code>, <code>%r</code>, and <code>%scala</code>. In this post, we’ll focus on some lesser-known but equally powerful magics. :::</p>
</section>
<section id="timing-code-execution" class="level2">
<h2 class="anchored" data-anchor-id="timing-code-execution">Timing Code Execution</h2>
<p>One of the most practical applications of magic commands is timing code execution to identify performance bottlenecks. The <code>%time</code> and <code>%%timeit</code> magics are your go-to tools for this.</p>
<p><strong>::: {.callout-note title=“Line vs Cell Magics” appearance=“simple”}</strong> Remember, line magics like <code>%time</code> operate on a single line, while cell magics like <code>%%timeit</code> operate on the entire contents of the cell. :::</p>
<section id="using-time" class="level3">
<h3 class="anchored" data-anchor-id="using-time">Using %time</h3>
<p>Use <code>%time</code> to time a single line of code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>time df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM large_table"</span>)</span></code></pre></div></div>
<pre><code>CPU times: user 456 ms, sys: 123 ms, total: 579 ms
Wall time: 5.67 s</code></pre>
</section>
<section id="using-timeit" class="level3">
<h3 class="anchored" data-anchor-id="using-timeit">Using %%timeit</h3>
<p>Use <code>%%timeit</code> to time execution of the entire cell, averaging over multiple runs for more robust timing:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span>timeit</span>
<span id="cb4-2">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM large_table"</span>)</span>
<span id="cb4-3">df.count()  </span></code></pre></div></div>
<pre><code>2.31 s ± 135 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)</code></pre>
<p>By strategically using these timing magics, you can pinpoint which parts of your code are taking the longest and optimize accordingly. For example, you might discover a particular Spark SQL query that is taking much longer than expected, and then you can focus your efforts on improving that query.</p>
</section>
</section>
<section id="logging-notebook-activities" class="level2">
<h2 class="anchored" data-anchor-id="logging-notebook-activities">Logging Notebook Activities</h2>
<p>Databricks magic commands also provide handy tools for logging notebook activities. This can be extremely useful for tracking progress, debugging, and monitoring long-running jobs. These logs are output to local file in your notebooks local folder on your databricks workspace which by default is named <code>ipython_log.py</code></p>
<p>Key logging magics include:</p>
<ul>
<li><code>%logstart</code> - start logging cell activities</li>
<li><code>%logstop</code> - stop logging cell activities</li>
<li><code>%logstate</code> - display current logging state</li>
</ul>
<section id="using-logstart-and-logstop" class="level3">
<h3 class="anchored" data-anchor-id="using-logstart-and-logstop">Using %logstart and %logstop</h3>
<p>To start logging, simply use <code>%logstart</code> at the beginning of a cell:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>logstart</span>
<span id="cb6-2">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM large_table"</span>)</span>
<span id="cb6-3">df.count()</span></code></pre></div></div>
<p>This will log all stdout/stderr output as well as execution metadata to a log file.</p>
<p>To stop logging, use <code>%%logstop</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>logstop</span></code></pre></div></div>
</section>
<section id="checking-log-state" class="level3">
<h3 class="anchored" data-anchor-id="checking-log-state">Checking Log State</h3>
<p>To check the current state of logging, use <code>%logstate</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>logstate</span></code></pre></div></div>
<pre><code>Logging is currently ON
Log file at: &lt;log_path&gt;.log</code></pre>
<p>The Internals of the file look like:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb10-1"># IPython log file</span>
<span id="cb10-2">get_ipython().run_line_magic('logstart', '')</span>
<span id="cb10-3">df = spark.sql("SELECT * FROM default.my_test_delta_table")</span>
<span id="cb10-4">df.count()</span>
<span id="cb10-5">get_ipython().run_line_magic('logstate', '')</span>
<span id="cb10-6">get_ipython().run_cell_magic('logstop', '', '')</span></code></pre></div></div>
<p>By utilizing these logging magics, you can keep detailed records of your notebook activities. You can expand this to implement your own custom version of the logger magic.</p>
</section>
</section>
<section id="running-shell-commands-and-other-languages" class="level2">
<h2 class="anchored" data-anchor-id="running-shell-commands-and-other-languages">Running Shell Commands and Other Languages</h2>
<p>Databricks magic commands also allow you to execute shell commands and code in languages beyond the notebook’s default. This can be extremely powerful for tasks like file system operations, system monitoring, and leveraging existing scripts.</p>
<p>Some useful magics for this include:</p>
<ul>
<li><code>%sh</code> - execute shell commands</li>
<li><code>%%perl</code> - execute Perl code</li>
</ul>
<p>For example, you can use <code>%sh</code> to run a simple bash command:</p>
<pre><code>%sh 
echo "Hello from bash!"</code></pre>
<p>Or use <code>%perl</code> to execute a Perl one-liner:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb12-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">%perl</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Hello from Perl!</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>;</span></code></pre></div></div>
<p>These language agnostic magics open up a world of possibilities for integrating Databricks notebooks into diverse workflows or migrating old code in a grandual manner.</p>
</section>
<section id="exploring-further" class="level2">
<h2 class="anchored" data-anchor-id="exploring-further">Exploring Further</h2>
<p>We’ve only scratched the surface of what’s possible with Databricks magic commands. To dive deeper, check out these resources:</p>
<ul>
<li><p><a href="../../tips/Notebook/Customcellmagics.html">Custom Magics Tip</a></p></li>
<li><p><a href="https://docs.databricks.com/notebooks/notebooks-use.html#mix-languages">Databricks Magic Commands Documentation</a></p></li>
<li><p><a href="https://ipython.readthedocs.io/en/stable/interactive/magics.html">Jupyter Notebook Documentation on Magic Commands</a></p></li>
</ul>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Databricks</category>
  <category>Python</category>
  <category>Tips</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/moremagics.html</guid>
  <pubDate>Wed, 11 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Exploring Databricks Metadata with Spark Catalog and Unity Catalog</title>
  <link>DailyDatabricks.Tips/tips/SparkCatalog/MetadataDriven.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note" title="">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This tip is part of the <a href="../../#category=Decembricks">Decembricks 2024 series</a> to teach a new tip everyday of december.</p>
</div>
</div>
</div>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Use <code>spark.catalog.list</code> methods to programmatically explore catalogs, databases, tables and columns</li>
<li>Recursively traverse the metadata hierarchy to build a catalog structure dictionary</li>
<li>Compare catalog structures to identify differences between environments</li>
</ul>
</section>
<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p>As your lakehouse platform grows to encompass more use cases, teams, and workloads, keeping track of all the metadata - catalogs, databases, tables, and columns - can become increasingly challenging. Fortunately, the Spark Catalog APIs provide programmatic methods to explore and analyze this metadata.</p>
<p>In this post, we’ll walk through an example of how to use these APIs in PySpark to:</p>
<ol type="1">
<li>Recursively list all databases and tables within a catalog</li>
<li>Build a dictionary representing the hierarchical catalog structure</li>
<li>Compare catalog structures across environments to identify metadata differences</li>
</ol>
</section>
<section id="exploring-metadata-programmatically" class="level2">
<h2 class="anchored" data-anchor-id="exploring-metadata-programmatically">Exploring Metadata Programmatically</h2>
<p>The key to programmatic metadata exploration in Databricks is the family of <code>spark.catalog.list</code> methods:</p>
<ul>
<li><code>spark.catalog.listCatalogs()</code> - List all available catalogs</li>
<li><code>spark.catalog.listDatabases()</code> - List databases in the current catalog</li>
<li><code>spark.catalog.listTables()</code> - List tables in a specified database</li>
<li><code>spark.catalog.listColumns()</code> - List columns in a specified table</li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>These same methods are also available on the <a href="https://docs.databricks.com/en/sql/language-manual/sql-ref-information-schema.html">unity catalog information schema tables</a> if you prefer to use sql.</p>
</div>
</div>
<p>Let’s see a quick example of using these methods to explore all databases and tables in each catalog:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> catalog <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spark.catalog.listCatalogs():</span>
<span id="cb1-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Catalog is "</span>,catalog.name)</span>
<span id="cb1-3">    spark.catalog.setCurrentCatalog(catalog.name)</span>
<span id="cb1-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> database <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spark.catalog.listDatabases():</span>
<span id="cb1-5">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Database is:"</span>, database.name)</span>
<span id="cb1-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spark.catalog.listTables(database.name):</span>
<span id="cb1-7">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Table is:"</span>, table.name)</span></code></pre></div></div>
<p>This will output something like:</p>
<pre><code>Catalog is hive_metastore  
Database is: default
Table is: customers
Table is: orders
Database is: example
Table is: flights
Catalog is unity_dev_environment
Database is: dlt_pipeline_metrics
Table is: pipeline_status</code></pre>
</section>
<section id="building-a-catalog-structure" class="level2">
<h2 class="anchored" data-anchor-id="building-a-catalog-structure">Building a Catalog Structure</h2>
<p>While the above snippet is useful for exploration, let’s see how we can build a more programmatic representation of our catalog structure that will enable richer analysis and comparison.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_catalog_structure():</span>
<span id="cb3-2">    catalog_structure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb3-3">    all_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-4">   </span>
<span id="cb3-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> catalog <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spark.catalog.listCatalogs():</span>
<span id="cb3-6">        catalog_structure[catalog.name] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb3-7">        spark.catalog.setCurrentCatalog(catalog.name)</span>
<span id="cb3-8">       </span>
<span id="cb3-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> database <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> spark.catalog.listDatabases():</span>
<span id="cb3-10">            catalog_structure[catalog.name][database.name] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-11">           </span>
<span id="cb3-12">            tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.catalog.listTables(database.name)</span>
<span id="cb3-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tables:</span>
<span id="cb3-14">                catalog_structure[catalog.name][database.name].append(table.name)</span>
<span id="cb3-15">                all_paths.append(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>catalog<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>database<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>table<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-16"></span>
<span id="cb3-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> catalog_structure, all_paths</span></code></pre></div></div>
<p>This recursively traverses catalogs and databases, building a dictionary <code>catalog_structure</code> where: - Keys are catalog names - Values are dictionaries where: - Keys are database names - Values are lists of tables in that database</p>
<p>It also collects a list <code>all_paths</code> containing the fully-qualified paths of each table in “<code>catalog.database.table</code>” notation.</p>
<div class="callout callout-style-simple callout-caution callout-titled" title="Error Handling">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Error Handling
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The actual implementation wraps each <code>spark.catalog</code> call in a try/except block to gracefully handle any missing privileges or other exceptions such as references to tables where the data no longer exists at that location.</p>
</div>
</div>
</div>
</section>
<section id="comparing-catalog-structures" class="level2">
<h2 class="anchored" data-anchor-id="comparing-catalog-structures">Comparing Catalog Structures</h2>
<p>Now that we can capture a catalog structure, let’s put it to use by comparing structures across environments to identify metadata diffs.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compute_catalog_diffs(catalog_structure):</span>
<span id="cb4-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get list of all catalogs</span></span>
<span id="cb4-3">    catalogs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(catalog_structure.keys())</span>
<span id="cb4-4">    diffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb4-5">   </span>
<span id="cb4-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare each catalog with every other catalog</span></span>
<span id="cb4-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(catalogs)):</span>
<span id="cb4-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(catalogs)):</span>
<span id="cb4-9">            cat1, cat2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> catalogs[i], catalogs[j]</span>
<span id="cb4-10">           </span>
<span id="cb4-11">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get all paths for catalog 1</span></span>
<span id="cb4-12">            cat1_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb4-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> db <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> catalog_structure[cat1]:</span>
<span id="cb4-14">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> catalog_structure[cat1][db]:</span>
<span id="cb4-15">                    cat1_paths.add(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>db<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>table<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-16">           </span>
<span id="cb4-17">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get all paths for catalog 2</span></span>
<span id="cb4-18">            cat2_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb4-19">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> db <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> catalog_structure[cat2]:</span>
<span id="cb4-20">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> catalog_structure[cat2][db]:</span>
<span id="cb4-21">                    cat2_paths.add(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>db<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>table<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-22">           </span>
<span id="cb4-23">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute differences</span></span>
<span id="cb4-24">            diffs[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cat1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> vs </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cat2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-25">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'onlyin'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> cat1: cat1_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cat2_paths,</span>
<span id="cb4-26">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'onlyin'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> cat2: cat2_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cat1_paths,</span>
<span id="cb4-27">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'common'</span>: cat1_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> cat2_paths</span>
<span id="cb4-28">            }</span>
<span id="cb4-29">   </span>
<span id="cb4-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> diffs</span></code></pre></div></div>
<p>This compares every unique pair of catalogs, building path sets for each, and computing 3 diffs: 1. <code>only_in_X</code> - paths only in catalog X 2. <code>only_in_Y</code> - paths only in catalog Y 3. <code>common</code> - paths in both</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>For large numbers of catalogs, you may want to selectively compare certain pairs rather than combinatorially comparing all pairs.</p>
</div>
</div>
<section id="usage" class="level4">
<h4 class="anchored" data-anchor-id="usage">Usage</h4>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">structure, all_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_catalog_structure()</span>
<span id="cb5-2">differences <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_catalog_diffs(structure) </span></code></pre></div></div>
</section>
</section>
<section id="resources" class="level2">
<h2 class="anchored" data-anchor-id="resources">Resources</h2>
<ul>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.Catalog.listCatalogs.html">PySpark listCatalogs()</a></li>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.Catalog.listDatabases.html">PySpark listDatabases()</a><br>
</li>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.Catalog.listTables.html">PySpark listTables()</a></li>
</ul>
<p>```</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Databricks</category>
  <category>PySpark</category>
  <category>Unity Catalog</category>
  <category>metadata</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/SparkCatalog/MetadataDriven.html</guid>
  <pubDate>Mon, 09 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Custom IPython Magics in Databricks</title>
  <link>DailyDatabricks.Tips/tips/Notebook/Customcellmagics.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note" title="">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This tip is part of the <a href="../../#category=Decembricks">Decembricks 2024 series</a> to teach a new tip everyday of december.</p>
</div>
</div>
</div>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Learn how to create custom IPython magics % to streamline common Databricks notebook operations</li>
<li>Implement practical examples for cluster information, SQL execution plans, and configuration management</li>
</ul>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>If you’re working with Databricks notebooks regularly, you’ve probably used magic commands like <code>%sql</code> or <code>%md</code>. But did you know you can create your own custom magics to automate repetitive tasks and enhance your notebook workflow? In this tip, we’ll explore how to create custom magics specifically designed for Databricks environments.</p>
</section>
<section id="understanding-ipython-magics" class="level2">
<h2 class="anchored" data-anchor-id="understanding-ipython-magics">Understanding IPython Magics</h2>
<p>IPython magics come in two flavors: - Line magics (prefixed with <code>%</code>): Execute on a single line of input - Cell magics (prefixed with <code>%%</code>): Execute on multiple lines of cell content</p>
<div class="callout callout-style-simple callout-tip callout-titled" title="Built In Magics Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Built In Magics Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>While Custom Magics are incredibly useful to build. Databricks provides many built-in magics which you can list using <code>%lsmagic</code>.</p>
</div>
</div>
<section id="defining-custom-magics-in-code" class="level3">
<h3 class="anchored" data-anchor-id="defining-custom-magics-in-code">Defining Custom Magics in Code</h3>
<p>You will need to always import the appropriate functions from IPython.</p>
<p>Let’s look at how to define each type of magic and their usage patterns:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.core.magic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (register_line_magic,register_cell_magic,register_line_cell_magic)</span></code></pre></div></div>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Line Magic</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Cell Magic</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Line &amp; Cell Magic</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_magic</span></span>
<span id="cb2-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> my_line_magic(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, line):</span>
<span id="cb2-3">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Simple line magic that processes input after the command."""</span></span>
<span id="cb2-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"You passed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>line<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
<p>Example Usage</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">%my_line_magic</span> hello world</span>
<span id="cb3-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output: "You passed: hello world"</span></span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_cell_magic</span></span>
<span id="cb4-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> my_cell_magic(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, line, cell):</span>
<span id="cb4-3">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Cell magic that processes both command line and cell content."""</span></span>
<span id="cb4-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Command line: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>line<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Cell contents:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cell<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
<p>Example Usage</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">%%my_cell_magic</span> optional command line args</span>
<span id="cb5-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">This</span> is the</span>
<span id="cb5-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cell</span> content</span>
<span id="cb5-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">over</span> multiple lines</span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output: </span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Command line: optional command line args</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cell contents:</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is the</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cell content</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># over multiple lines</span></span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>When you want want to be able to use it both ways</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_cell_magic</span></span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> my_flexible_magic(line, cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb6-3">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Magic that works in both line and cell modes."""</span></span>
<span id="cb6-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> cell <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb6-5">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Line mode</span></span>
<span id="cb6-6">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Line mode: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>line<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb6-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb6-8">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cell mode</span></span>
<span id="cb6-9">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Line: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>line<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Cell:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cell<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
<p>Example line magic usage:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">%my_flexible_magic</span> some_args</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output: "Line mode: some_args"</span></span></code></pre></div></div>
<p>Example cell magic usage</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">%%my_flexible_magic</span> command line</span>
<span id="cb8-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cell</span></span>
<span id="cb8-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">content</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output:</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Line: command line, Cell:</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cell</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># content</span></span></code></pre></div></div>
</div>
</div>
</div>
</section>
</section>
<section id="custom-magic-examples-for-databricks" class="level2">
<h2 class="anchored" data-anchor-id="custom-magic-examples-for-databricks">Custom Magic Examples for Databricks</h2>
<p>Let’s explore some practical custom magics that can enhance your Databricks workflow:</p>
<section id="cluster-information-magic" class="level3">
<h3 class="anchored" data-anchor-id="cluster-information-magic">1. Cluster Information Magic</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.core.magic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> register_line_magic</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_magic</span></span>
<span id="cb9-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> spark_info(line):</span>
<span id="cb9-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Print Spark version and environment info."""</span></span>
<span id="cb9-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Spark Version: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>spark<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>version<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-7">    </span>
<span id="cb9-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If on Databricks, often `dbutils` is available for additional info</span></span>
<span id="cb9-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dbutils'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>():</span>
<span id="cb9-10">        cluster_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils.notebook.entry_point.getDbutils().notebook().getContext().tags().get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clusterId"</span>).get()</span>
<span id="cb9-11">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Running on cluster: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cluster_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-13">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dbutils not found. Cannot retrieve cluster info."</span>)</span></code></pre></div></div>
<div class="callout callout-style-simple callout-note callout-titled" title="Usage Example">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Usage Example
</div>
</div>
<div class="callout-body-container callout-body">
<p>Simply run <code>%spark_info</code> in your notebook to quickly check your environment details:</p>
<pre><code>Spark Version: 3.5.0
Running on cluster: 097-221359-d1i6gxa1</code></pre>
</div>
</div>
</section>
<section id="table-explorer-magic" class="level3">
<h3 class="anchored" data-anchor-id="table-explorer-magic">2. Table Explorer Magic</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_magic</span></span>
<span id="cb11-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> show_tables(line):</span>
<span id="cb11-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""List tables in the given database: %show_tables dbname"""</span></span>
<span id="cb11-4">    dbname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> line.strip() <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default"</span></span>
<span id="cb11-5">    tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.catalog.listTables(dbname)</span>
<span id="cb11-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tables:</span>
<span id="cb11-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>tableType<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</section>
<section id="sql-execution-plan-magic" class="level3">
<h3 class="anchored" data-anchor-id="sql-execution-plan-magic">3. SQL Execution Plan Magic</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_magic</span></span>
<span id="cb12-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> explain_sql(line):</span>
<span id="cb12-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Explain the Spark execution plan for a given SQL query.</span></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Usage: %explain_sql SELECT * FROM my_table</span></span>
<span id="cb12-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb12-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_cell_magic</span></span>
<span id="cb12-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> explain_sql(line, cell):</span>
<span id="cb12-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb12-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Explain the Spark execution plan for a given SQL query.</span></span>
<span id="cb12-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Usage: %explain_sql SELECT * FROM my_table</span></span>
<span id="cb12-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb12-13">    query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cell.strip()</span>
<span id="cb12-14">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.sql(query)</span>
<span id="cb12-15">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df._jdf.queryExecution().simpleString())</span></code></pre></div></div>
</section>
</section>
<section id="notebook-context-magic" class="level2">
<h2 class="anchored" data-anchor-id="notebook-context-magic">4. Notebook Context Magic</h2>
<p>Here’s a more advanced example that demonstrates how to access and utilize the Databricks <a href="../../tips/Notebook/context.html">notebook context</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_magic</span></span>
<span id="cb13-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> notebook_context(line):</span>
<span id="cb13-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Display current notebook context information"""</span></span>
<span id="cb13-4">    context <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils.notebook.entry_point.getDbutils().notebook().getContext()</span>
<span id="cb13-5">    </span>
<span id="cb13-6">    info <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb13-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"notebook_path"</span>: context.notebookPath().get(),</span>
<span id="cb13-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster_id"</span>: context.clusterId().get(),</span>
<span id="cb13-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workspace_id"</span>: context.workspaceId().get()</span>
<span id="cb13-10">    }</span>
<span id="cb13-11">    </span>
<span id="cb13-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> info</span></code></pre></div></div>
<div class="callout callout-style-simple callout-caution callout-titled" title="Use it to enable code that executes per cell">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Use it to enable code that executes per cell
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Utilise our recent tip on <a href="../../tips/Notebook/IpythonEvents.html">IPython Events</a> to enable hidden Aspect Oriented Programming code i.e logging of every cell when you need to debug. The below shows how to debug to console but you can change it to your prefered logging tools.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.core <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> events <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ip_events</span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.core.magic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (register_line_magic,register_cell_magic,register_line_cell_magic)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> logging_pre(info):</span>
<span id="cb14-5"></span>
<span id="cb14-6">&nbsp; &nbsp; <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb14-7"></span>
<span id="cb14-8">&nbsp; &nbsp; &nbsp; &nbsp; <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Debug Logging to Notebook"</span> )</span>
<span id="cb14-9"></span>
<span id="cb14-10"> &nbsp; &nbsp; &nbsp; &nbsp;print(info.__dict__)</span>
<span id="cb14-11"></span>
<span id="cb14-12">&nbsp; &nbsp; <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb14-13"></span>
<span id="cb14-14">&nbsp; &nbsp; &nbsp; &nbsp; info.raw_cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb14-15"></span>
<span id="cb14-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> logging_post(info):</span>
<span id="cb14-17"></span>
<span id="cb14-18">&nbsp; &nbsp; <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb14-19"></span>
<span id="cb14-20">&nbsp; &nbsp; &nbsp; &nbsp; <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Debug Logging to Notebook cell succesfully executed:"</span>, info.raw_cell)</span>
<span id="cb14-21"></span>
<span id="cb14-22"> &nbsp; &nbsp; &nbsp; &nbsp;print(info.__dict__)</span>
<span id="cb14-23"></span>
<span id="cb14-24">&nbsp; &nbsp; <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb14-25"></span>
<span id="cb14-26">&nbsp; &nbsp; &nbsp; &nbsp; info.raw_cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb14-27"></span>
<span id="cb14-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@register_line_cell_magic</span></span>
<span id="cb14-29"></span>
<span id="cb14-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> enable_debug_logging(line):</span>
<span id="cb14-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Line could include logging level etc</span></span>
<span id="cb14-32">&nbsp; &nbsp; ipython <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython()</span>
<span id="cb14-33"></span>
<span id="cb14-34">&nbsp; &nbsp; ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pre_run_cell'</span>, logging_pre)</span>
<span id="cb14-35"></span>
<span id="cb14-36">&nbsp; &nbsp; ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'post_run_cell'</span>, logging_post)</span></code></pre></div></div>
<p>Usage:</p>
<p><code>%enable_debug_logging verbose</code></p>
</div>
</div>
</div>
</section>
<section id="best-practices-for-custom-magics" class="level2">
<h2 class="anchored" data-anchor-id="best-practices-for-custom-magics">Best Practices for Custom Magics</h2>
<ol type="1">
<li><strong>Documentation</strong>: Always include detailed docstrings explaining usage and parameters</li>
<li><strong>Error Handling</strong>: Implement robust error checking for different environments</li>
<li><strong>Performance</strong>: Keep magics focused and lightweight to maintain notebook responsiveness</li>
<li><strong>Reusability</strong>: Place commonly used magics in a shared module that can be imported</li>
</ol>
</section>
<section id="loading-custom-magics" class="level2">
<h2 class="anchored" data-anchor-id="loading-custom-magics">Loading Custom Magics</h2>
<p>To use your custom magics, create a Python module (e.g., <code>custom_magics.py</code>) and load it in your notebook:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>load_ext custom_magics</span></code></pre></div></div>
<p>You can also create classes of magics, see the <a href="https://ipython.readthedocs.io/en/stable/config/custommagics.html#defining-custom-magics">documentation</a>.</p>
<p>You can also register them directly in code</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython()</span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#e.g. ip.register_magics(&lt;magic_function&gt;)</span></span>
<span id="cb16-3">ip.register_magics(my_flexible_magic)</span></code></pre></div></div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Custom IPython magics can streamline your Databricks workflow by automating common tasks and providing quick access to important information. Start with these examples and build your own magics tailored to your specific needs.</p>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://ipython.readthedocs.io/en/stable/config/custommagics.html">Official IPython Magic Documentation</a></li>
<li><a href="content.qmd">Databricks Notebook Content</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Databricks</category>
  <category>Python</category>
  <category>Tips</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/Customcellmagics.html</guid>
  <pubDate>Sun, 08 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>IPython Events</title>
  <link>DailyDatabricks.Tips/tips/Notebook/IpythonEvents.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Learn how to leverage IPython events in Databricks to execute code before or after every cell in a notebook is executed</li>
<li>Implement Aspect-Oriented Programming (AOP) patterns to add cross-cutting concerns like logging and DataFrame validation</li>
<li>Demonstrate practical examples for automatic DataFrame counting and execution logging</li>
</ul>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Databricks notebooks are powerful, but sometimes you need more control over execution flow and logging. Enter IPython events - a mechanism that allows you to hook into the notebook execution lifecycle.</p>
</section>
<section id="understanding-ipython-events" class="level2">
<h2 class="anchored" data-anchor-id="understanding-ipython-events">Understanding IPython Events</h2>
<p>IPython provides several event hooks (code that is called when a specfic activity happens e.g.&nbsp;someone runs a notebook cell in databricks) that allow you to execute code before and after cell execution. The main events we’ll focus on are:</p>
<ul>
<li><code>pre_run_cell</code>: Fires before a cell execution</li>
<li><code>post_run_cell</code>: Fires after a cell execution</li>
<li><code>pre_execute</code>: Fires before any code execution (including non-interactive)</li>
<li><code>post_execute</code>: Fires after any code execution</li>
</ul>
<div class="callout callout-style-simple callout-important callout-titled" title="Cross-Language Support">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Cross-Language Support
</div>
</div>
<div class="callout-body-container callout-body">
<p>A powerful feature of IPython events in Databricks is that once registered in Python, they will execute for cells in other languages too! This means for example you can:</p>
<ul>
<li>Set up logging in Python and capture execution information from SQL cells</li>
<li>Monitor DataFrame operations across PySpark and Scala notebooks</li>
<li>Track performance metrics for R, Python, and SQL code uniformly</li>
</ul>
</div>
</div>
</section>
<section id="basic-implementation" class="level2">
<h2 class="anchored" data-anchor-id="basic-implementation">Basic Implementation</h2>
<p>Here’s the basic code to call some in your Databricks notebook everytime before a cell runs and after its ran:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">ipython <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython()</span>
<span id="cb1-2">ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pre_run_cell'</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Some Code <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> Function You Want to run each time this event <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> called <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>)</span>
<span id="cb1-3">ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'post_run_cell'</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Some Code <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> Function You Want to run each time this event <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> called <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>)</span></code></pre></div></div>
<p>This prints hello from your favourite tips site everytime a cell is queued to be run.</p>
<p>Result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">ipython <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython()</span>
<span id="cb2-2">ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pre_run_cell'</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>()<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello From DailyDatabricks"</span>))</span></code></pre></div></div>
<p><img src="DailyDatabricks.Tips/tips/Notebook/Assets/ipythonEvents_example.jpg" class="img-fluid"></p>
<p>This means you can start to build up generic code that runs every time a specfic event is triggered. Often this can lead to useful AOP patterns</p>
</section>
<section id="benefits-and-applications" class="level2">
<h2 class="anchored" data-anchor-id="benefits-and-applications">Benefits and Applications</h2>
<ul>
<li><strong>Automated Quality Checks</strong>: Validate DataFrame transformations automatically</li>
<li><strong>Enhanced Debugging</strong>: Track execution flow and catch issues early</li>
<li><strong>Audit Trail</strong>: Maintain comprehensive logs of notebook execution</li>
<li><strong>Performance Optimization</strong>: Monitor execution time and resource usage</li>
<li><strong>Cross-Cutting Concerns</strong>: Implement security checks, logging, and monitoring without cluttering business logic</li>
</ul>
<div class="callout callout-style-simple callout-note callout-titled" title="What is Aspect-Oriented Programming (AOP)?">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>What is Aspect-Oriented Programming (AOP)?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Aspect-Oriented Programming is a programming paradigm that allows you to add behavioral modifications to existing code without changing the code itself. This often means any cross cutting concerns that you want todo everywhere without having to implement that code everywhere. Common use cases include:</p>
<ul>
<li>Logging</li>
<li>Performance monitoring</li>
<li>Security checks</li>
<li>Transaction management</li>
</ul>
<p>In our examples, we’re using IPython events to implement AOP patterns by injecting logging and validation behavior around notebook cell execution.</p>
</div>
</div>
</section>
<section id="logging-implementation" class="level2">
<h2 class="anchored" data-anchor-id="logging-implementation">Logging Implementation</h2>
<p>Here’s how to implement execution logging:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> logging_pre(info):</span>
<span id="cb3-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb3-3">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Debug Logging to Console or Your Preferened Logging tool:"</span>)</span>
<span id="cb3-4">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># You can access cell content via info.raw_cell</span></span>
<span id="cb3-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb3-6">        info.raw_cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> logging_post(info):</span>
<span id="cb3-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb3-10">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Debug Logging to Console or Your Preferened Logging tool. Cell successfully executed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>raw_cell<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb3-12">        info.raw_cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span></code></pre></div></div>
<div class="callout callout-style-simple callout-caution callout-titled" title="Available Context Information">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Available Context Information
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>info</code> object provides rich context about cell execution: - <code>raw_cell</code>: The cell’s source code - <code>store_history</code>: Whether this execution is stored in history - <code>silent</code>: If True, suppress all output - <code>cell_id</code>: Unique identifier for the cell - <code>shell_futures</code>: Future statements in the cell</p>
</div>
</div>
</section>
<section id="advanced-use-case-dataframe-validation" class="level2">
<h2 class="anchored" data-anchor-id="advanced-use-case-dataframe-validation">Advanced Use Case: DataFrame Validation</h2>
<div class="callout callout-style-simple callout-tip callout-titled" title="Practical Example">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Practical Example
</div>
</div>
<div class="callout-body-container callout-body">
<p>One powerful application is automatically tracking DataFrame counts before and after transformations:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> inspect_spark_dataframes(code_namespace):</span>
<span id="cb4-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Inspects a given namespace for Spark DataFrames and prints their counts.</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb4-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, obj <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> code_namespace.items():</span>
<span id="cb4-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> name.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(obj, DataFrame):</span>
<span id="cb4-7">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb4-8">                count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> obj.count()</span>
<span id="cb4-9">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The dataframe called '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">' has the following count: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-10">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb4-11">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error counting DataFrame '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">': </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(e)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pre_counts(info):</span>
<span id="cb4-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Validating pre transformation Dataframe counts"</span>)</span>
<span id="cb4-15">    inspect_spark_dataframes(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>())</span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> post_counts(info):</span>
<span id="cb4-18">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Validating post transformation Dataframe counts"</span>)</span>
<span id="cb4-19">    inspect_spark_dataframes(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>())</span>
<span id="cb4-20"></span>
<span id="cb4-21">ipython <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython()</span>
<span id="cb4-22">ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pre_run_cell'</span>, pre_counts)</span>
<span id="cb4-23">ipython.events.register(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'post_run_cell'</span>, post_counts)</span></code></pre></div></div>
<p>This code automatically tracks DataFrame row counts before and after cell execution, helping catch data issues early.</p>
</div>
</div>
</section>
<section id="additional-aop-patterns-suggestions" class="level2">
<h2 class="anchored" data-anchor-id="additional-aop-patterns-suggestions">Additional AOP Patterns Suggestions</h2>
<p>Here are some other useful patterns you can implement:</p>
<ol type="1">
<li><strong>Performance Monitoring</strong>:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> performance_pre(info):</span>
<span id="cb5-2">    info.start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> performance_post(info):</span>
<span id="cb5-5">    duration <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> info.start_time</span>
<span id="cb5-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Cell execution took </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>duration<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> seconds"</span>)</span></code></pre></div></div>
<ol start="2" type="1">
<li><strong>Memory Usage Tracking</strong>:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> memory_tracking(info):</span>
<span id="cb6-2">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> SparkSession</span>
<span id="cb6-3">    spark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SparkSession.builder.getOrCreate()</span>
<span id="cb6-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(spark.sparkContext.statusTracker().getExecutorMetrics())</span></code></pre></div></div>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://ipython.readthedocs.io/en/stable/config/callbacks.html">IPython Events Documentation</a></li>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/dataframe.html">PySpark DataFrame API</a></li>
</ul>
<p>Remember to initialize these patterns at the start of your notebook.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Databricks</category>
  <category>Python</category>
  <category>Tips</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/IpythonEvents.html</guid>
  <pubDate>Sun, 01 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Reusable Window Functions</title>
  <link>DailyDatabricks.Tips/tips/Data Engineering/windowfunctions.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Named window specifications allow you to define a window once and reference it in multiple window functions</li>
<li>This improves code readability and reduces duplication when using the same window definition repeatedly</li>
<li>Supported in both Databricks SQL and PySpark DataFrame API</li>
</ul>
</section>
<section id="detail" class="level2">
<h2 class="anchored" data-anchor-id="detail">Detail</h2>
<p>Window functions are a powerful tool for performing calculations across a set of rows related to the current row. However, when you need to apply the same window definition to multiple functions, the query can quickly become verbose and hard to read.</p>
<p>Named window specifications solve this by allowing you to declare the window spec once and reference it by name. Here’s how it works:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Databricks SQL</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">PySpark</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="syntax" class="level3">
<h3 class="anchored" data-anchor-id="syntax">Syntax:</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1">WINDOW { window_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> window_spec } [, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>.]</span></code></pre></div></div>
<p>Where:</p>
<ul>
<li><code>window_name</code> is an identifier to reference the window spec</li>
<li><code>window_spec</code> is the window definition to share across functions</li>
</ul>
</section>
<section id="example" class="level3">
<h3 class="anchored" data-anchor-id="example">Example:</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> product, </span>
<span id="cb2-2">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SUM</span>(sales) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OVER</span> monthly_window <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> monthly_sales,</span>
<span id="cb2-3">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AVG</span>(sales) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OVER</span> monthly_window <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> avg_monthly_sales</span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> sales_data</span>
<span id="cb2-5">WINDOW monthly_window <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PARTITION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> product <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">date</span>)</span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Window</span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql.functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb3-3"></span>
<span id="cb3-4">window <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Window.partitionBy(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>).orderBy(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>)</span>
<span id="cb3-5"></span>
<span id="cb3-6">sales_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sales_data.select(</span>
<span id="cb3-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>,</span>
<span id="cb3-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>).over(window).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"monthly_sales"</span>),</span>
<span id="cb3-9">    avg(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>).over(window).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg_monthly_sales"</span>)</span>
<span id="cb3-10">)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="benefits" class="level2">
<h2 class="anchored" data-anchor-id="benefits">Benefits</h2>
<div class="callout callout-style-simple callout-tip callout-titled" title="Why use named windows?">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Why use named windows?
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>Makes queries with multiple window functions more concise and readable</li>
<li>Allows easy reuse of complex window definitions</li>
<li>Helps avoid errors from repeating the same window spec multiple times</li>
</ul>
</div>
</div>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://docs.databricks.com/sql/language-manual/sql-ref-syntax-qry-select-named-window.html">Databricks SQL - SELECT Named Window</a></li>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.Window.html">PySpark Window Functions</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>deltalake</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Data Engineering/windowfunctions.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Efficient Performance Testing with Spark Write NOOP</title>
  <link>DailyDatabricks.Tips/tips/Performance/NOOP.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Beginner Friendly">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Beginner Friendly
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post is part of our series on making Spark development easier and more efficient, especially for those new to the platform.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li>Understanding and utilizing Spark’s NOOP (No Operation) write format</li>
<li>Benefits of using NOOP in development and testing</li>
<li>Practical code examples in Python</li>
</ul>
</section>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Apache Spark is a powerful tool for big data processing. However, developing and testing Spark applications can be challenging, especially when dealing with large datasets. The Spark write NOOP operation provides a solution for testing data processing without the overhead of actual data output.</p>
</section>
<section id="code-examples" class="level1">
<h1>Code Examples</h1>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Scala</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="basic-usage-of-write-noop" class="level3">
<h3 class="anchored" data-anchor-id="basic-usage-of-write-noop">Basic Usage of Write NOOP</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">dataframe.write.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"noop"</span>).mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>).save()</span></code></pre></div></div>
<p>This code snippet demonstrates the basic usage of the NOOP write format in Spark. It’s an excellent way for testing data processing logic without writing any data to disk or external systems.</p>
</section>
<section id="use-case-testing-data-transformations" class="level3">
<h3 class="anchored" data-anchor-id="use-case-testing-data-transformations">Use Case: Testing Data Transformations</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example DataFrame transformation</span></span>
<span id="cb2-2">transformed_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dataframe.withColumn(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new_column"</span>, f.expr(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"existing_column * 2"</span>))</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writing with NOOP for testing</span></span>
<span id="cb2-5">transformed_df.write.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"noop"</span>).mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>).save()</span></code></pre></div></div>
<p>This example shows how to use NOOP for testing transformations. It’s a great way to validate your logic without incurring the cost of data storage.</p>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<section id="basic-usage-of-write-noop-1" class="level3">
<h3 class="anchored" data-anchor-id="basic-usage-of-write-noop-1">Basic Usage of Write NOOP</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb3-1">dataframe<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>write<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"noop"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mode</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span></code></pre></div></div>
<p>Similar to Python, using NOOP in Scala offers a straightforward approach to test Spark jobs efficiently.</p>
</section>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Testing Made Simple">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Testing Made Simple
</div>
</div>
<div class="callout-body-container callout-body">
<p>The NOOP writer in Spark is a powerful tool for testing and validating data processing pipelines without the need for actual data persistence, saving both time and resources.</p>
</div>
</div>
</section>
<section id="detail" class="level1">
<h1>Detail</h1>
<p>The write NOOP operation in Spark is a unique feature that allows for the execution of data processing tasks without performing any actual write operation. This feature is particularly useful in scenarios where the primary goal is to test the processing logic of Spark jobs, without the need to persist the output.</p>
<section id="benefits-of-using-spark-write-noop" class="level2">
<h2 class="anchored" data-anchor-id="benefits-of-using-spark-write-noop">Benefits of Using Spark Write NOOP</h2>
<ol type="1">
<li><p><strong>Cost-Effective</strong>: Since no data is written to disk or external systems, it reduces the costs associated with data storage and management.</p></li>
<li><p><strong>Faster Testing Cycles</strong>: It allows for quicker iterations during the development phase, as there is no time lost in writing and reading data.</p></li>
<li><p><strong>Simplified Debugging</strong>: Debugging becomes easier, as you can focus purely on the processing logic without worrying about the output format or destination.</p></li>
<li><p><strong>Scalability Testing</strong>: You can test the scalability of your data processing logic on large datasets without the overhead of handling actual data output.</p></li>
</ol>
</section>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<section id="related-resources" class="level3">
<h3 class="anchored" data-anchor-id="related-resources">Related Resources</h3>
<ul>
<li>Spark Official Documentation: Provides comprehensive details on various Spark operations, including the write NOOP feature.</li>
<li>Data Processing Blogs: Many online resources and blogs discuss practical use cases and optimizations in Spark, including testing methodologies.</li>
<li>Community Forums: Platforms like Stack Overflow offer insights and solutions related to common issues encountered while using Spark’s NOOP operation.</li>
</ul>
<p>Remember, the Spark write NOOP operation is not just a feature; it’s a paradigm shift in how we approach data processing testing and development in the Spark ecosystem.</p>
<hr>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <guid>DailyDatabricks.Tips/tips/Performance/NOOP.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
  <media:content url="DailyDatabricks.Tips/tips/Performance/Assets/spark-noop.png" medium="image" type="image/png"/>
</item>
<item>
  <title>Enhancing Delta Tables with Custom Metadata Logging</title>
  <link>DailyDatabricks.Tips/tips/Delta/userMetdataData.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Learn how to enrich your Delta tables with custom metadata for better data lineage and governance</li>
<li>Implement metadata logging at both session and individual write operation levels</li>
<li>Explore practical use cases for leveraging custom metadata in data pipelines</li>
</ul>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>When working with Delta Lake tables in Databricks, one often overlooked but powerful feature is the ability to log custom metadata. This capability allows you to embed valuable context directly into your Delta table’s commit history, making it easier to track data lineage, audit changes, and maintain documentation. In this post, we’ll explore how to implement custom metadata logging and discuss some practical applications.</p>
</section>
<section id="setting-up-custom-metadata" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-custom-metadata">Setting Up Custom Metadata</h2>
<section id="session-level-configuration" class="level3">
<h3 class="anchored" data-anchor-id="session-level-configuration">Session-Level Configuration</h3>
<p>You can configure custom metadata logging for all Delta operations in a Spark session. Here’s how to do it in different languages:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">SQL</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">R</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="session-level-config" class="level3">
<h3 class="anchored" data-anchor-id="session-level-config">Session Level Config</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using SparkConf</span></span>
<span id="cb1-2">spark.conf.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(</span>
<span id="cb1-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.delta.commitInfo.userMetadata"</span>,</span>
<span id="cb1-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global session metadata - Data processed by ETL pipeline v2.1"</span></span>
<span id="cb1-5">)</span></code></pre></div></div>
</section>
<section id="operation-level-configuration" class="level3">
<h3 class="anchored" data-anchor-id="operation-level-configuration">Operation-Level Configuration</h3>
<p>For more granular control, you can add metadata to specific Dataframe Writer operations:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">df.write.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delta"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-2">    .option(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Custom metadata for this specific write operation"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-3">    .mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"append"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-4">    .saveAsTable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my_table"</span>)</span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> spark.databricks.delta.commitInfo.userMetadata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Global session metadata - Data processed by ETL pipeline v2.1'</span>;</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(SparkR)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparkR.session</span>()</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparkR.session</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sparkConfig =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.delta.commitInfo.userMetadata"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb4-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global session metadata - Data processed by ETL pipeline v2.3"</span>))</span></code></pre></div></div>
</div>
</div>
</div>
</section>
</section>
<section id="practical-use-cases" class="level2">
<h2 class="anchored" data-anchor-id="practical-use-cases">Practical Use Cases</h2>
<section id="data-pipeline-tracking" class="level3">
<h3 class="anchored" data-anchor-id="data-pipeline-tracking">1. Data Pipeline Tracking</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb5-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb5-3"></span>
<span id="cb5-4">pipeline_metadata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb5-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pipeline_id"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ETL_123"</span>,</span>
<span id="cb5-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source_system"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CRM"</span>,</span>
<span id="cb5-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"processing_timestamp"</span>: datetime.now().isoformat(),</span>
<span id="cb5-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_rules_version"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.2.0"</span></span>
<span id="cb5-9">}</span>
<span id="cb5-10"></span>
<span id="cb5-11">df.write.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delta"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb5-12">    .option(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata"</span>, json.dumps(pipeline_metadata)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb5-13">    .mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"append"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb5-14">    .saveAsTable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer_data"</span>)</span></code></pre></div></div>
</section>
<section id="data-quality-monitoring" class="level3">
<h3 class="anchored" data-anchor-id="data-quality-monitoring">2. Data Quality Monitoring</h3>
<p>Track quality metrics and validation results alongside your data:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">quality_metadata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb6-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"null_percentage"</span>: df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"important_field"</span>).isNull()).count() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> df.count() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"distinct_values"</span>: df.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"category_field"</span>).distinct().count(),</span>
<span id="cb6-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validation_status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PASSED"</span>,</span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quality_score"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span></span>
<span id="cb6-6">}</span>
<span id="cb6-7"></span>
<span id="cb6-8">df.write.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delta"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb6-9">    .option(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata"</span>, json.dumps(quality_metadata)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb6-10">    .mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb6-11">    .saveAsTable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"validated_transactions"</span>)</span></code></pre></div></div>
</section>
<section id="compliance-and-audit-trail" class="level3">
<h3 class="anchored" data-anchor-id="compliance-and-audit-trail">3. Compliance and Audit Trail</h3>
<p>For regulated industries, maintain detailed audit trails:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">audit_metadata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb7-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"approved_by"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.governance@company.com"</span>,</span>
<span id="cb7-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"approval_ticket"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TICK-123"</span>,</span>
<span id="cb7-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"compliance_check_version"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.0"</span>,</span>
<span id="cb7-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"retention_policy"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7_years"</span></span>
<span id="cb7-6">}</span></code></pre></div></div>
</section>
</section>
<section id="best-practices-and-tips" class="level2">
<h2 class="anchored" data-anchor-id="best-practices-and-tips">Best Practices and Tips</h2>
<ol type="1">
<li><strong>Structured Metadata</strong>: Use JSON format for complex metadata to maintain consistency and queryability</li>
<li><strong>Size Considerations</strong>: Keep metadata concise - it’s stored with every commit</li>
<li><strong>Automation</strong>: Implement automated metadata logging in your ETL frameworks</li>
<li><strong>Documentation</strong>: Include metadata schemas in your data documentation</li>
</ol>
<div class="callout callout-style-simple callout-warning callout-titled" title="Important Considerations">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Important Considerations
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>Metadata is immutable once written</li>
<li>Large metadata can impact performance</li>
<li>Consider implementing a standard metadata schema across your organization</li>
</ul>
</div>
</div>
</section>
<section id="querying-metadata-history" class="level2">
<h2 class="anchored" data-anchor-id="querying-metadata-history">Querying Metadata History</h2>
<p>There are several ways to access the commit history and metadata of your Delta tables. Let’s explore the different approaches:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">SQL</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Python DeltaTable API</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href="">Scala DeltaTable API</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<p>The simplest way is to use native SQL:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- View complete history with metadata</span></span>
<span id="cb8-2">DESCRIBE HISTORY my_delta_table;</span></code></pre></div></div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>For Python users, the <code>DeltaTable</code> class provides a programmatic way to access history:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> delta.tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> DeltaTable</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the DeltaTable instance</span></span>
<span id="cb9-4">deltaTable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DeltaTable.forName(spark, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my_delta_table"</span>)</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get complete history</span></span>
<span id="cb9-7">history_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deltaTable.history()</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select specific columns and filter</span></span>
<span id="cb9-10">metadata_history <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (history_df</span>
<span id="cb9-11">    .select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"timestamp"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"operation"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata"</span>)</span>
<span id="cb9-12">    .where(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata IS NOT NULL"</span>)</span>
<span id="cb9-13">    .orderBy(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span>)</span>
<span id="cb9-14">)</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display the results</span></span>
<span id="cb9-17">metadata_history.show(truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<p>If you’re working in Scala, you can use similar functionality:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">import</span> io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>delta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>tables<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>_</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Get the DeltaTable instance</span></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> deltaTable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DeltaTable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forName</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my_delta_table"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Get complete history</span></span>
<span id="cb10-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> historyDF <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deltaTable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">history</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Select specific columns and filter</span></span>
<span id="cb10-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> metadataHistory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> historyDF</span>
<span id="cb10-11">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"timestamp"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"operation"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb10-12">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"userMetadata IS NOT NULL"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb10-13">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">orderBy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"version"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb10-14"></span>
<span id="cb10-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Display the results</span></span>
<span id="cb10-16">metadataHistory<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">show</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Pro Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Pro Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>The history command returns a DataFrame, so you can leverage all the standard DataFrame operations to analyze your metadata. For example, you could: - Parse JSON metadata into structured columns - Aggregate metadata patterns over time - Join with other tracking tables for comprehensive lineage</p>
</div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Custom metadata logging in Delta tables provides a powerful way to enhance your data lake’s observability and governance. By implementing these practices, you can build more maintainable and traceable data pipelines.</p>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/delta/custom-metadata">Enrich Delta Lake tables with custom metadata</a></li>
<li><a href="https://www.databricks.com/blog/2019/08/21/diving-into-delta-lake-unpacking-the-transaction-log.html">Delta Lake Documentation on Transaction Log</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>deltalake</category>
  <category>Decembricks</category>
  <guid>DailyDatabricks.Tips/tips/Delta/userMetdataData.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Sensible SQL Warehouse Defaults for Governance and Cost Control</title>
  <link>DailyDatabricks.Tips/tips/SQL Warehouse/WorkspaceDefaults.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Configure workspace-level SQL configuration parameters for all SQL Warehouses</li>
<li>Set sensible defaults for <code>STATEMENT_TIMEOUT</code>, <code>ANSI_MODE</code>, <code>TIMEZONE</code>, and more</li>
<li>Prevent runaway queries, enforce SQL standards, and improve cost governance</li>
</ul>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Every Databricks workspace ships with SQL Warehouse configuration parameters set to very permissive defaults. A query can run for <strong>2 full days</strong> before it times out. ANSI mode may be off for older accounts, hiding silent behaviour. These are the kind of settings that, left untouched, lead to bill shock and hard-to-diagnose data quality issues.</p>
<p>The good news is that workspace admins can override these defaults in a single place and have them apply to <strong>every SQL Warehouse</strong> in the workspace. This post walks through the key parameters and recommends sensible <a href="https://www.thoughtworks.com/insights/topic/sensible-defaults">starting points</a>.</p>
<div class="callout callout-style-simple callout-note callout-titled" title="Admin Access Required">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Admin Access Required
</div>
</div>
<div class="callout-body-container callout-body">
<p>You must be a workspace admin to view and change these settings. If you’re not an admin, share this post with your team and advocate for setting these defaults.</p>
<p>They can only be set at the workspace level, not for all workspaces in an databricks account, so each workspace needs to be configured individually.</p>
</div>
</div>
</section>
<section id="where-to-set-these-parameters" class="level2">
<h2 class="anchored" data-anchor-id="where-to-set-these-parameters">Where to Set These Parameters</h2>
<p>Navigate to <strong>Admin Settings &gt; Compute &gt; SQL Warehouses &gt; SQL Configuration Parameters</strong>.</p>
<p>Enter one key-value pair per line, separating the parameter name from its value with a space:</p>
<pre><code>STATEMENT_TIMEOUT 3600
ANSI_MODE true
TIMEZONE UTC</code></pre>
<div class="callout callout-style-simple callout-warning callout-titled" title="Restart Warning">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Restart Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Changing any SQL configuration parameter will <strong>automatically restart all running SQL Warehouses</strong> in the workspace. Plan changes during a maintenance window or low-usage period.</p>
</div>
</div>
<p>You can also set these via the <a href="https://docs.databricks.com/api/workspace/warehouses">SQL Warehouses API</a> for infrastructure-as-code workflows.</p>
</section>
<section id="recommended-defaults" class="level2">
<h2 class="anchored" data-anchor-id="recommended-defaults">Recommended Defaults</h2>
<section id="statement_timeout" class="level3">
<h3 class="anchored" data-anchor-id="statement_timeout">1. STATEMENT_TIMEOUT</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<tbody>
<tr class="odd">
<td><strong>What it does</strong></td>
<td>Cancels any SQL statement that exceeds the configured number of seconds</td>
</tr>
<tr class="even">
<td><strong>System default</strong></td>
<td><code>172800</code> (2 days)</td>
</tr>
<tr class="odd">
<td><strong>Recommended</strong></td>
<td><code>3600</code> (1 hour)</td>
</tr>
</tbody>
</table>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Set at workspace level via Admin Settings, or per-session:</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> STATEMENT_TIMEOUT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3600</span>;</span></code></pre></div></div>
<p>A 2-day timeout is almost never what you want. Runaway queries are the single biggest source of unexpected SQL Warehouse costs. Setting a 1-hour default catches the vast majority of accidental full-table scans and cartesian joins while still allowing legitimate long-running analytics.</p>
<div class="callout callout-style-simple callout-tip callout-titled" title="Per-Session Override">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Per-Session Override
</div>
</div>
<div class="callout-body-container callout-body">
<p>Users who genuinely need longer execution times can override the timeout in their session with <code>SET STATEMENT_TIMEOUT = 7200;</code> — so this default doesn’t block anyone, it just makes the dangerous case opt-in rather than opt-out.</p>
</div>
</div>
</section>
<section id="ansi_mode" class="level3">
<h3 class="anchored" data-anchor-id="ansi_mode">2. ANSI_MODE</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<tbody>
<tr class="odd">
<td><strong>What it does</strong></td>
<td>Enforces strict ANSI SQL behaviour for casting, arithmetic overflow, and string operations</td>
</tr>
<tr class="even">
<td><strong>System default</strong></td>
<td><code>TRUE</code> (accounts created after Oct 2022), <code>FALSE</code> (older accounts)</td>
</tr>
<tr class="odd">
<td><strong>Recommended</strong></td>
<td><code>TRUE</code></td>
</tr>
</tbody>
</table>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> ANSI_MODE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>;</span></code></pre></div></div>
<p>With ANSI mode <strong>off</strong>, operations like dividing by zero return <code>NULL</code> instead of an error, and implicit casts can silently truncate data. Turning it on surfaces these issues at query time rather than hiding them in your data. If you have an older workspace still on <code>FALSE</code>, switching to <code>TRUE</code> is one of the highest-impact governance changes you can make.</p>
<div class="callout callout-style-simple callout-note callout-titled" title="Migration Consideration">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Migration Consideration
</div>
</div>
<div class="callout-body-container callout-body">
<p>Enabling ANSI mode on an existing workspace may cause some legacy queries to fail — particularly those relying on silent overflow or implicit type coercion. Test with a representative query set first.</p>
</div>
</div>
</section>
<section id="timezone" class="level3">
<h3 class="anchored" data-anchor-id="timezone">3. TIMEZONE</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<tbody>
<tr class="odd">
<td><strong>What it does</strong></td>
<td>Sets the default time zone for timestamp operations</td>
</tr>
<tr class="even">
<td><strong>System default</strong></td>
<td><code>UTC</code></td>
</tr>
<tr class="odd">
<td><strong>Recommended</strong></td>
<td><code>UTC</code></td>
</tr>
</tbody>
</table>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> TIMEZONE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'UTC'</span>;</span></code></pre></div></div>
<p>Unless you have a strong regional reason to change this, keep it at <code>UTC</code>. Consistent time zone handling across all warehouses avoids a whole class of subtle bugs when joining or comparing timestamp data across tables, pipelines, and teams.</p>
</section>
<section id="use_cached_result" class="level3">
<h3 class="anchored" data-anchor-id="use_cached_result">4. USE_CACHED_RESULT</h3>
<table class="caption-top table">
<tbody>
<tr class="odd">
<td><strong>What it does</strong></td>
<td>Caches and reuses query results when possible</td>
</tr>
<tr class="even">
<td><strong>System default</strong></td>
<td><code>TRUE</code></td>
</tr>
<tr class="odd">
<td><strong>Recommended</strong></td>
<td><code>TRUE</code></td>
</tr>
</tbody>
</table>
<p>This is already set to a sensible default. Leave it enabled — it reduces compute costs for repeated queries (dashboards, reports) without any correctness risk, since the cache is invalidated when underlying data changes.</p>
</section>
<section id="legacy_time_parser_policy" class="level3">
<h3 class="anchored" data-anchor-id="legacy_time_parser_policy">5. LEGACY_TIME_PARSER_POLICY</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<tbody>
<tr class="odd">
<td><strong>What it does</strong></td>
<td>Controls how dates and timestamps are parsed and formatted</td>
</tr>
<tr class="even">
<td><strong>System default</strong></td>
<td><code>EXCEPTION</code></td>
</tr>
<tr class="odd">
<td><strong>Recommended</strong></td>
<td><code>EXCEPTION</code></td>
</tr>
</tbody>
</table>
<p>The <code>EXCEPTION</code> default is the strictest and safest option — it raises an error on ambiguous date/time parsing rather than silently guessing. Keep it.</p>
</section>
</section>
<section id="putting-it-all-together" class="level2">
<h2 class="anchored" data-anchor-id="putting-it-all-together">Putting It All Together</h2>
<p>Here is a recommended workspace-level configuration block you can paste directly into the SQL Configuration Parameters textbox:</p>
<pre><code>STATEMENT_TIMEOUT 3600
ANSI_MODE true
TIMEZONE UTC
USE_CACHED_RESULT true
LEGACY_TIME_PARSER_POLICY EXCEPTION</code></pre>
</section>
<section id="managing-as-infrastructure-as-code" class="level2">
<h2 class="anchored" data-anchor-id="managing-as-infrastructure-as-code">Managing as Infrastructure as Code</h2>
<p>Clicking through the Admin UI is fine for a single workspace, but if you manage multiple environments (dev, staging, prod) you want these settings version-controlled and reproducible.</p>
<section id="terraform" class="level3">
<h3 class="anchored" data-anchor-id="terraform">Terraform</h3>
<p>The <a href="https://registry.terraform.io/providers/databricks/databricks/latest/docs/resources/sql_global_config"><code>databricks_sql_global_config</code></a> resource lets you set all the recommended parameters in one block using <code>sql_config_params</code>:</p>
<pre class="hcl"><code>resource "databricks_sql_global_config" "this" {
  sql_config_params = {
    "STATEMENT_TIMEOUT"         = "3600"
    "ANSI_MODE"                 = "true"
    "TIMEZONE"                  = "UTC"
    "USE_CACHED_RESULT"         = "true"
    "LEGACY_TIME_PARSER_POLICY" = "EXCEPTION"
  }
}</code></pre>
<div class="callout callout-style-simple callout-warning callout-titled" title="Restart Warning">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Restart Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Just like the UI, any change to this Terraform resource will <strong>automatically restart all running SQL Warehouses</strong> in the workspace. Run <code>terraform plan</code> carefully and apply during low-usage periods.</p>
</div>
</div>
</section>
<section id="databricks-asset-bundles-dabs" class="level3">
<h3 class="anchored" data-anchor-id="databricks-asset-bundles-dabs">Databricks Asset Bundles (DABs)</h3>
<p>DABs can manage individual <a href="https://docs.databricks.com/aws/en/dev-tools/bundles/resources"><code>sql_warehouse</code></a> resources (size, auto-stop, serverless, etc.) but <strong>does not currently support a <code>sql_global_config</code> resource type</strong> for workspace-level SQL configuration parameters.</p>
<p>So use Terraform or the <a href="https://docs.databricks.com/api/workspace/warehouses">SQL Warehouses API</a> to manage these global defaults alongside your DABs deployments.</p>
</section>
</section>
<section id="quick-reference-table" class="level2">
<h2 class="anchored" data-anchor-id="quick-reference-table">Quick Reference Table</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Parameter</th>
<th>System Default</th>
<th>Recommended</th>
<th>Why</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>STATEMENT_TIMEOUT</code></td>
<td>172,800s (2 days)</td>
<td>3,600s (1 hour)</td>
<td>Prevent runaway query costs</td>
</tr>
<tr class="even">
<td><code>ANSI_MODE</code></td>
<td><code>TRUE</code> / <code>FALSE</code>*</td>
<td><code>TRUE</code></td>
<td>Enforce SQL standards, catch bugs early</td>
</tr>
<tr class="odd">
<td><code>TIMEZONE</code></td>
<td><code>UTC</code></td>
<td><code>UTC</code></td>
<td>Consistency across pipelines</td>
</tr>
<tr class="even">
<td><code>USE_CACHED_RESULT</code></td>
<td><code>TRUE</code></td>
<td><code>TRUE</code></td>
<td>Free cost savings on repeated queries</td>
</tr>
<tr class="odd">
<td><code>LEGACY_TIME_PARSER_POLICY</code></td>
<td><code>EXCEPTION</code></td>
<td><code>EXCEPTION</code></td>
<td>Strictest date parsing, no silent errors</td>
</tr>
</tbody>
</table>
<p><em>*<code>FALSE</code> for accounts created before October 2022</em></p>
</section>
<section id="references-further-reading" class="level2">
<h2 class="anchored" data-anchor-id="references-further-reading">References &amp; Further Reading</h2>
<ul>
<li><a href="https://docs.databricks.com/aws/en/admin/sql/">SQL Warehouse Admin Settings</a></li>
<li><a href="https://docs.databricks.com/sql/language-manual/sql-ref-parameters.html">Configuration Parameters Reference</a></li>
<li><a href="https://docs.databricks.com/aws/en/sql/language-manual/parameters/statement_timeout">STATEMENT_TIMEOUT Parameter</a></li>
<li><a href="https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode">ANSI_MODE Parameter</a></li>
<li><a href="https://docs.databricks.com/aws/en/sql/language-manual/parameters/timezone">TIMEZONE Parameter</a></li>
<li><a href="https://registry.terraform.io/providers/databricks/databricks/latest/docs/resources/sql_global_config">databricks_sql_global_config Terraform Resource</a></li>
<li><a href="https://docs.databricks.com/aws/en/dev-tools/bundles/resources">Databricks Asset Bundles Resources</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>sql</category>
  <category>governance</category>
  <category>cost</category>
  <guid>DailyDatabricks.Tips/tips/SQL Warehouse/WorkspaceDefaults.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Grant Write Access Without Handing Over the Schema</title>
  <link>DailyDatabricks.Tips/tips/Unity Catalog/FineGrainedDMLPrivileges.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Unity Catalog splits <code>MODIFY</code> into three child privileges: <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code>.</li>
<li>Grant one of them when a principal must change a table’s data but not its schema.</li>
<li><code>MODIFY</code> still covers <code>ALTER TABLE</code>, <code>OPTIMIZE</code>, <code>VACUUM</code>, and schema evolution.</li>
</ul>
</section>
<section id="the-problem-with-modify" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-with-modify">The Problem With MODIFY</h2>
<p>Until now, Unity Catalog offered one write privilege: <code>MODIFY</code>. Granting it to an ingestion job so the job can append rows also lets that job drop a column, truncate the table, or rewrite the schema. Most write workloads need one verb, so <code>MODIFY</code> grants far more than they use.</p>
<p>As of August 2026, you can grant that single verb. <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code> are child privileges of <code>MODIFY</code>, and each covers a subset of its write access.</p>
<div class="callout callout-style-simple callout-important callout-titled" title="Beta">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Beta
</div>
</div>
<div class="callout-body-container callout-body">
<p>This feature is in Beta. A workspace admin enables it from <strong>Settings &gt; Previews</strong>. It requires Databricks Runtime 18.1 or above and runs on serverless compute, SQL warehouses, and classic compute in standard access mode. Dedicated access mode is not supported — use <code>MODIFY</code> there.</p>
</div>
</div>
</section>
<section id="working-example" class="level2">
<h2 class="anchored" data-anchor-id="working-example">Working Example</h2>
<p>The example below builds an append-only ingestion path. The <code>order-loaders</code> group can add orders and read them back. It cannot delete rows, and it cannot change the table’s shape.</p>
<section id="set-up-the-table" class="level3">
<h3 class="anchored" data-anchor-id="set-up-the-table">1. Set Up the Table</h3>
<p>Run this as a catalog admin or the table owner.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> main.dml_demo;</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> main.dml_demo.orders (</span>
<span id="cb1-4">  order_id BIGINT,</span>
<span id="cb1-5">  customer STRING,</span>
<span id="cb1-6">  amount   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">DECIMAL</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-7">);</span></code></pre></div></div>
</section>
<section id="grant-the-privileges" class="level3">
<h3 class="anchored" data-anchor-id="grant-the-privileges">2. Grant the Privileges</h3>
<p>A fine-grained DML privilege never works alone. The principal also needs <code>SELECT</code> on the table and traversal privileges on the parents.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Traversal. Required to reach the table at all.</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USE</span> CATALOG <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> CATALOG main <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> main.dml_demo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Read the data, and append to it. Nothing else.</span></span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span>
<span id="cb2-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span></code></pre></div></div>
<p>Confirm the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1">SHOW GRANTS `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders` <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders;</span></code></pre></div></div>
<pre class="text"><code>principal      actionType  objectType  objectKey
-------------  ----------  ----------  ----------------------
order-loaders  SELECT      TABLE       main.dml_demo.orders
order-loaders  INSERT      TABLE       main.dml_demo.orders</code></pre>
</section>
<section id="confirm-the-boundary" class="level3">
<h3 class="anchored" data-anchor-id="confirm-the-boundary">3. Confirm the Boundary</h3>
<p>Run the following as a member of <code>order-loaders</code>. The first two statements succeed; the rest fail with <code>PERMISSION_DENIED</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Succeeds. Covered by INSERT.</span></span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Acme Corp'</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">250.00</span>);</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Succeeds. Covered by SELECT.</span></span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.dml_demo.orders;</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Fails. Requires DELETE.</span></span>
<span id="cb5-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DELETE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> order_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Fails. Requires INSERT and DELETE, because it replaces existing rows.</span></span>
<span id="cb5-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> OVERWRITE main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Globex'</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">99.00</span>);</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Fails. Schema changes still require MODIFY.</span></span>
<span id="cb5-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> region STRING;</span></code></pre></div></div>
<p>The job can do its one job. The blast radius of a bad deploy is now bounded by the privilege, not by review discipline.</p>
</section>
</section>
<section id="which-privilege-covers-which-operation" class="level2">
<h2 class="anchored" data-anchor-id="which-privilege-covers-which-operation">Which Privilege Covers Which Operation</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Operation</th>
<th>Required privileges</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>INSERT INTO</code></td>
<td><code>INSERT</code></td>
</tr>
<tr class="even">
<td><code>UPDATE</code></td>
<td><code>UPDATE</code></td>
</tr>
<tr class="odd">
<td><code>DELETE</code></td>
<td><code>DELETE</code></td>
</tr>
<tr class="even">
<td><code>TRUNCATE</code></td>
<td><code>DELETE</code></td>
</tr>
<tr class="odd">
<td><code>INSERT OVERWRITE</code>, <code>REPLACE WHERE</code>, dynamic partition overwrite</td>
<td><code>INSERT</code> and <code>DELETE</code></td>
</tr>
<tr class="even">
<td><code>MERGE INTO</code></td>
<td><code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code>, matching the actions in the statement</td>
</tr>
<tr class="odd">
<td>Schema evolution, <code>ALTER TABLE</code>, <code>OPTIMIZE</code>, <code>VACUUM</code></td>
<td><code>MODIFY</code></td>
</tr>
</tbody>
</table>
<p><code>MERGE INTO</code> is the useful case. A merge that only matches and updates needs <code>UPDATE</code>; add a <code>WHEN NOT MATCHED THEN INSERT</code> clause and it also needs <code>INSERT</code>. Grant the clauses your statement actually uses:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>upserters`;</span></code></pre></div></div>
</section>
<section id="two-behaviours-to-watch" class="level2">
<h2 class="anchored" data-anchor-id="two-behaviours-to-watch">Two Behaviours to Watch</h2>
<section id="granting-modify-does-not-grant-its-children" class="level3">
<h3 class="anchored" data-anchor-id="granting-modify-does-not-grant-its-children">Granting MODIFY Does Not Grant Its Children</h3>
<p><code>MODIFY</code> and its child privileges are granted and revoked independently. Granting <code>MODIFY</code> does not grant <code>INSERT</code>. More importantly, revoking <code>MODIFY</code> does not revoke an <code>INSERT</code> that was granted explicitly:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REVOKE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">MODIFY</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- An explicitly granted INSERT survives this. Revoke it too.</span></span>
<span id="cb7-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REVOKE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.dml_demo.orders <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span></code></pre></div></div>
<p>Audit with <code>SHOW GRANTS</code> after any revoke. Do not assume that dropping the parent privilege closed the door.</p>
</section>
<section id="catalog-and-schema-grants-cascade" class="level3">
<h3 class="anchored" data-anchor-id="catalog-and-schema-grants-cascade">Catalog and Schema Grants Cascade</h3>
<p>Privilege inheritance applies here as it does elsewhere. Granting <code>INSERT</code> on a schema grants it on every table in that schema, including tables created later:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Every current and future table in the schema becomes appendable.</span></span>
<span id="cb8-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GRANT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> main.dml_demo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> `order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>loaders`;</span></code></pre></div></div>
<p>Use a schema-level grant for a landing zone you own end to end. Grant at the table level everywhere else.</p>
<div class="callout callout-style-simple callout-warning callout-titled" title="Path-Based Access">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Path-Based Access
</div>
</div>
<div class="callout-body-container callout-body">
<p>Fine-grained DML privileges apply to table-based access only. Writing to an external table by its storage path still requires <code>MODIFY</code>.</p>
</div>
</div>
</section>
</section>
<section id="when-to-use-each" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-each">When to Use Each</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Principal</th>
<th>Grant</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Append-only ingestion job</td>
<td><code>SELECT</code>, <code>INSERT</code></td>
</tr>
<tr class="even">
<td>CDC or upsert pipeline</td>
<td><code>SELECT</code>, <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code></td>
</tr>
<tr class="odd">
<td>GDPR erasure service</td>
<td><code>SELECT</code>, <code>DELETE</code></td>
</tr>
<tr class="even">
<td>Correction or backfill tool</td>
<td><code>SELECT</code>, <code>UPDATE</code></td>
</tr>
<tr class="odd">
<td>Table owner or maintenance job</td>
<td><code>MODIFY</code></td>
</tr>
</tbody>
</table>
</section>
<section id="references-further-reading" class="level2">
<h2 class="anchored" data-anchor-id="references-further-reading">References &amp; Further Reading</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/data-governance/unity-catalog/access-control/fine-grained-dml-privileges">Fine-grained DML privileges</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/data-governance/unity-catalog/access-control/privileges-reference">Unity Catalog privileges reference</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/data-governance/unity-catalog/access-control/permissions-concepts">Privilege inheritance</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/release-notes/product/2026/august">August 2026 platform release notes</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Unity Catalog</category>
  <category>governance</category>
  <category>sql</category>
  <guid>DailyDatabricks.Tips/tips/Unity Catalog/FineGrainedDMLPrivileges.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Make multi-table writes all-or-nothing</title>
  <link>DailyDatabricks.Tips/tips/Unity Catalog/Transactions.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Wrap multiple SQL statements in <code>BEGIN ATOMIC ... END;</code> to commit them as one transaction.</li>
<li>Every table you write to must be a Unity Catalog managed table with catalog commits enabled.</li>
<li>A failure anywhere in the block rolls back the whole block. No partial writes reach the table.</li>
</ul>
</section>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>A pipeline that debits one table, credits another, and appends to an audit log runs three statements. Each one commits on its own. If the second statement fails, the first is already durable and the third never runs. The tables now disagree, and you repair them by hand.</p>
<p>As of July 2026, transactions on Unity Catalog managed Delta tables are generally available. Group the statements and the lakehouse guarantees all or nothing.</p>
</section>
<section id="before-you-begin" class="level2">
<h2 class="anchored" data-anchor-id="before-you-begin">Before you begin</h2>
<p>You need the following:</p>
<ul>
<li>A SQL warehouse, serverless compute, or a cluster running Databricks Runtime 18.0 or above.</li>
<li>Unity Catalog managed tables with the <code>catalogManaged</code> table feature on every write target.</li>
<li>Permission to create a schema and tables in a catalog. This guide uses <code>main</code>.</li>
</ul>
</section>
<section id="create-the-tables" class="level2">
<h2 class="anchored" data-anchor-id="create-the-tables">Create the tables</h2>
<p>Run the following statements outside a transaction. Transactions don’t support DDL.</p>
<p>Set <code>delta.feature.catalogManaged</code> at creation time. Catalog commits move commit coordination from the file system to Unity Catalog, which is what lets one commit span two tables.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> main.txn_demo;</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.txn_demo.accounts (</span>
<span id="cb1-4">  account_id BIGINT,</span>
<span id="cb1-5">  owner      STRING,</span>
<span id="cb1-6">  balance    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">DECIMAL</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-7">) TBLPROPERTIES (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'delta.feature.catalogManaged'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'supported'</span>);</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.txn_demo.transfer_log (</span>
<span id="cb1-10">  from_account BIGINT,</span>
<span id="cb1-11">  to_account   BIGINT,</span>
<span id="cb1-12">  amount       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">DECIMAL</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-13">  logged_at    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">TIMESTAMP</span></span>
<span id="cb1-14">) TBLPROPERTIES (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'delta.feature.catalogManaged'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'supported'</span>);</span></code></pre></div></div>
<p>Add a constraint so the example has a rule to break, then seed two accounts:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> main.txn_demo.accounts</span>
<span id="cb2-2">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CONSTRAINT</span> positive_balance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CHECK</span> (balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>);</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span></span>
<span id="cb2-5">  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Ada'</span>,   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500.00</span>),</span>
<span id="cb2-6">  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Grace'</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">125.00</span>);</span></code></pre></div></div>
<p>To confirm that catalog commits are on, run <code>DESCRIBE DETAIL main.txn_demo.accounts</code> and look for <code>catalogManaged</code> in the <code>tableFeatures</code> column.</p>
<div class="callout callout-style-simple callout-note callout-titled" title="Existing tables">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Existing tables
</div>
</div>
<div class="callout-body-container callout-body">
<p>To enable catalog commits on a table you already have, run <code>ALTER TABLE &lt;table&gt; SET TBLPROPERTIES ('delta.feature.catalogManaged' = 'supported')</code>. The statement syncs table state with the catalog, so it can take several minutes on a table with a long write history.</p>
</div>
</div>
</section>
<section id="move-money-in-one-commit" class="level2">
<h2 class="anchored" data-anchor-id="move-money-in-one-commit">Move money in one commit</h2>
<p>The following transaction debits one account, credits another, and writes the audit row. All three statements commit together.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BEGIN</span> ATOMIC</span>
<span id="cb3-2">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb3-3">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb3-4">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> main.txn_demo.transfer_log</span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.00</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_timestamp</span>());</span>
<span id="cb3-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span>;</span></code></pre></div></div>
<p>Check the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> account_id, owner, balance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> account_id;</span></code></pre></div></div>
<pre class="text"><code>account_id  owner   balance
----------  ------  -------
1           Ada     400.00
2           Grace   225.00</code></pre>
</section>
<section id="watch-it-roll-back" class="level2">
<h2 class="anchored" data-anchor-id="watch-it-roll-back">Watch it roll back</h2>
<p>Now run a transfer that Grace can’t cover. The audit row and the credit come first and both succeed. The debit is last, and it violates <code>positive_balance</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BEGIN</span> ATOMIC</span>
<span id="cb6-2">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> main.txn_demo.transfer_log</span>
<span id="cb6-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_timestamp</span>());</span>
<span id="cb6-4">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb6-5">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb6-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span>;</span></code></pre></div></div>
<p>The statement fails with <code>DELTA_VIOLATE_CONSTRAINT_WITH_VALUES</code>. Verify that the two successful statements were undone as well:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span></span>
<span id="cb7-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> balance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ada,</span>
<span id="cb7-3">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.txn_demo.transfer_log)                 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> log_rows;</span></code></pre></div></div>
<pre class="text"><code>ada     log_rows
------  --------
400.00  1</code></pre>
<p>Ada’s balance is untouched and the log still holds one row. Without the transaction, you would be holding a phantom <code>5000.00</code> credit and an audit entry for a transfer that never happened.</p>
</section>
<section id="fail-fast-instead" class="level2">
<h2 class="anchored" data-anchor-id="fail-fast-instead">Fail fast instead</h2>
<p>The rollback above does the right thing, but it does the work first and discards it. Validate up front with <a href="https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/control-flow/signal-stmt"><code>SIGNAL</code></a>, which raises an error and triggers the same automatic rollback:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb9-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BEGIN</span> ATOMIC</span>
<span id="cb9-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> balance <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span></span>
<span id="cb9-3">    SIGNAL SQLSTATE <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'75001'</span></span>
<span id="cb9-4">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> MESSAGE_TEXT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Insufficient funds in account 2.'</span>;</span>
<span id="cb9-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span>;</span>
<span id="cb9-6"></span>
<span id="cb9-7">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb9-8">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> main.txn_demo.accounts <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000.00</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> account_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb9-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span>;</span></code></pre></div></div>
</section>
<section id="choose-a-mode" class="level2">
<h2 class="anchored" data-anchor-id="choose-a-mode">Choose a mode</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th>Mode</th>
<th>Syntax</th>
<th>Commit and rollback</th>
<th>Conflict detection</th>
<th>Use it for</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Non-interactive</td>
<td><code>BEGIN ATOMIC ... END;</code></td>
<td>Automatic</td>
<td>Row level</td>
<td>Jobs, pipelines, stored procedures</td>
</tr>
<tr class="even">
<td>Interactive</td>
<td><code>BEGIN TRANSACTION; ... COMMIT;</code></td>
<td>Manual, with <code>ROLLBACK</code></td>
<td>Table level</td>
<td>JDBC, ODBC, and Python clients that drive commits themselves</td>
</tr>
</tbody>
</table>
<p>Prefer <code>BEGIN ATOMIC</code>. It detects conflicts at the row level, so two transactions can write different rows of the same file without colliding, and it can’t leave a session holding an open transaction.</p>
<p>Reach for interactive transactions when a client outside SQL decides whether to commit. Start those sessions with a <code>ROLLBACK</code> to clear any leftover state, and note that they roll back after 10 minutes of inactivity.</p>
</section>
<section id="behaviour-to-plan-for" class="level2">
<h2 class="anchored" data-anchor-id="behaviour-to-plan-for">Behaviour to plan for</h2>
<p><strong>Reads are repeatable.</strong> The first time a transaction touches a table, it pins a snapshot. Every later read of that table in the same transaction sees that snapshot, even if someone else commits to it meanwhile.</p>
<p><strong>Commits are optimistic.</strong> Nothing locks. Conflicts surface at commit time, and the loser fails. Retry failed transactions against fresh data rather than assuming they’ll succeed.</p>
<p><strong>One transaction is one Delta log entry.</strong> However many statements ran, the table history shows a single commit, with the individual operations as JSON metadata. Auditing and rollback stay simple.</p>
</section>
<section id="limits-worth-knowing" class="level2">
<h2 class="anchored" data-anchor-id="limits-worth-knowing">Limits worth knowing</h2>
<ul>
<li>No DDL. Run <code>CREATE</code>, <code>ALTER</code>, and <code>DROP</code> outside the transaction.</li>
<li>No time travel, no <code>SHOW TABLES</code>, and no queries against system tables inside a transaction.</li>
<li>No path-based access. Selecting straight from a storage path fails with <code>PATH_BASED_ACCESS</code>. To read a non-transactional source, register it as a table and add the <code>WITH (allow_nontransactional_read = true)</code> hint.</li>
<li>Up to 100 tables written or read, and up to 100 views read, per transaction.</li>
<li>Every transaction rolls back after 48 hours.</li>
</ul>
<div class="callout callout-style-simple callout-important callout-titled" title="Iceberg">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Iceberg
</div>
</div>
<div class="callout-body-container callout-body">
<p>Transactions that write to Unity Catalog managed Iceberg tables are still in Private Preview. Managed Delta tables are GA.</p>
</div>
</div>
</section>
<section id="clean-up" class="level2">
<h2 class="anchored" data-anchor-id="clean-up">Clean up</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SCHEMA</span> main.txn_demo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CASCADE</span>;</span></code></pre></div></div>
</section>
<section id="references-further-reading" class="level2">
<h2 class="anchored" data-anchor-id="references-further-reading">References &amp; Further Reading</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/transactions/">Transactions</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/transactions/transaction-modes">Transaction modes</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/tables/features/catalog-commits">Catalog commits</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/sql-ref-syntax-txn-begin-atomic">ATOMIC compound statement</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/databricks/release-notes/product/2026/july">July 2026 platform release notes</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Unity Catalog</category>
  <category>Delta</category>
  <category>sql</category>
  <category>transactions</category>
  <guid>DailyDatabricks.Tips/tips/Unity Catalog/Transactions.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Undropping Managed Tables</title>
  <link>DailyDatabricks.Tips/tips/Errors/INVALID_STATE.RESTORATION_PERIOD_EXPIRED .html</link>
  <description><![CDATA[ 





<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li><p><strong>Error Overview</strong>: Insights into why the <code>INVALID_STATE.RESTORATION_PERIOD_EXPIRED</code> error occurs within Databricks and Unity Catalog environments.</p></li>
<li><p><strong>Troubleshooting Steps</strong>: Step-by-step troubleshooting process to identify and resolve Databricks table restoration issues, using commands like <code>SHOW TABLES DROPPED</code>.</p></li>
<li><p><strong>Restoration with Alternative Names</strong>: Strategies to avoid naming conflicts during the restoration of Databricks tables by employing alternative names.</p></li>
</ul>
<section id="understanding-the-error" class="level2">
<h2 class="anchored" data-anchor-id="understanding-the-error">Understanding the Error</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">[RequestId<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&lt;</span>GUID<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> ErrorClass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>INVALID_STATE.RESTORATION_PERIOD_EXPIRED] Cannot undrop table because the table was deleted before the maximum supported restorable period of <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> days.</span></code></pre></div></div>
<p><img src="DailyDatabricks.Tips/tips/Errors/Assets/InvalidStateError.png" class="img-fluid"></p>
<p>When working with data in Databricks, particularly with the Unity Catalog managed tables, users may encounter the <code>INVALID_STATE.RESTORATION_PERIOD_EXPIRED</code> error. This error suggests an attempt to recover a table that was deleted more than 7 days ago, exceeding the maximum restorable period supported by Databricks. However, may not always be the case.</p>
<div class="callout callout-style-simple callout-warning callout-titled" title="Key Info">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Key Info
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post won’t help you address tables that were dropped after the 7 day restoration period. It will help with errors within that 7 day window</p>
</div>
</div>
<p>The <code>INVALID_STATE.RESTORATION_PERIOD_EXPIRED</code> error occurs under the following conditions:</p>
<ul>
<li><p>Time Limit Exceeded: The primary reason is attempting to undrop a table after the 7-day restoration window has closed.</p></li>
<li><p>Recreated Table: If a table with the same name has been recreated after being dropped, it can complicate the restoration process.</p></li>
<li><p>Table Name Issues: Mistakes in the table name during the restoration command might lead to failure in identifying the correct dropped table.</p></li>
</ul>
</section>
</section>
<section id="solution" class="level1">
<h1>Solution</h1>
<section id="troubleshooting-steps" class="level2">
<h2 class="anchored" data-anchor-id="troubleshooting-steps">Troubleshooting Steps</h2>
<p>To effectively resolve the <code>INVALID_STATE.RESTORATION_PERIOD_EXPIRED</code> error, follow these detailed steps:</p>
<ol type="1">
<li>Verify the Restoration Window</li>
</ol>
<p>First, ensure that the table was dropped within the last 7 days. If the table was dropped earlier, it cannot be recovered using the UNDROP command.</p>
<ol start="2" type="1">
<li>Use SHOW TABLES DROPPED</li>
</ol>
<p>To identify the correct table and verify the drop date, use the SHOW TABLES DROPPED command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1">SHOW <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLES</span> DROPPED <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IN</span> my_schema;</span></code></pre></div></div>
<p>This command will list all the dropped tables in the specified schema along with their metadata including the drop time and table ID.</p>
<ol start="3" type="1">
<li>Check for Recreated Tables</li>
</ol>
<p>If a table with the intended name has been recreated, it may not be immediately visible that the original table has been dropped. Confirm the status of the table names and consider using different names for newly created tables to avoid confusion.</p>
<ol start="4" type="1">
<li>Restore with Alternative Names</li>
</ol>
<p>If restoration is required but the original name is taken or to avoid confusion, use the ALTER TABLE RENAME TO command before executing the UNDROP command to assign a new name to the existing table:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> existing_table <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RENAME</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TO</span> new_table_name;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Rename the existing table</span></span>
<span id="cb3-2">UNDROP <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ID</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_table_id_here'</span>;            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Restore the dropped table</span></span></code></pre></div></div>
<ol start="5" type="1">
<li>Correct Usage of Table IDs</li>
</ol>
<p>When attempting to recover a specific table, use the table ID retrieved from the SHOW TABLES DROPPED command. This approach minimizes the risk of errors related to table names:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb4-1">UNDROP <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ID</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_table_id_here'</span>;</span></code></pre></div></div>
</section>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<section id="related-tweets" class="level3">
<h3 class="anchored" data-anchor-id="related-tweets">Related Tweets</h3>
</section>
<section id="links" class="level3">
<h3 class="anchored" data-anchor-id="links">Links</h3>
<p><a href="https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-undrop-table.html">Undrop Documentation</a></p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>unity</category>
  <category>errors</category>
  <category>sql</category>
  <guid>DailyDatabricks.Tips/tips/Errors/INVALID_STATE.RESTORATION_PERIOD_EXPIRED .html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>.transform() vs .withColumns(): Right Tool, Right Grain</title>
  <link>DailyDatabricks.Tips/tips/Spark/TransformVsWithColumns.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Live Execution">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Live Execution
</div>
</div>
<div class="callout-body-container callout-body">
<p>All code in this post is rendered against a Databricks workspace before publication.</p>
</div>
</div>
<div class="callout callout-style-simple callout-note callout-titled" title="Context">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Context
</div>
</div>
<div class="callout-body-container callout-body">
<p>Inspired by <a href="https://www.linkedin.com/posts/denys-kulemza_stop-writing-500-line-pyspark-scripts-we-share-7431723120543182848-k9Ws?utm_source=share&amp;utm_medium=member_desktop&amp;rcm=ACoAAA-Omc0BReiFFqoOChIIcZpbs7HxWFIfdcU">Denys K.’s LinkedIn post</a> on using <code>.transform()</code> to break up long <code>.withColumn()</code> chains. The intent is spot on — nobody wants 500-line mega-chains. But there’s a better tool for column-level work.</p>
</div>
</div>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Wrapping individual <code>.withColumn()</code> calls in functions and chaining with <code>.transform()</code> hides column-level intent — you have to read every function definition to know what columns are being added</li>
<li><code>.withColumns()</code> (Spark 3.3+ / DBR 12.0+) keeps every column name and expression visible at the call site, in a single plan node</li>
<li><code>.transform()</code> shines at <strong>pipeline-level</strong> chaining: deduplication, SCD logic, audit metadata — not individual column expressions</li>
<li>Both approaches are testable.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled" title="Setup: Create Sample Data">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Setup: Create Sample Data
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Faker-generated data matching the original post’s scenario: <code>first</code>, <code>last</code>, <code>age</code>, <code>email</code>, <code>status</code>.</p>
<div id="52c8e919" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> faker <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Faker</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql.types <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb1-5"></span>
<span id="cb1-6">fake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Faker()</span>
<span id="cb1-7">Faker.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb1-8">random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb1-9"></span>
<span id="cb1-10">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb1-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>):</span>
<span id="cb1-12">    rows.append((</span>
<span id="cb1-13">        fake.first_name(),</span>
<span id="cb1-14">        fake.last_name(),</span>
<span id="cb1-15">        random.randint(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>),</span>
<span id="cb1-16">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fake<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>email()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  "</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># whitespace to clean</span></span>
<span id="cb1-17">        random.choice([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inactive"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pending"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"suspended"</span>]),</span>
<span id="cb1-18">    ))</span>
<span id="cb1-19"></span>
<span id="cb1-20">schema <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StructType([</span>
<span id="cb1-21">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>,  StringType()),</span>
<span id="cb1-22">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>,   StringType()),</span>
<span id="cb1-23">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>,    IntegerType()),</span>
<span id="cb1-24">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>,  StringType()),</span>
<span id="cb1-25">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>, StringType()),</span>
<span id="cb1-26">])</span>
<span id="cb1-27"></span>
<span id="cb1-28">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.createDataFrame(rows, schema)</span>
<span id="cb1-29">df.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+--------+--------+---+-----------------------------+--------+
|first   |last    |age|email                        |status  |
+--------+--------+---+-----------------------------+--------+
|Danielle|Johnson |58 |  john21@example.net         |active  |
|Joy     |Gardner |19 |  fjohnson@example.org       |pending |
|Jesse   |Guzman  |33 |  jennifermiles@example.com  |inactive|
|Jeffrey |Lawrence|26 |  blakeerik@example.com      |active  |
|Matthew |Moore   |61 |  curtis61@example.com       |active  |
+--------+--------+---+-----------------------------+--------+
only showing top 5 rows</code></pre>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="approach-1-.transform-with-per-column-functions" class="level2">
<h2 class="anchored" data-anchor-id="approach-1-.transform-with-per-column-functions">Approach 1: <code>.transform()</code> with Per-Column Functions</h2>
<p>This is the pattern from the original post — each column gets its own function:</p>
<div id="8ff6225b" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_full_name(df):</span>
<span id="cb3-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumn(</span>
<span id="cb3-5">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full_name"</span>, F.concat_ws(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>))</span>
<span id="cb3-6">    )</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> categorize_age(df):</span>
<span id="cb3-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumn(</span>
<span id="cb3-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>,</span>
<span id="cb3-11">        F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"young"</span>)</span>
<span id="cb3-12">         .when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mid"</span>)</span>
<span id="cb3-13">         .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"senior"</span>),</span>
<span id="cb3-14">    )</span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> normalize_email(df):</span>
<span id="cb3-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumn(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email_clean"</span>, F.lower(F.trim(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>))))</span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> clean_status(df):</span>
<span id="cb3-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumn(</span>
<span id="cb3-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_clean"</span>,</span>
<span id="cb3-22">        F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACTIVE"</span>).otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INACTIVE"</span>),</span>
<span id="cb3-23">    )</span>
<span id="cb3-24"></span>
<span id="cb3-25">df_transform <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb3-26">    df</span>
<span id="cb3-27">    .transform(add_full_name)</span>
<span id="cb3-28">    .transform(categorize_age)</span>
<span id="cb3-29">    .transform(normalize_email)</span>
<span id="cb3-30">    .transform(clean_status)</span>
<span id="cb3-31">)</span>
<span id="cb3-32"></span>
<span id="cb3-33">df_transform.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+------------+
|first   |last    |age|email                        |status  |full_name       |age_group|email_clean              |status_clean|
+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+------------+
|Danielle|Johnson |58 |  john21@example.net         |active  |Danielle Johnson|senior   |john21@example.net       |ACTIVE      |
|Joy     |Gardner |19 |  fjohnson@example.org       |pending |Joy Gardner     |young    |fjohnson@example.org     |INACTIVE    |
|Jesse   |Guzman  |33 |  jennifermiles@example.com  |inactive|Jesse Guzman    |mid      |jennifermiles@example.com|INACTIVE    |
|Jeffrey |Lawrence|26 |  blakeerik@example.com      |active  |Jeffrey Lawrence|young    |blakeerik@example.com    |ACTIVE      |
|Matthew |Moore   |61 |  curtis61@example.com       |active  |Matthew Moore   |senior   |curtis61@example.com     |ACTIVE      |
+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+------------+
only showing top 5 rows</code></pre>
</div>
</div>
<div class="callout callout-style-simple callout-warning callout-titled" title="The problem">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>The problem
</div>
</div>
<div class="callout-body-container callout-body">
<p>To understand what columns this adds, you need to read <strong>4 separate function definitions</strong>. The column names and expressions are scattered. Each <code>.transform()</code> wrapping a <code>.withColumn()</code> still creates a separate <code>Project</code> node — 4 nodes total, same as a loop. And the total line count went <strong>up</strong>, not down.</p>
</div>
</div>
</section>
<section id="approach-2-.withcolumns-explicit-and-compact" class="level2">
<h2 class="anchored" data-anchor-id="approach-2-.withcolumns-explicit-and-compact">Approach 2: <code>.withColumns()</code> — Explicit and Compact</h2>
<div id="60107830" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb5-2"></span>
<span id="cb5-3">df_with_columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumns({</span>
<span id="cb5-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full_name"</span>:    F.concat_ws(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>)),</span>
<span id="cb5-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>:    F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"young"</span>)</span>
<span id="cb5-6">                     .when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mid"</span>)</span>
<span id="cb5-7">                     .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"senior"</span>),</span>
<span id="cb5-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email_clean"</span>:  F.lower(F.trim(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>))),</span>
<span id="cb5-9">})</span>
<span id="cb5-10"></span>
<span id="cb5-11">df_with_columns.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+
|first   |last    |age|email                        |status  |full_name       |age_group|email_clean              |
+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+
|Danielle|Johnson |58 |  john21@example.net         |active  |Danielle Johnson|senior   |john21@example.net       |
|Joy     |Gardner |19 |  fjohnson@example.org       |pending |Joy Gardner     |young    |fjohnson@example.org     |
|Jesse   |Guzman  |33 |  jennifermiles@example.com  |inactive|Jesse Guzman    |mid      |jennifermiles@example.com|
|Jeffrey |Lawrence|26 |  blakeerik@example.com      |active  |Jeffrey Lawrence|young    |blakeerik@example.com    |
|Matthew |Moore   |61 |  curtis61@example.com       |active  |Matthew Moore   |senior   |curtis61@example.com     |
+--------+--------+---+-----------------------------+--------+----------------+---------+-------------------------+
only showing top 5 rows</code></pre>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Why this wins">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Why this wins
</div>
</div>
<div class="callout-body-container callout-body">
<p>One call. Every column name and its expression visible at the call site. No function definitions to chase. Fewer lines. More readable.</p>
</div>
</div>
</section>
<section id="when-.transform-is-the-right-tool" class="level2">
<h2 class="anchored" data-anchor-id="when-.transform-is-the-right-tool">When <code>.transform()</code> IS the Right Tool</h2>
<p><code>.transform()</code> is excellent at the <strong>pipeline</strong> grain — composing meaningful DataFrame stages:</p>
<div id="0e77dd38" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql.window <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Window</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> select_latest_per_email(df):</span>
<span id="cb7-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Keep the most recent record per email."""</span></span>
<span id="cb7-6">    w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Window.partitionBy(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>).orderBy(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>).desc())</span>
<span id="cb7-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df</span>
<span id="cb7-8">        .withColumn(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_rn"</span>, F.row_number().over(w))</span>
<span id="cb7-9">        .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_rn = 1"</span>)</span>
<span id="cb7-10">        .drop(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_rn"</span>))</span>
<span id="cb7-11"></span>
<span id="cb7-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> enrich_columns(df):</span>
<span id="cb7-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Add derived columns in one pass."""</span></span>
<span id="cb7-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumns({</span>
<span id="cb7-15">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full_name"</span>:    F.concat_ws(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>)),</span>
<span id="cb7-16">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>:    F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"young"</span>)</span>
<span id="cb7-17">                         .when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mid"</span>)</span>
<span id="cb7-18">                         .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"senior"</span>),</span>
<span id="cb7-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email_clean"</span>:  F.lower(F.trim(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>))),</span>
<span id="cb7-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_clean"</span>: F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACTIVE"</span>)</span>
<span id="cb7-21">                         .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INACTIVE"</span>),</span>
<span id="cb7-22">    })</span>
<span id="cb7-23"></span>
<span id="cb7-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_audit_metadata(df):</span>
<span id="cb7-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Standard audit columns."""</span></span>
<span id="cb7-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.withColumns({</span>
<span id="cb7-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ingested_at"</span>: F.current_timestamp(),</span>
<span id="cb7-28">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source"</span>:      F.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CRM"</span>),</span>
<span id="cb7-29">    })</span>
<span id="cb7-30"></span>
<span id="cb7-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pipeline-level chaining — THIS is the right grain for .transform()</span></span>
<span id="cb7-32">df_final <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb7-33">    df</span>
<span id="cb7-34">    .transform(select_latest_per_email)</span>
<span id="cb7-35">    .transform(enrich_columns)</span>
<span id="cb7-36">    .transform(add_audit_metadata)</span>
<span id="cb7-37">)</span>
<span id="cb7-38"></span>
<span id="cb7-39">df_final.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+-------+---------+---+-----------------------------+---------+---------------+---------+-------------------------+------------+--------------------------+------+
|first  |last     |age|email                        |status   |full_name      |age_group|email_clean              |status_clean|ingested_at               |source|
+-------+---------+---+-----------------------------+---------+---------------+---------+-------------------------+------------+--------------------------+------+
|Clayton|Gray     |47 |  abrown@example.net         |suspended|Clayton Gray   |mid      |abrown@example.net       |INACTIVE    |2026-02-27 19:08:57.959623|CRM   |
|James  |Rodriguez|59 |  adam65@example.net         |suspended|James Rodriguez|senior   |adam65@example.net       |INACTIVE    |2026-02-27 19:08:57.959623|CRM   |
|Tanya  |Riley    |60 |  adkinsbrian@example.net    |pending  |Tanya Riley    |senior   |adkinsbrian@example.net  |INACTIVE    |2026-02-27 19:08:57.959623|CRM   |
|Larry  |Gibson   |35 |  adrianabailey@example.org  |pending  |Larry Gibson   |mid      |adrianabailey@example.org|INACTIVE    |2026-02-27 19:08:57.959623|CRM   |
|Kelly  |Raymond  |73 |  agarcia@example.net        |pending  |Kelly Raymond  |senior   |agarcia@example.net      |INACTIVE    |2026-02-27 19:08:57.959623|CRM   |
+-------+---------+---+-----------------------------+---------+---------------+---------+-------------------------+------------+--------------------------+------+
only showing top 5 rows</code></pre>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Best of both">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Best of both
</div>
</div>
<div class="callout-body-container callout-body">
<p>Each <code>.transform()</code> function is a meaningful pipeline stage — deduplication, enrichment, auditing. Inside each stage, <code>.withColumns()</code> handles the column work. This is composable, readable, and generates an efficient plan.</p>
</div>
</div>
</section>
<section id="what-about-testing" class="level2">
<h2 class="anchored" data-anchor-id="what-about-testing">What About Testing?</h2>
<p>A fair point for <code>.transform()</code>: you can test each function in isolation with a tiny DataFrame. But <code>.withColumns()</code> is equally testable:</p>
<div id="e2ee4d92" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define expressions as reusable variables</span></span>
<span id="cb9-4">_age_group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb9-5">    F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"young"</span>)</span>
<span id="cb9-6">     .when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mid"</span>)</span>
<span id="cb9-7">     .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"senior"</span>)</span>
<span id="cb9-8">)</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test against a 3-row DataFrame — same as you would with a function</span></span>
<span id="cb9-11">test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.createDataFrame([(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>,), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>,), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>,)], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>])</span>
<span id="cb9-12">test_df.select(_age_group.alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>)).show()</span>
<span id="cb9-13"></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or test the full enrichment dict</span></span>
<span id="cb9-15">ENRICHMENT_COLS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb9-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_group"</span>:    _age_group,</span>
<span id="cb9-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_clean"</span>: F.when(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACTIVE"</span>)</span>
<span id="cb9-18">                     .otherwise(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INACTIVE"</span>),</span>
<span id="cb9-19">}</span>
<span id="cb9-20"></span>
<span id="cb9-21">test_df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.createDataFrame(</span>
<span id="cb9-22">    [(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inactive"</span>)],</span>
<span id="cb9-23">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>],</span>
<span id="cb9-24">)</span>
<span id="cb9-25">test_df2.withColumns(ENRICHMENT_COLS).show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+---------+
|age_group|
+---------+
|    young|
|      mid|
|   senior|
+---------+

+---+--------+---------+------------+
|age|  status|age_group|status_clean|
+---+--------+---------+------------+
| 25|  active|    young|      ACTIVE|
| 45|inactive|      mid|    INACTIVE|
+---+--------+---------+------------+
</code></pre>
</div>
</div>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>Extract expressions to variables or the dict to a constant — both are testable with a 3-row dummy DataFrame.</p>
</div>
</div>
</div>
</section>
<section id="references-further-reading" class="level2">
<h2 class="anchored" data-anchor-id="references-further-reading">References &amp; Further Reading</h2>
<ul>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumns.html">PySpark DataFrame.withColumns API</a></li>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.transform.html">PySpark DataFrame.transform API</a></li>
<li><a href="../../tips/Spark/withColumns.html">From 20 withColumn() Calls to 1 withColumns()</a></li>
<li><a href="../../tips/Spark/withColumnsRenamed.html">Rename Multiple Columns with withColumnsRenamed()</a></li>
</ul>
<div class="dd-bottom-cta">
<h3 class="anchored">
Subscribe for daily Databricks tips
</h3>
<p>
Get practical examples and concise updates delivered to your inbox.
</p>
<form action="https://dev.us21.list-manage.com/subscribe/post?u=acde8b752414ef5c69a9ed1b4&amp;id=1fc9e5bce7&amp;f_id=00b8dde6f0" method="post" target="_blank">
<input type="email" name="EMAIL" required="" placeholder="Email address"> <input type="text" name="FNAME" placeholder="First name (optional)"> <button type="submit">Subscribe</button>
</form>
</div>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>pyspark</category>
  <category>Spark</category>
  <category>best-practices</category>
  <guid>DailyDatabricks.Tips/tips/Spark/TransformVsWithColumns.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>From 20 withColumn() Calls to 1 withColumns()</title>
  <link>DailyDatabricks.Tips/tips/Spark/withColumns.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Live Execution">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Live Execution
</div>
</div>
<div class="callout-body-container callout-body">
<p>All code in this post is rendered against a Databricks workspace before publication.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li>Calling <code>.withColumn()</code> in a loop creates a new DataFrame per iteration, generating deeply nested logical plans that slow down the Catalyst optimizer</li>
<li><code>.withColumns()</code> (Spark 3.3+ / DBR 12.0+) accepts a dictionary of <code>{column_name: expression}</code> and applies all transformations in a single plan step</li>
<li>Real-world use cases: metadata enrichment, column standardisation, acronym expansion, cleaning messy column names</li>
</ul>
</section>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>One of the most common PySpark anti-patterns is wrapping <code>.withColumn()</code> in a <code>for</code> loop. It reads well, feels Pythonic, and works… until it doesn’t.</p>
<p>Every call to <code>.withColumn()</code> returns a <strong>brand-new DataFrame</strong> object. Spark stacks each transformation as a separate <code>Project</code> node in the logical plan. With 50 columns in your loop, that’s 50 nested <code>Project</code> nodes the Catalyst optimizer has to traverse, analyze, and optimize through. With hundreds of columns (common in wide fact tables or IoT data) you can even hit a <code>StackOverflowError</code> because plan tree traversal is recursive.</p>
<p><code>.withColumns()</code> was introduced in PySpark 3.3 to solve exactly this. One dictionary in, one plan node out.</p>
<pre><code>df.withColumns(colsMap: dict[str, Column]) -&gt; DataFrame</code></pre>
<div class="callout callout-style-default callout-note callout-titled" title="Setup: Create Sample Data">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Setup: Create Sample Data
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Every example below runs against this <code>df</code>, a realistic messy dataset that mirrors what you’d typically get from a legacy source system.</p>
<p>The column names are intentionally inconsistent: <code>CustID</code> (PascalCase), <code>custName</code> (camelCase), <code>trnx_date</code> (abbreviated snake_case), <code>trnxAmt</code> (camelCase + abbreviation), <code>ACCT-bal</code> (UPPER + hyphen), <code>pmt$Status</code> (dollar sign), <code>addr.Line1</code> (dot + PascalCase). This is what real source data can look like.</p>
<div id="38fd5f57" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> faker <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Faker</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql.types <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb2-5"></span>
<span id="cb2-6">fake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Faker()</span>
<span id="cb2-7">Faker.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-8">random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate 1000 rows of realistic messy data</span></span>
<span id="cb2-11">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>):</span>
<span id="cb2-13">    rows.append((</span>
<span id="cb2-14">        fake.random_int(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99999</span>),          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CustID</span></span>
<span id="cb2-15">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fake<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>name()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  "</span>,                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># custName (with whitespace)</span></span>
<span id="cb2-16">        fake.date_between(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-2y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"today"</span>).isoformat(),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># trnx_date</span></span>
<span id="cb2-17">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(random.uniform(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9999.99</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># trnxAmt (extra decimals)</span></span>
<span id="cb2-18">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(random.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50000.0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ACCT-bal</span></span>
<span id="cb2-19">        random.choice([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pending"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"complete"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"failed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REVERSED"</span>]),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pmt$Status</span></span>
<span id="cb2-20">        fake.street_address(),                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># addr.Line1</span></span>
<span id="cb2-21">    ))</span>
<span id="cb2-22"></span>
<span id="cb2-23">schema <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StructType([</span>
<span id="cb2-24">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>,     IntegerType()),</span>
<span id="cb2-25">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"custName"</span>,   StringType()),</span>
<span id="cb2-26">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date"</span>,  StringType()),</span>
<span id="cb2-27">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>,    DoubleType()),</span>
<span id="cb2-28">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACCT-bal"</span>,   DoubleType()),</span>
<span id="cb2-29">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt$Status"</span>, StringType()),</span>
<span id="cb2-30">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr.Line1"</span>, StringType()),</span>
<span id="cb2-31">])</span>
<span id="cb2-32"></span>
<span id="cb2-33">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.createDataFrame(rows, schema)</span>
<span id="cb2-34">df.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+------+---------------------+----------+---------+--------+----------+------------------------------+
|CustID|custName             |trnx_date |trnxAmt  |ACCT-bal|pmt$Status|addr.Line1                    |
+------+---------------------+----------+---------+--------+----------+------------------------------+
|93810 |  Patrick Sanchez    |2024-05-30|6396.0645|763.04  |failed    |819 Johnson Course            |
|38657 |  Lance Hoffman      |2025-07-25|2452.6916|6546.67 |pending   |79402 Peterson Drives Apt. 511|
|59797 |  Ryan Munoz         |2025-09-30|6768.6046|44555.07|pending   |161 Calderon River Suite 931  |
|16006 |  Patricia Galloway  |2025-11-04|5906.9668|1105.03 |pending   |4752 Kelly Skyway             |
|44993 |  Melinda Jones      |2025-05-27|2190.2844|25020.44|pending   |76483 Cameron Trail           |
+------+---------------------+----------+---------+--------+----------+------------------------------+
only showing top 5 rows</code></pre>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="code-examples" class="level1">
<h1>Code Examples</h1>
<section id="the-anti-pattern" class="level3">
<h3 class="anchored" data-anchor-id="the-anti-pattern">The Anti-Pattern</h3>
<div id="dd333d28" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each iteration creates a new DataFrame with a deeper logical plan</span></span>
<span id="cb4-4">columns_to_add <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date_upper"</span>:   F.upper(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date"</span>)),</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cust_name_trimmed"</span>:  F.trim(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"custName"</span>)),</span>
<span id="cb4-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acct_balance_abs"</span>:   F.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`ACCT-bal`"</span>)),</span>
<span id="cb4-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"txn_amt_rounded"</span>:    F.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb4-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"load_timestamp"</span>:     F.current_timestamp(),</span>
<span id="cb4-10">}</span>
<span id="cb4-11"></span>
<span id="cb4-12">df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df</span>
<span id="cb4-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col_name, expression <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> columns_to_add.items():</span>
<span id="cb4-14">    df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_result.withColumn(col_name, expression)</span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5 iterations = 5 nested Project nodes in the plan</span></span>
<span id="cb4-16"></span>
<span id="cb4-17">df_result.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
|CustID|custName             |trnx_date |trnxAmt  |ACCT-bal|pmt$Status|addr.Line1                    |trnx_date_upper|cust_name_trimmed|acct_balance_abs|txn_amt_rounded|load_timestamp            |
+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
|93810 |  Patrick Sanchez    |2024-05-30|6396.0645|763.04  |failed    |819 Johnson Course            |2024-05-30     |Patrick Sanchez  |763.04          |6396.06        |2026-02-18 12:13:29.416136|
|38657 |  Lance Hoffman      |2025-07-25|2452.6916|6546.67 |pending   |79402 Peterson Drives Apt. 511|2025-07-25     |Lance Hoffman    |6546.67         |2452.69        |2026-02-18 12:13:29.416136|
|59797 |  Ryan Munoz         |2025-09-30|6768.6046|44555.07|pending   |161 Calderon River Suite 931  |2025-09-30     |Ryan Munoz       |44555.07        |6768.6         |2026-02-18 12:13:29.416136|
|16006 |  Patricia Galloway  |2025-11-04|5906.9668|1105.03 |pending   |4752 Kelly Skyway             |2025-11-04     |Patricia Galloway|1105.03         |5906.97        |2026-02-18 12:13:29.416136|
|44993 |  Melinda Jones      |2025-05-27|2190.2844|25020.44|pending   |76483 Cameron Trail           |2025-05-27     |Melinda Jones    |25020.44        |2190.28        |2026-02-18 12:13:29.416136|
+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
only showing top 5 rows</code></pre>
</div>
</div>
</section>
<section id="the-fix-.withcolumns" class="level3">
<h3 class="anchored" data-anchor-id="the-fix-.withcolumns">The Fix: <code>.withColumns()</code></h3>
<div id="29f6ac31" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Single plan node, single pass, same result</span></span>
<span id="cb6-4">df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumns({</span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date_upper"</span>:   F.upper(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date"</span>)),</span>
<span id="cb6-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cust_name_trimmed"</span>:  F.trim(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"custName"</span>)),</span>
<span id="cb6-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acct_balance_abs"</span>:   F.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`ACCT-bal`"</span>)),</span>
<span id="cb6-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"txn_amt_rounded"</span>:    F.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb6-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"load_timestamp"</span>:     F.current_timestamp(),</span>
<span id="cb6-10">})</span>
<span id="cb6-11"></span>
<span id="cb6-12">df_result.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
|CustID|custName             |trnx_date |trnxAmt  |ACCT-bal|pmt$Status|addr.Line1                    |trnx_date_upper|cust_name_trimmed|acct_balance_abs|txn_amt_rounded|load_timestamp            |
+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
|93810 |  Patrick Sanchez    |2024-05-30|6396.0645|763.04  |failed    |819 Johnson Course            |2024-05-30     |Patrick Sanchez  |763.04          |6396.06        |2026-02-18 12:13:31.643766|
|38657 |  Lance Hoffman      |2025-07-25|2452.6916|6546.67 |pending   |79402 Peterson Drives Apt. 511|2025-07-25     |Lance Hoffman    |6546.67         |2452.69        |2026-02-18 12:13:31.643766|
|59797 |  Ryan Munoz         |2025-09-30|6768.6046|44555.07|pending   |161 Calderon River Suite 931  |2025-09-30     |Ryan Munoz       |44555.07        |6768.6         |2026-02-18 12:13:31.643766|
|16006 |  Patricia Galloway  |2025-11-04|5906.9668|1105.03 |pending   |4752 Kelly Skyway             |2025-11-04     |Patricia Galloway|1105.03         |5906.97        |2026-02-18 12:13:31.643766|
|44993 |  Melinda Jones      |2025-05-27|2190.2844|25020.44|pending   |76483 Cameron Trail           |2025-05-27     |Melinda Jones    |25020.44        |2190.28        |2026-02-18 12:13:31.643766|
+------+---------------------+----------+---------+--------+----------+------------------------------+---------------+-----------------+----------------+---------------+--------------------------+
only showing top 5 rows</code></pre>
</div>
</div>
</section>
<section id="real-world-standardise-messy-columns-and-expand-abbreviations" class="level3">
<h3 class="anchored" data-anchor-id="real-world-standardise-messy-columns-and-expand-abbreviations">Real-World: Standardise Messy Columns and Expand Abbreviations</h3>
<p>Our setup DataFrame has columns like <code>CustID</code>, <code>trnxAmt</code>, <code>ACCT-bal</code>, <code>pmt$Status</code>, <code>addr.Line1</code>. Mixed casing, special characters, and cryptic abbreviations everywhere. Here’s how to clean them all in one pass:</p>
<div id="d2e0b83e" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb8-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> re</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> to_snake_case(name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb8-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Convert any naming convention to snake_case."""</span></span>
<span id="cb8-6">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[.$</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\-</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@#!]</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, name)            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># special chars to _</span></span>
<span id="cb8-7">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[a-z]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[A-Z]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\2</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, name)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># camelCase split</span></span>
<span id="cb8-8">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{2,}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, name)                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># collapse doubles</span></span>
<span id="cb8-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> name.lower().strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>)</span>
<span id="cb8-10"></span>
<span id="cb8-11">ABBREVIATIONS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb8-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transaction"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cust"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer"</span>,</span>
<span id="cb8-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acct"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"account"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payment"</span>,</span>
<span id="cb8-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"address"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amt"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amount"</span>,</span>
<span id="cb8-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bal"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"balance"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qty"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantity"</span>,</span>
<span id="cb8-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"desc"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dt"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>,</span>
<span id="cb8-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nbr"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number"</span>,      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cd"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span>,</span>
<span id="cb8-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nm"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>,</span>
<span id="cb8-19">}</span>
<span id="cb8-20"></span>
<span id="cb8-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> expand_abbreviations(name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb8-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Expand common data abbreviations in a column name."""</span></span>
<span id="cb8-23">    parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> name.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>)</span>
<span id="cb8-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>.join(ABBREVIATIONS.get(p, p) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> parts)</span>
<span id="cb8-25"></span>
<span id="cb8-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build the transformation dict dynamically</span></span>
<span id="cb8-27">transformations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb8-28"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.columns:</span>
<span id="cb8-29">    clean_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expand_abbreviations(to_snake_case(col_name))</span>
<span id="cb8-30">    transformations[clean_name] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> F.col(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>col_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">`"</span>)</span>
<span id="cb8-31"></span>
<span id="cb8-32">df_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumns(transformations)</span>
<span id="cb8-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_clean.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['CustID', 'custName', 'trnx_date', 'trnxAmt', 'ACCT-bal', 'pmt$Status', 'addr.Line1', 'customer_id', 'customer_name', 'transaction_date', 'transaction_amount', 'account_balance', 'payment_status', 'address_line1']</code></pre>
</div>
</div>
</section>
<section id="real-world-metadata-enrichment-in-one-pass" class="level3">
<h3 class="anchored" data-anchor-id="real-world-metadata-enrichment-in-one-pass">Real-World: Metadata Enrichment in One Pass</h3>
<p>Adding audit columns is one of the most common ETL tasks. Do it in one call instead of five:</p>
<div id="c6e2bbb8" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> functions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb10-2"></span>
<span id="cb10-3">pipeline_run_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"run-2026-02-16-001"</span></span>
<span id="cb10-4"></span>
<span id="cb10-5">df_enriched <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumns({</span>
<span id="cb10-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ingestion_timestamp"</span>: F.current_timestamp(),</span>
<span id="cb10-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source_system"</span>:       F.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CRM_PROD"</span>),</span>
<span id="cb10-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pipeline_run_id"</span>:     F.lit(pipeline_run_id),</span>
<span id="cb10-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row_hash"</span>:            F.sha2(F.concat_ws(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>[F.col(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>c<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">`"</span>).cast(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"string"</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.columns]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>),</span>
<span id="cb10-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is_valid"</span>: F.when(</span>
<span id="cb10-11">        F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>).isNotNull() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (F.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb10-12">    ).otherwise(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>),</span>
<span id="cb10-13">})</span>
<span id="cb10-14"></span>
<span id="cb10-15">df_enriched.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ingestion_timestamp"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source_system"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row_hash"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is_valid"</span>).show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>+------+--------------------------+-------------+----------------------------------------+--------+
|CustID|       ingestion_timestamp|source_system|                                row_hash|is_valid|
+------+--------------------------+-------------+----------------------------------------+--------+
| 93810|2026-02-18 12:13:32.732663|     CRM_PROD|0a16938d05f57ecdac3a93290a6b091113435...|    true|
| 38657|2026-02-18 12:13:32.732663|     CRM_PROD|2852cc60ce03fa94327046eaa23647536574e...|    true|
| 59797|2026-02-18 12:13:32.732663|     CRM_PROD|8fdc5170c3c067b621370b9b015f84dd4e66c...|    true|
| 16006|2026-02-18 12:13:32.732663|     CRM_PROD|15e0ed67555e8ed6a965c5fa76a6071d3b953...|    true|
| 44993|2026-02-18 12:13:32.732663|     CRM_PROD|7dcd0fb0db5ffe761939d1de92d8b1798783e...|    true|
+------+--------------------------+-------------+----------------------------------------+--------+
only showing top 5 rows</code></pre>
</div>
</div>
</section>
<section id="scala-equivalent" class="level3">
<h3 class="anchored" data-anchor-id="scala-equivalent">Scala Equivalent</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">import</span> org<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>apache<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>spark<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sql<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>functions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>_</span>
<span id="cb12-2"></span>
<span id="cb12-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Scala uses a Map of column name to expression</span></span>
<span id="cb12-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> dfResult <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">withColumns</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb12-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date_upper"</span>   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)),</span></span>
<span id="cb12-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cust_name_trimmed"</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"custName"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)),</span></span>
<span id="cb12-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acct_balance_abs"</span>   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`ACCT-bal`"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)),</span></span>
<span id="cb12-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"txn_amt_rounded"</span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb12-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"load_timestamp"</span>     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_timestamp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb12-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span></span></code></pre></div></div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Check Your Spark Version">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Check Your Spark Version
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>.withColumns()</code> requires PySpark 3.3+. If you’re not using that version yet upgrade your databricks runtime for goodness sake.</p>
</div>
</div>
</section>
</section>
<section id="detail" class="level1">
<h1>Detail</h1>
<section id="why-.withcolumn-in-a-loop-is-costly" class="level2">
<h2 class="anchored" data-anchor-id="why-.withcolumn-in-a-loop-is-costly">Why <code>.withColumn()</code> in a Loop Is Costly</h2>
<p>Each <code>.withColumn()</code> call returns a new <code>DataFrame</code> with a new <code>Project</code> node appended to the unresolved logical plan. With N loop iterations, you get N nested <code>Project</code> nodes.</p>
<p>The Catalyst optimizer has to traverse and optimize through each one. Analysis, resolution, and optimization passes all scale with plan depth. Beyond roughly 100 columns you can hit <code>StackOverflowError</code> because plan tree traversal is recursive. Even before that, planning time can balloon from milliseconds to seconds.</p>
</section>
<section id="how-.withcolumns-fixes-this" class="level2">
<h2 class="anchored" data-anchor-id="how-.withcolumns-fixes-this">How <code>.withColumns()</code> Fixes This</h2>
<ol type="1">
<li><strong>Single Plan Node</strong>: All column expressions merge into one <code>Project</code> node</li>
<li><strong>Faster Optimization</strong>: Catalyst processes one flat set of expressions instead of a deeply nested tree</li>
<li><strong>Cleaner Code</strong>: A dictionary is more readable and maintainable than a loop</li>
<li><strong>Prevents StackOverflow</strong>: Even with hundreds of columns, the plan stays shallow</li>
<li><strong>Idiomatic</strong>: This is the pattern the Spark developers intended for multi-column operations</li>
</ol>
</section>
<section id="when-to-use-.withcolumns" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-.withcolumns">When to Use <code>.withColumns()</code></h2>
<ul>
<li>Adding metadata or audit columns to ETL outputs</li>
<li>Applying type casts or formatting to multiple columns</li>
<li>Building derived columns from business rules</li>
<li>Any time you find yourself writing a loop with <code>.withColumn()</code></li>
</ul>
</section>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<ul>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumns.html">PySpark DataFrame.withColumns API Documentation</a></li>
<li><a href="../../tips/Spark/withColumnsRenamed.html">Companion Tip: Rename Multiple Columns in PySpark with withColumnsRenamed()</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>pyspark</category>
  <category>performance</category>
  <category>Spark</category>
  <guid>DailyDatabricks.Tips/tips/Spark/withColumns.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Rename Multiple Columns in PySpark with withColumnsRenamed()</title>
  <link>DailyDatabricks.Tips/tips/Spark/withColumnsRenamed.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Live Execution">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Live Execution
</div>
</div>
<div class="callout-body-container callout-body">
<p>All code in this post is rendered against a Databricks workspace before publication.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li>Calling <code>.withColumnRenamed()</code> in a loop has the same problem as <code>.withColumn()</code> loops: each call adds a new <code>Project</code> node to the logical plan</li>
<li><code>.withColumnsRenamed()</code> (Spark 3.4+ / DBR 13.0+) takes a dictionary of <code>{old_name: new_name}</code> and applies all renames in a single plan step</li>
<li>Perfect for converting to snake_case, expanding abbreviations, removing special characters, and applying generic naming conventions across entire DataFrames</li>
</ul>
</section>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>This is the companion anti-pattern to <a href="../../tips/Spark/withColumns.html">calling <code>.withColumn()</code> in a loop</a>. If you’ve ever ingested data from vendor feeds, legacy systems, APIs, or CSVs, you know the pain. Column names come in every flavour: <code>CustID</code>, <code>trnxAmt</code>, <code>ACCT-bal</code>, <code>pmt$Status</code>, <code>addr.Line1</code>. The instinct is to write a loop renaming them one by one.</p>
<p>The problem is identical to <code>.withColumn()</code> loops. Each <code>.withColumnRenamed()</code> call returns a new DataFrame with a new <code>Project</code> node stacked on the logical plan. With dozens of renames, the Catalyst optimizer bogs down traversing and optimizing through all those nested nodes.</p>
<p><code>.withColumnsRenamed()</code> was introduced in PySpark 3.4 to solve this. One dictionary mapping old names to new names, one plan node.</p>
<pre><code>df.withColumnsRenamed(colsMap: dict[str, str]) -&gt; DataFrame</code></pre>
<div class="callout callout-style-default callout-note callout-titled" title="Setup: Create Sample Data">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Setup: Create Sample Data
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Every example below runs against this <code>df</code>, a realistic messy dataset with intentionally inconsistent column names.</p>
<p>The column names cover the usual mess: <code>CustID</code> (PascalCase), <code>custName</code> (camelCase), <code>trnx_date</code> (abbreviated snake_case), <code>trnxAmt</code> (camelCase + abbreviation), <code>ACCT-bal</code> (UPPER + hyphen), <code>pmt$Status</code> (dollar sign), <code>addr.Line1</code> (dot + PascalCase). This is what real source data looks like.</p>
<div id="fc4df480" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> faker <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Faker</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyspark.sql.types <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb2-4"></span>
<span id="cb2-5">fake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Faker()</span>
<span id="cb2-6">Faker.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-7">random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate 1000 rows with intentionally messy column names</span></span>
<span id="cb2-10">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>):</span>
<span id="cb2-12">    rows.append((</span>
<span id="cb2-13">        fake.random_int(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99999</span>),</span>
<span id="cb2-14">        fake.name(),</span>
<span id="cb2-15">        fake.date_between(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-2y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"today"</span>).isoformat(),</span>
<span id="cb2-16">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(random.uniform(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9999.99</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb2-17">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(random.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50000.0</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb2-18">        random.choice([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pending"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"complete"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"failed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REVERSED"</span>]),</span>
<span id="cb2-19">        fake.street_address(),</span>
<span id="cb2-20">    ))</span>
<span id="cb2-21"></span>
<span id="cb2-22">schema <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StructType([</span>
<span id="cb2-23">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>,     IntegerType()),</span>
<span id="cb2-24">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"custName"</span>,   StringType()),</span>
<span id="cb2-25">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx_date"</span>,  StringType()),</span>
<span id="cb2-26">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>,    DoubleType()),</span>
<span id="cb2-27">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACCT-bal"</span>,   DoubleType()),</span>
<span id="cb2-28">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt$Status"</span>, StringType()),</span>
<span id="cb2-29">    StructField(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr.Line1"</span>, StringType()),</span>
<span id="cb2-30">])</span>
<span id="cb2-31"></span>
<span id="cb2-32">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spark.createDataFrame(rows, schema)</span>
<span id="cb2-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df.columns)</span>
<span id="cb2-34">df.show(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, truncate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['CustID', 'custName', 'trnx_date', 'trnxAmt', 'ACCT-bal', 'pmt$Status', 'addr.Line1']
+------+-----------------+----------+---------+--------+----------+------------------------------+
|CustID|custName         |trnx_date |trnxAmt  |ACCT-bal|pmt$Status|addr.Line1                    |
+------+-----------------+----------+---------+--------+----------+------------------------------+
|93810 |Patrick Sanchez  |2024-06-01|6396.0645|763.04  |failed    |819 Johnson Course            |
|38657 |Lance Hoffman    |2025-07-27|2452.6916|6546.67 |pending   |79402 Peterson Drives Apt. 511|
|59797 |Ryan Munoz       |2025-10-02|6768.6046|44555.07|pending   |161 Calderon River Suite 931  |
|16006 |Patricia Galloway|2025-11-06|5906.9668|1105.03 |pending   |4752 Kelly Skyway             |
|44993 |Melinda Jones    |2025-05-29|2190.2844|25020.44|pending   |76483 Cameron Trail           |
+------+-----------------+----------+---------+--------+----------+------------------------------+
only showing top 5 rows</code></pre>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="code-examples" class="level1">
<h1>Code Examples</h1>
<section id="the-anti-pattern" class="level3">
<h3 class="anchored" data-anchor-id="the-anti-pattern">The Anti-Pattern</h3>
<div id="8b3f3bb4" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each iteration creates a new DataFrame with a deeper logical plan</span></span>
<span id="cb4-2">rename_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>:     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer_id"</span>,</span>
<span id="cb4-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>:    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transaction_amount"</span>,</span>
<span id="cb4-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACCT-bal"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"account_balance"</span>,</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt$Status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payment_status"</span>,</span>
<span id="cb4-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr.Line1"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"address_line_1"</span>,</span>
<span id="cb4-8">}</span>
<span id="cb4-9"></span>
<span id="cb4-10">df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df</span>
<span id="cb4-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> old_name, new_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> rename_map.items():</span>
<span id="cb4-12">    df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_result.withColumnRenamed(old_name, new_name)</span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5 iterations = 5 nested Project nodes</span></span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_result.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['customer_id', 'custName', 'trnx_date', 'transaction_amount', 'account_balance', 'payment_status', 'address_line_1']</code></pre>
</div>
</div>
</section>
<section id="the-fix-.withcolumnsrenamed" class="level3">
<h3 class="anchored" data-anchor-id="the-fix-.withcolumnsrenamed">The Fix: <code>.withColumnsRenamed()</code></h3>
<div id="a336c828" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Single plan node, single pass, same result</span></span>
<span id="cb6-2">df_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumnsRenamed({</span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>:     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer_id"</span>,</span>
<span id="cb6-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>:    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transaction_amount"</span>,</span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACCT-bal"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"account_balance"</span>,</span>
<span id="cb6-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt$Status"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payment_status"</span>,</span>
<span id="cb6-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr.Line1"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"address_line_1"</span>,</span>
<span id="cb6-8">})</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_result.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['customer_id', 'custName', 'trnx_date', 'transaction_amount', 'account_balance', 'payment_status', 'address_line_1']</code></pre>
</div>
</div>
</section>
<section id="real-world-generic-snake_case-conversion-for-all-columns" class="level3">
<h3 class="anchored" data-anchor-id="real-world-generic-snake_case-conversion-for-all-columns">Real-World: Generic snake_case Conversion for All Columns</h3>
<p>When you want every column in your DataFrame to follow a consistent naming convention, build the rename dictionary dynamically:</p>
<div id="035b43f1" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> re</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> to_snake_case(name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb8-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Convert any naming convention to clean snake_case."""</span></span>
<span id="cb8-5">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[.$</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\-</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@#!&amp;*()+=/</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, name)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># special chars to _</span></span>
<span id="cb8-6">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[a-z0-9]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[A-Z]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\2</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, name)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># camelCase split</span></span>
<span id="cb8-7">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[A-Z]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[A-Z][a-z]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\2</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, name) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ACRONYMWord split</span></span>
<span id="cb8-8">    name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{2,}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, name)                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># collapse doubles</span></span>
<span id="cb8-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> name.lower().strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>)</span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build rename dict from current column names</span></span>
<span id="cb8-12">rename_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {col: to_snake_case(col) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.columns}</span>
<span id="cb8-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(rename_map)</span>
<span id="cb8-14"></span>
<span id="cb8-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply all renames in one pass</span></span>
<span id="cb8-16">df_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumnsRenamed(rename_map)</span>
<span id="cb8-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_clean.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'CustID': 'cust_id', 'custName': 'cust_name', 'trnx_date': 'trnx_date', 'trnxAmt': 'trnx_amt', 'ACCT-bal': 'acct_bal', 'pmt$Status': 'pmt_status', 'addr.Line1': 'addr_line1'}
['cust_id', 'cust_name', 'trnx_date', 'trnx_amt', 'acct_bal', 'pmt_status', 'addr_line1']</code></pre>
</div>
</div>
</section>
<section id="real-world-expand-common-abbreviations" class="level3">
<h3 class="anchored" data-anchor-id="real-world-expand-common-abbreviations">Real-World: Expand Common Abbreviations</h3>
<p>Data teams often inherit column names full of cryptic abbreviations from source systems. Combine snake_case conversion with abbreviation expansion for truly readable schemas:</p>
<div id="810aa699" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> re</span>
<span id="cb10-2"></span>
<span id="cb10-3">ABBREVIATIONS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb10-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnx"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transaction"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cust"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer"</span>,</span>
<span id="cb10-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acct"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"account"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payment"</span>,</span>
<span id="cb10-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"address"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amt"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amount"</span>,</span>
<span id="cb10-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bal"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"balance"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qty"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantity"</span>,</span>
<span id="cb10-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"desc"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dt"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>,</span>
<span id="cb10-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ts"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"timestamp"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nbr"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number"</span>,</span>
<span id="cb10-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"num"</span>:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number"</span>,      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cd"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span>,</span>
<span id="cb10-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nm"</span>:   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>,</span>
<span id="cb10-12">}</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> expand_column_name(name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb10-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""snake_case + expand common abbreviations."""</span></span>
<span id="cb10-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First convert to snake_case</span></span>
<span id="cb10-17">    snake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[.$</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\-</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@#!]</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, name)</span>
<span id="cb10-18">    snake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[a-z0-9]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[A-Z]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\2</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, snake)</span>
<span id="cb10-19">    snake <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{2,}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>, snake).lower().strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>)</span>
<span id="cb10-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then expand abbreviations</span></span>
<span id="cb10-21">    parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> snake.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>)</span>
<span id="cb10-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>.join(ABBREVIATIONS.get(p, p) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> parts)</span>
<span id="cb10-23"></span>
<span id="cb10-24">rename_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {col: expand_column_name(col) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.columns}</span>
<span id="cb10-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(rename_map)</span>
<span id="cb10-26"></span>
<span id="cb10-27">df_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumnsRenamed(rename_map)</span>
<span id="cb10-28"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_clean.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'CustID': 'customer_id', 'custName': 'customer_name', 'trnx_date': 'transaction_date', 'trnxAmt': 'transaction_amount', 'ACCT-bal': 'account_balance', 'pmt$Status': 'payment_status', 'addr.Line1': 'address_line1'}
['customer_id', 'customer_name', 'transaction_date', 'transaction_amount', 'account_balance', 'payment_status', 'address_line1']</code></pre>
</div>
</div>
</section>
<section id="real-world-strip-vendor-prefixes" class="level3">
<h3 class="anchored" data-anchor-id="real-world-strip-vendor-prefixes">Real-World: Strip Vendor Prefixes</h3>
<p>Vendor data often arrives with prefixes like <code>SRC_</code>, <code>RAW_</code>, or <code>STG_</code>. Here we create a prefixed version and strip it back:</p>
<div id="13f97ec2" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First simulate vendor-prefixed data from our setup df</span></span>
<span id="cb12-2">df_vendor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.withColumnsRenamed({col: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"SRC_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>col<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.columns})</span>
<span id="cb12-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Before:"</span>, df_vendor.columns)</span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now strip the prefix in one pass</span></span>
<span id="cb12-6">prefix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SRC_"</span></span>
<span id="cb12-7">rename_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb12-8">    col: col[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(prefix):] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> col.startswith(prefix) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> col</span>
<span id="cb12-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df_vendor.columns</span>
<span id="cb12-10">}</span>
<span id="cb12-11">df_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_vendor.withColumnsRenamed(rename_map)</span>
<span id="cb12-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"After: "</span>, df_clean.columns)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Before: ['SRC_CustID', 'SRC_custName', 'SRC_trnx_date', 'SRC_trnxAmt', 'SRC_ACCT-bal', 'SRC_pmt$Status', 'SRC_addr.Line1']
After:  ['CustID', 'custName', 'trnx_date', 'trnxAmt', 'ACCT-bal', 'pmt$Status', 'addr.Line1']</code></pre>
</div>
</div>
</section>
<section id="scala-equivalent" class="level3">
<h3 class="anchored" data-anchor-id="scala-equivalent">Scala Equivalent</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">import</span> org<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>apache<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>spark<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sql<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>functions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>_</span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Scala uses a Map of old name to new name</span></span>
<span id="cb14-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> dfResult <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">withColumnsRenamed</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb14-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CustID"</span>     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customer_id"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb14-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trnxAmt"</span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transaction_amount"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb14-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACCT-bal"</span>   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"account_balance"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb14-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pmt$Status"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payment_status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb14-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"addr.Line1"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"address_line_1"</span></span>
<span id="cb14-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span></span></code></pre></div></div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Check Your Spark Version">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Check Your Spark Version
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>.withColumnsRenamed()</code> requires PySpark 3.4+ (Databricks Runtime 13.0+).</p>
</div>
</div>
</section>
</section>
<section id="detail" class="level1">
<h1>Detail</h1>
<section id="the-same-problem-different-method" class="level2">
<h2 class="anchored" data-anchor-id="the-same-problem-different-method">The Same Problem, Different Method</h2>
<p><code>.withColumnRenamed()</code> suffers from exactly the same plan-nesting issue as <code>.withColumn()</code>. Each call returns a new DataFrame with a new <code>Project</code> node appended to the logical plan. See <a href="../../tips/Spark/withColumns.html">the companion tip on .withColumns()</a> for the full Catalyst optimizer breakdown.</p>
<p>The key difference is that <code>.withColumnsRenamed()</code> works with strings only, no <code>Column</code> expressions needed, making it even simpler to use.</p>
</section>
<section id="common-rename-patterns" class="level2">
<h2 class="anchored" data-anchor-id="common-rename-patterns">Common Rename Patterns</h2>
<ul>
<li><strong>snake_case conversion</strong>: Teams following Python naming conventions</li>
<li><strong>Abbreviation expansion</strong>: Improved readability and self-documenting schemas</li>
<li><strong>Prefix/suffix stripping</strong>: Vendor data normalisation</li>
<li><strong>Special character removal</strong>: Compatibility with downstream systems (Parquet, Delta, BI tools that choke on dots, dollar signs, or hyphens in column names)</li>
</ul>
</section>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<ul>
<li><a href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumnsRenamed.html">PySpark DataFrame.withColumnsRenamed API Documentation</a></li>
<li><a href="../../tips/Spark/withColumns.html">Companion Tip: From 20 withColumn() Calls to 1 withColumns()</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>pyspark</category>
  <category>performance</category>
  <category>Spark</category>
  <guid>DailyDatabricks.Tips/tips/Spark/withColumnsRenamed.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Notebook Context</title>
  <link>DailyDatabricks.Tips/tips/Notebook/context.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Beginner Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Beginner Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This tip is part of the <a href="../../#tag=basic">basic tips series</a> to level up your work with high impact, low effort changes.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li>Get Metadata &amp; Context on your current notebook environment</li>
<li>User Credentials (email, IPs, azuread object ids)</li>
<li>Browser details &amp; language</li>
<li>Cluster Ids</li>
</ul>
</section>
<section id="code" class="level1">
<h1>Code</h1>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Scala</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">R</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="get-context-object" class="level3">
<h3 class="anchored" data-anchor-id="get-context-object">Get Context Object</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">dbutils.notebook.entry_point.getDbutils().notebook().getContext()</span></code></pre></div></div>
</section>
<section id="example-get-notebook-path-property" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"></span>
<span id="cb2-2">dbutils.notebook.entry_point.getDbutils().notebook().getContext() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-3">  .notebookPath() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-4">  .toString() </span></code></pre></div></div>
</section>
<section id="convert-context-json-object-to-dictionary" class="level3">
<h3 class="anchored" data-anchor-id="convert-context-json-object-to-dictionary">Convert Context Json Object to Dictionary</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb3-2">json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())</span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<section id="get-context-object-1" class="level3">
<h3 class="anchored" data-anchor-id="get-context-object-1">Get Context Object</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb4-1">dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span></code></pre></div></div>
</section>
<section id="example-get-notebook-path-property-1" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property-1">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb5-1"></span>
<span id="cb5-2">dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>notebookPath \</span>
<span id="cb5-3">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> </span></code></pre></div></div>
</section>
<section id="convert-json-object-to-dictionary" class="level3">
<h3 class="anchored" data-anchor-id="convert-json-object-to-dictionary">Convert Json Object to Dictionary</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> context <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Returns MAP[String,String] no need to map it</span></span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>R doesn’t have dbutils.notebook context object. However, there are several objects located in the R global environment which have properties from the databricks notebook context. We can view these by using the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ls">LS</a> command with pattern matching</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"></span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List only the objects that begin with "spark"</span></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^spark"</span>)</span></code></pre></div></div>
<section id="example-get-notebook-path-property-2" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property-2">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"> spark.databricks.api.url  </span></code></pre></div></div>
</section>
<section id="sample-list-of-properties" class="level3">
<h3 class="anchored" data-anchor-id="sample-list-of-properties">Sample List of properties</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.api.url"</span>                                              </span>
<span id="cb9-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.driverNfs.clusterWideVirtualEnv"</span>                      </span>
<span id="cb9-3">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.env"</span>                                                  </span>
<span id="cb9-4">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.inherited.credentials.keys.spark.databricks.api.token"</span></span>
<span id="cb9-5">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.notebook.id"</span>                                          </span>
<span id="cb9-6">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.notebook.path"</span>                                        </span>
<span id="cb9-7">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.replId"</span>                                               </span>
<span id="cb9-8">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.token"</span>                                                </span>
<span id="cb9-9">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.scheduler.pool"</span>     </span>
<span id="cb9-10">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orgId"</span></span></code></pre></div></div>
</section>
<section id="turn-the-spark-variables-into-a-key-value-list" class="level3">
<h3 class="anchored" data-anchor-id="turn-the-spark-variables-into-a-key-value-list">Turn the spark variables into a Key Value list</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the names of objects that begin with "spark"</span></span>
<span id="cb10-2">spark_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^spark"</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the values of these objects</span></span>
<span id="cb10-5">spark_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mget</span>(spark_names)</span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert the named vector to a list</span></span>
<span id="cb10-8">spark_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(spark_values)</span></code></pre></div></div>
</section>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-caution callout-titled" title="Sample Output">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Sample Output
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Below is a sample object returned from the notebook context object (some details are sanitized for security) and this is not an exhaustive output of the context output</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb11-1"></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'rootRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">None</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-3"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'currentRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">None</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-4"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jobGroup'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3390617077668218965</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7460294295175766547</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89e1</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">d0119f914af39bafdb017336882e'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-5"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tags'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServerBackend</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-54787e706</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ebf268a'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-6">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'shardName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'az-eastus-c3'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-7">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'com.databricks.backend.common.rpc.InternalDriverBackendMessages$StartRepl'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-8">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterMemory'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8192</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-9">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'serverBackendName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'com.databricks.backend.daemon.driver.DriverCorral'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-10">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebookId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-11">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'projectName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'driver'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-12">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tier'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tier-multitenant'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-13">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'eventWindowTime'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2254196.899999991</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-14">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'httpTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/notebook/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/command/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3362446343388373</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-15">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'commandRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'dddd47ee</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-68</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fe</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fd2</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-9042</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-fbde8fd28a8e'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-16">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'buildHash'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">''</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-17">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'workspaceRoutingTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'null'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-18">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHash'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'#notebook/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/command/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3362446343388373</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-19">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'host'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.139</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">64.4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-20">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserPathName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-21">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebookLanguage'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sql'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-22">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'workspaceRoutingBucket'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'null'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-23">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sparkVersion'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.1</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.x-scala2.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-24">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'hostName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'cons-webapp</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-25">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'httpMethod'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'POST'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-26">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserIdleTime'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">411</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-27">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jettyRpcJettyVersion'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-28">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserLanguage'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'en-GB'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-29">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserTabId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fc6c79</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-18</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">df</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ff9-b0e7</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-35</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">df3da8bc08'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-30">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sourceIpAddress'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;AN_IP&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-31">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'loadedUiVersions'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Map(monolith</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">f03dcdb1e979dfefcf2f878284c318bd21970a5d)'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-32">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserUserAgent'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Mozilla/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(Windows</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">NT</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Win64;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">x64)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">AppleWebKit/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">537.36</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(KHTML</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">like</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Gecko)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Chrome/109.0.0.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Safari/537.36',</span></span>
<span id="cb11-33">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'orgId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2675080094955785</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-34">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'userAgent'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Mozilla/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(Windows</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">NT</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Win64;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">x64)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">AppleWebKit/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">537.36</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(KHTML</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">like</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Gecko)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Chrome/109.0.0.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Safari/537.36',</span></span>
<span id="cb11-35">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'0120</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-154009-7</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">bof0jyo'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-36">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'serverEventId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'CgwIusXzngYQ9+mEqAIQgAQYiQE6EKeKn3/v7UOlvUQlALPm2wI='</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-37">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'rootOpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServiceMain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ef19d61f07f0002'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-38">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'requestIdWasMissing'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-39">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sessionId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'d9be357a4121697a1de52d1c3b447874315900e2b5a5054b8aa92576f1e865be'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-40">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterCreator'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;EMAIL_ADDRESS&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-41">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'originatedFromEnvoy'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-42">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientBranchName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'webapp_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-27</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.45</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z_master_b5b81920_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1040710074</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-43">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientTimestamp'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1675421567053</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-44">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'spot'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-45">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'requestId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'CgwIusXzngYQ4/r7pwIQgAQYiQE6EKeKn3/v7UOlvUQlALPm2wI='</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-46">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHasFocus'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-47">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'userId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8925728005363108</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-48">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserIsHidden'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'false'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-49">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'principalIdpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;GUID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-50">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientLocale'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'en'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-51">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'branchName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'webapp_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-27</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.45</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z_master_b5b81920_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1040710074</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-52">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServerBackend'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-53">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sourcePortNumber'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-54">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'user'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;EMAIL_ADDRESS&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-55">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'principalIdpObjectId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;GUID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-56">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHostName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;WORKSPACE&gt;.azuredatabricks.net'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-57">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'parentOpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'RPCClient</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ef19d61f07f11ae'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-58">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jettyRpcType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'InternalDriverBackendMessages$DriverBackendRequest'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb11-59"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'extraContext'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'allowStdin'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-60">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'non_uc_api_token'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">''</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-61">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'commandResultJsonMaxBytes'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20971520</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-62">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebook_path'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/sqlsdamples'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-63">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'thresholdForStoringInDbfs'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-64">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'enableStoringResultsInDbfs'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-65">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'api_url'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'https://eastus-c3.azuredatabricks.net'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-66">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'aclPathOfAclRoot'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/workspace/&lt;ID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-67">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'api_token'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">REDACTED</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb11-68"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'credentialKeys'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'adls_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-69">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'adls_gen2_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-70">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'synapse_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-71">  </span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Capture your current notebook cell">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Capture your current notebook cell
</div>
</div>
<div class="callout-body-container callout-body">
<p>See how you can use the <a href="../../tips/Notebook/context.html">Notebook Context</a> to capture the <a href="">current cell of your notebook</a> and leverage it in your applications and logs <a href="">here</a></p>
</div>
</div>
</section>
<section id="detail" class="level1">
<h1>Detail</h1>
<p>The code snippet <code>dbutils.notebook.entry_point.getDbutils().notebook().getContext()</code> may appear intimidating at first glance, but fret not, let’s break it down:</p>
<ol type="1">
<li><p><strong>dbutils</strong>: This is a utility library in Databricks, providing a collection of utility functions and classes that help in interacting with Databricks functionalities.</p></li>
<li><p><strong>notebook</strong>: This refers to the notebook module within <code>dbutils</code>, which contains methods for interacting with the notebook environment.</p></li>
<li><p><strong>entry_point</strong>: The <code>entry_point</code> attribute within the notebook module refers to the starting point of the notebook execution.</p></li>
<li><p><strong>getDbutils</strong>: This is a method call which returns an instance of <code>dbutils</code>.</p></li>
<li><p><strong>notebook()</strong>: This method call, following <code>getDbutils</code>, returns an instance of the notebook module.</p></li>
<li><p><strong>getContext()</strong>: Finally, this method retrieves the current Notebook Context, encapsulating information like notebook path, user, and other contextual details.</p></li>
</ol>
<p>Benefits of Accessing Notebook Context: Accessing the Notebook Context can serve various purposes:</p>
<ul>
<li><p><strong>Identifying User Information</strong>: By accessing the Notebook Context, you can retrieve information about the user executing the notebook. This could be useful for auditing or personalizing the notebook behavior based on the user.</p></li>
<li><p><strong>Fetching Notebook Path</strong>: If your logic depends on the particular notebook or its location within the Databricks workspace, accessing the Notebook Context is invaluable.</p></li>
<li><p><strong>Managing Execution Flow</strong>: Utilizing the Notebook Context can aid in managing the flow of notebook execution, especially in complex, multi-notebook workflows.</p></li>
</ul>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<section id="related-tweets" class="level3">
<h3 class="anchored" data-anchor-id="related-tweets">Related Tweets</h3>
<blockquote class="twitter-tweet blockquote" data-dnt="true" data-theme="light">
<p lang="en" dir="ltr">
</p><p>⚠ Productionising your <a href="https://twitter.com/databricks?ref_src=twsrc%5Etfw"><span class="citation" data-cites="Databricks">@Databricks</span></a> workloads<br><br>This is an essential tip to enhance your logging and monitoring‼<br><br>❔Did you know you can capture context &amp; metadata in <a href="https://twitter.com/hashtag/databricks?src=hash&amp;ref_src=twsrc%5Etfw">#databricks</a> notebook? <br><br>Information such as<br>📔Current Databricks notebook<br>⛏Job Ids<br>👩‍💻User<br>🌐Browser &amp; more🔽</p>
<p></p>
<p>— Daily Databricks (<span class="citation" data-cites="DailyDatabricks">@DailyDatabricks</span>) <a href="https://twitter.com/DailyDatabricks/status/1535319517609459715?ref_src=twsrc%5Etfw">June 10, 2022</a></p>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</section>
<section id="links" class="level3">
<h3 class="anchored" data-anchor-id="links">Links</h3>
<p><a href="https://stackoverflow.com/questions/69158343/how-to-get-databricks-notebook-context-of-child-notebook">Retriving child notebooks context objects</a></p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>context</category>
  <category>notebook</category>
  <category>dbutils</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/context.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
  <media:content url="DailyDatabricks.Tips/tips/Notebook/Assets/notebook-context.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Loading Unit Test Cases Dynamically in a Notebook</title>
  <link>DailyDatabricks.Tips/tips/Notebook/UnitTests.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Beginner Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Beginner Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This tip is part of the <a href="../../#category=Decembricks">Decembricks 2024 series</a> to teach a new tip everyday of december.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li><strong>Dynamic Test Case Discovery</strong>: Automatically loads test cases from the current notebook module.</li>
<li><strong>Reflection Magic</strong>: Uses reflection to introspect classes, searching for <code>unittest.TestCase</code> subclasses.</li>
<li><strong>Test Suite Management</strong>: Organizes tests into a suite for easier management.</li>
</ul>
</section>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Have you ever had to define a unit test suite and got fed up with having to create a list of the unit test classes to include into the suite for running? Well this tip is for you, it looks up the objects defined in the current namespace that are of type UnitTest case.</p>
</section>
<section id="code" class="level1">
<h1>Code</h1>
<section id="load-all-test-classes-from-the-current-notebook" class="level2">
<h2 class="anchored" data-anchor-id="load-all-test-classes-from-the-current-notebook">Load All Test Classes from the Current Notebook</h2>
<div class="callout callout-style-simple callout-warning callout-titled" title="Update August 2025">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Update August 2025
</div>
</div>
<div class="callout-body-container callout-body">
<p>This tip was recently updated due to work going on with the databricks platform it seems the sys.module[<strong>name</strong>] call returns the different module details now. So the below code has been modified to resolve that by using the iPython Users Namespace.</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> unittest</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> load_test_classes_from_notebook():</span>
<span id="cb1-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb1-5">        ns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_ipython().user_ns <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#UserNameSpace</span></span>
<span id="cb1-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">NameError</span>:</span>
<span id="cb1-7">        ns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>()</span>
<span id="cb1-8"></span>
<span id="cb1-9">    suite <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> unittest.TestSuite()</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Limit to classes that look like *your* tests</span></span>
<span id="cb1-12">    nb_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ns.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__name__'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>)</span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, obj <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(ns.items()):</span>
<span id="cb1-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(obj, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">issubclass</span>(obj, unittest.TestCase) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> obj <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> unittest.TestCase:</span>
<span id="cb1-15">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__module__'</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (nb_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>): <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maybe Overkill todo this filtering </span></span>
<span id="cb1-16">                suite.addTests(unittest.TestLoader().loadTestsFromTestCase(obj))</span>
<span id="cb1-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> suite</span>
<span id="cb1-18"></span>
<span id="cb1-19">    </span>
<span id="cb1-20">suite <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_test_classes_from_notebook()</span>
<span id="cb1-21">runner <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> unittest.TextTestRunner(verbosity<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-22">runner.run(suite)</span></code></pre></div></div>
<section id="example-code-tests" class="level3">
<h3 class="anchored" data-anchor-id="example-code-tests">Example Code &amp; Tests</h3>
<section id="example-methods-to-test" class="level4">
<h4 class="anchored" data-anchor-id="example-methods-to-test">Example Methods to Test</h4>
<p>These are some sample functions in python that we will use to illustrate how unit tests work</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add(a, b):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Perform addition of two numeric values."""</span></span>
<span id="cb2-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> multiply(a, b):  </span>
<span id="cb2-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Perform multiplication of two numeric values."""</span></span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b</span></code></pre></div></div>
</section>
<section id="example-unit-test-cases" class="level4">
<h4 class="anchored" data-anchor-id="example-unit-test-cases">Example Unit Test Cases</h4>
<p>These are sample Unit Test Cases</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> unittest</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> TestGenericMathOperations(unittest.TestCase):</span>
<span id="cb3-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> test_addition(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb3-5">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test with numeric values</span></span>
<span id="cb3-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertEqual(add(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Addition of 2 and 3 should equal 5"</span>)</span>
<span id="cb3-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertEqual(add(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Addition of -1 and 5 should equal 4"</span>) </span>
<span id="cb3-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertAlmostEqual(add(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, places<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, msg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Addition of 0.1 and 0.2 should approximate 0.3"</span>)</span>
<span id="cb3-9">        </span>
<span id="cb3-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test with an invalid case</span></span>
<span id="cb3-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertRaises(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">TypeError</span>, msg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adding a number and a string should raise a TypeError"</span>):</span>
<span id="cb3-12">            add(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span>)</span>
<span id="cb3-13">            </span>
<span id="cb3-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> test_multiplication(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb3-15">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test with numeric values  </span></span>
<span id="cb3-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertEqual(multiply(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multiplication of 2 and 3 should equal 6"</span>)</span>
<span id="cb3-17">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertEqual(multiply(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multiplication of -2 and 4 should equal -8"</span>)</span>
<span id="cb3-18">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertAlmostEqual(multiply(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, places<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, msg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multiplying 0.5 and 0.4 should approximate 0.2"</span>)</span>
<span id="cb3-19">        </span>
<span id="cb3-20">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test with an invalid case</span></span>
<span id="cb3-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.assertRaises(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">TypeError</span>, msg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multiplying a number and a string should raise a TypeError"</span>):</span>
<span id="cb3-22">            multiply(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"world"</span>)</span></code></pre></div></div>
</section>
</section>
</section>
</section>
<section id="details" class="level1">
<h1>Details</h1>
<p>In the spirit of enabling flexible test discovery, this snippet takes advantage of reflection to dynamically load all test cases in the users ipython notebook namespace. Let’s dive into the finer details:</p>
<ol type="1">
<li><p><strong><code>TestSuite</code> Setup</strong>: We initialize an empty <code>TestSuite</code> to collect individual test cases.</p></li>
<li><p><strong>Current Namespace Context</strong>: Accessing <code>get_ipython().user_ns</code> grants a reference to the current namespace, a key tool for reflection.</p></li>
<li><p><strong>Reflection and Filtering</strong>:</p>
<ul>
<li>The <code>dir</code> function retrieves all objects in the module.<br>
</li>
<li><code>getattr</code> allows accessing the object itself by name.</li>
<li>We then ensure the object is a subclass of <code>unittest.TestCase</code> but not the base class itself.</li>
</ul></li>
<li><p><strong>Loading Tests</strong>: <code>unittest.TestLoader().loadTestsFromTestCase</code> loads tests from each discovered class, and the suite aggregates them.</p></li>
</ol>
</section>
<section id="benefits" class="level1">
<h1>Benefits</h1>
<ul>
<li><strong>Automation and Flexibility</strong>: Automatically loading tests simplifies running new tests without manual suite updates.</li>
<li><strong>Dynamic Testing</strong>: Great for scenarios where test classes change frequently or are dynamically generated.</li>
</ul>
</section>
<section id="cautions" class="level1">
<h1>Cautions</h1>
<ul>
<li><strong>Over-Discovery</strong>: This method could inadvertently load experimental or partial test classes, leading to unintended test runs.</li>
<li><strong>Opaque Testing</strong>: The reliance on reflection makes the test suite implicit and less readable. Reviewers may struggle to understand the source of loaded tests.</li>
<li><strong>Performance</strong>: The reflective lookup over the entire module can be inefficient, especially in larger projects.</li>
<li><em>pytest:</em> Is more likely to align with your requirements and use concepts like fixtures to support you.</li>
</ul>
<section id="links" class="level3">
<h3 class="anchored" data-anchor-id="links">Links</h3>
<ul>
<li><a href="https://docs.python.org/3/library/unittest.html">unittest Documentation</a>: Learn more about Python’s built-in testing framework.</li>
</ul>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>testing</category>
  <category>unittest</category>
  <category>reflection</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/UnitTests.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Retrieving Notebook Bindings Dynamically</title>
  <link>DailyDatabricks.Tips/tips/Notebook/bindings.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Learn More">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Learn More
</div>
</div>
<div class="callout-body-container callout-body">
<p>Learn More about Notebooks and <a href="../../tips/Notebook/context.html">Context Objects</a></p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li><strong>Overview</strong>: An introduction to Databricks widgets, their usage, and how to retrieve their values.</li>
<li><strong>Dynamic Bindings</strong>: Explanation of dynamically fetching notebook bindings and when this is appropriate.</li>
<li><strong>Run Parameters</strong>: How to get job parameters when a notebook is executed as a job.</li>
</ul>
<section id="understanding-databricks-widgets" class="level2">
<h2 class="anchored" data-anchor-id="understanding-databricks-widgets">Understanding Databricks Widgets</h2>
<p>Databricks widgets are UI components that enable users to input parameters interactively when running notebooks. They are essential for parameterizing notebooks and making them more dynamic and reusable.</p>
<p>Widgets can be created using <code>dbutils.widgets</code> and their values can be retrieved using <code>dbutils.widgets.getArguments</code>. Here is an example of creating and retrieving a widget value:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">dbutils.widgets.text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default"</span>)</span>
<span id="cb1-2">input_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils.widgets.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>)</span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The value of the input widget is: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>input_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</section>
<section id="retrieving-notebook-bindings-dynamically" class="level2">
<h2 class="anchored" data-anchor-id="retrieving-notebook-bindings-dynamically">Retrieving Notebook Bindings Dynamically</h2>
<section id="using-job-parameters" class="level3">
<h3 class="anchored" data-anchor-id="using-job-parameters">Using Job Parameters</h3>
<p>When a notebook is run as part of a Databricks job, job parameters can be fetched as a dictionary using the <code>dbutils</code> package. This method is useful for dynamically retrieving parameters and making notebooks more adaptable to different environments and use cases.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">run_parameters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils.notebook.entry_point.getCurrentBindings()</span>
<span id="cb2-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(run_parameters)</span></code></pre></div></div>
<p>For example, if the job parameters were <code>{"foo": "bar"}</code>, the result of the code above would be the dictionary <code>{'foo': 'bar'}</code>. Note that Databricks only allows job parameter mappings of <code>str</code> to <code>str</code>, so keys and values will always be strings.</p>
<div class="callout callout-style-simple callout-warning callout-titled" title="Note">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If the notebook is run interactively (not as a job), the dictionary will be empty.</p>
</div>
</div>
<p>The <code>getCurrentBindings()</code> method can also retrieve any active widget values for the notebook when run interactively.</p>
</section>
</section>
<section id="practical-applications" class="level2">
<h2 class="anchored" data-anchor-id="practical-applications">Practical Applications</h2>
<section id="when-to-use-dynamic-bindings" class="level3">
<h3 class="anchored" data-anchor-id="when-to-use-dynamic-bindings">When to Use Dynamic Bindings</h3>
<p>Dynamic bindings are particularly useful in the following scenarios:</p>
<ul>
<li><p><strong>Scheduled Jobs</strong>: When notebooks are run as scheduled jobs, dynamically fetching parameters ensures that the notebook uses the correct configuration.</p></li>
<li><p><strong>Environment Configuration</strong>: Adjusting configurations based on the environment (development, staging, production) can be streamlined by dynamically fetching parameters.</p></li>
<li><p><strong>Reusable Notebooks</strong>: Creating reusable notebooks that adapt to different inputs without manual intervention enhances productivity and reduces errors.</p></li>
</ul>
</section>
</section>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<section id="related-tweets" class="level3">
<h3 class="anchored" data-anchor-id="related-tweets">Related Tweets</h3>
</section>
<section id="links" class="level3">
<h3 class="anchored" data-anchor-id="links">Links</h3>
<p><a href="https://docs.databricks.com/notebooks/widgets.html">Databricks Widgets Documentation</a><br>
<a href="https://docs.databricks.com/workflows/jobs.html">Databricks Job Parameters</a></p>
<p>[Dynamic Run Parameters] (https://stackoverflow.com/questions/63018871/how-do-you-get-the-run-parameters-and-runid-within-databricks-notebook)</p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>notebooks</category>
  <category>job parameters</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/bindings.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
<item>
  <title>Notebook Context</title>
  <link>DailyDatabricks.Tips/tips/Notebook/IpythonWidgets.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled" title="Beginner Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Beginner Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This tip is part of the <a href="../../#category=Decembricks">Decembricks 2024 series</a> to teach a new tip everyday of december.</p>
</div>
</div>
<section id="summary" class="level1">
<h1>Summary</h1>
<ul>
<li>Get Metadata &amp; Context on your current notebook environment</li>
<li>User Credentials (email, IPs, azuread object ids)</li>
<li>Browser details &amp; language</li>
<li>Cluster Ids</li>
</ul>
</section>
<section id="code" class="level1">
<h1>Code</h1>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="" aria-current="page">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Scala</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">R</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="get-context-object" class="level3">
<h3 class="anchored" data-anchor-id="get-context-object">Get Context Object</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">dbutils.notebook.entry_point.getDbutils().notebook().getContext()</span></code></pre></div></div>
</section>
<section id="example-get-notebook-path-property" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"></span>
<span id="cb2-2">dbutils.notebook.entry_point.getDbutils().notebook().getContext() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-3">  .notebookPath() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-4">  .toString() </span></code></pre></div></div>
</section>
<section id="convert-context-json-object-to-dictionary" class="level3">
<h3 class="anchored" data-anchor-id="convert-context-json-object-to-dictionary">Convert Context Json Object to Dictionary</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb3-2">json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())</span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<section id="get-context-object-1" class="level3">
<h3 class="anchored" data-anchor-id="get-context-object-1">Get Context Object</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb4-1">dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span></code></pre></div></div>
</section>
<section id="example-get-notebook-path-property-1" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property-1">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb5-1"></span>
<span id="cb5-2">dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>notebookPath \</span>
<span id="cb5-3">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> </span></code></pre></div></div>
</section>
<section id="convert-json-object-to-dictionary" class="level3">
<h3 class="anchored" data-anchor-id="convert-json-object-to-dictionary">Convert Json Object to Dictionary</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode scala code-with-copy"><code class="sourceCode scala"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> context <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dbutils<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>notebook<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getContext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Returns MAP[String,String] no need to map it</span></span></code></pre></div></div>
</section>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>R doesn’t have dbutils.notebook context object. However, there are several objects located in the R global environment which have properties from the databricks notebook context. We can view these by using the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ls">LS</a> command with pattern matching</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"></span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List only the objects that begin with "spark"</span></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^spark"</span>)</span></code></pre></div></div>
<section id="example-get-notebook-path-property-2" class="level3">
<h3 class="anchored" data-anchor-id="example-get-notebook-path-property-2">Example: Get Notebook Path Property</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"> spark.databricks.api.url  </span></code></pre></div></div>
</section>
<section id="sample-list-of-properties" class="level3">
<h3 class="anchored" data-anchor-id="sample-list-of-properties">Sample List of properties</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.api.url"</span>                                              </span>
<span id="cb9-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.driverNfs.clusterWideVirtualEnv"</span>                      </span>
<span id="cb9-3">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.env"</span>                                                  </span>
<span id="cb9-4">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.inherited.credentials.keys.spark.databricks.api.token"</span></span>
<span id="cb9-5">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.notebook.id"</span>                                          </span>
<span id="cb9-6">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.notebook.path"</span>                                        </span>
<span id="cb9-7">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.replId"</span>                                               </span>
<span id="cb9-8">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.databricks.token"</span>                                                </span>
<span id="cb9-9">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spark.scheduler.pool"</span>     </span>
<span id="cb9-10">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orgId"</span></span></code></pre></div></div>
</section>
<section id="turn-the-spark-variables-into-a-key-value-list" class="level3">
<h3 class="anchored" data-anchor-id="turn-the-spark-variables-into-a-key-value-list">Turn the spark variables into a Key Value list</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the names of objects that begin with "spark"</span></span>
<span id="cb10-2">spark_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^spark"</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the values of these objects</span></span>
<span id="cb10-5">spark_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mget</span>(spark_names)</span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert the named vector to a list</span></span>
<span id="cb10-8">spark_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(spark_values)</span></code></pre></div></div>
</section>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-caution callout-titled" title="Sample Output">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Sample Output
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Below is a sample object returned from the notebook context object (some details are sanitized for security) and this is not an exhaustive output of the context output</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb11-1"></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'rootRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">None</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-3"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'currentRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">None</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-4"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jobGroup'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3390617077668218965</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7460294295175766547</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89e1</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">d0119f914af39bafdb017336882e'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-5"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tags'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServerBackend</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-54787e706</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ebf268a'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-6">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'shardName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'az-eastus-c3'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-7">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'com.databricks.backend.common.rpc.InternalDriverBackendMessages$StartRepl'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-8">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterMemory'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8192</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-9">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'serverBackendName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'com.databricks.backend.daemon.driver.DriverCorral'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-10">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebookId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-11">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'projectName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'driver'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-12">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tier'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'tier-multitenant'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-13">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'eventWindowTime'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2254196.899999991</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-14">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'httpTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/notebook/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/command/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3362446343388373</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-15">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'commandRunId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'dddd47ee</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-68</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fe</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fd2</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-9042</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-fbde8fd28a8e'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-16">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'buildHash'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">''</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-17">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'workspaceRoutingTarget'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'null'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-18">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHash'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'#notebook/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2048538015852092</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/command/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3362446343388373</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-19">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'host'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.139</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">64.4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-20">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserPathName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-21">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebookLanguage'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sql'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-22">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'workspaceRoutingBucket'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'null'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-23">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sparkVersion'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.1</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.x-scala2.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-24">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'hostName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'cons-webapp</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-25">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'httpMethod'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'POST'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-26">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserIdleTime'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">411</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-27">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jettyRpcJettyVersion'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-28">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserLanguage'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'en-GB'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-29">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserTabId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">fc6c79</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-18</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">df</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-4</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ff9-b0e7</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-35</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">df3da8bc08'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-30">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sourceIpAddress'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;AN_IP&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-31">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'loadedUiVersions'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Map(monolith</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">f03dcdb1e979dfefcf2f878284c318bd21970a5d)'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-32">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserUserAgent'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Mozilla/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(Windows</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">NT</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Win64;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">x64)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">AppleWebKit/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">537.36</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(KHTML</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">like</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Gecko)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Chrome/109.0.0.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Safari/537.36',</span></span>
<span id="cb11-33">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'orgId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2675080094955785</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-34">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'userAgent'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'Mozilla/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(Windows</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">NT</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Win64;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">x64)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">AppleWebKit/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">537.36</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(KHTML</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">like</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Gecko)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Chrome/109.0.0.0</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Safari/537.36',</span></span>
<span id="cb11-35">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'0120</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-154009-7</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">bof0jyo'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-36">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'serverEventId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'CgwIusXzngYQ9+mEqAIQgAQYiQE6EKeKn3/v7UOlvUQlALPm2wI='</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-37">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'rootOpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServiceMain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ef19d61f07f0002'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-38">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'requestIdWasMissing'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-39">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sessionId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'d9be357a4121697a1de52d1c3b447874315900e2b5a5054b8aa92576f1e865be'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-40">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterCreator'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;EMAIL_ADDRESS&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-41">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'originatedFromEnvoy'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-42">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientBranchName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'webapp_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-27</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.45</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z_master_b5b81920_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1040710074</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-43">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientTimestamp'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1675421567053</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-44">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clusterType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'spot'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-45">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'requestId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'CgwIusXzngYQ4/r7pwIQgAQYiQE6EKeKn3/v7UOlvUQlALPm2wI='</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-46">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHasFocus'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-47">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'userId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8925728005363108</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-48">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserIsHidden'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'false'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-49">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'principalIdpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;GUID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-50">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'clientLocale'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'en'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-51">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'branchName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'webapp_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-27</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">_</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.45</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Z_master_b5b81920_</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1040710074</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-52">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'opType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'ServerBackend'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-53">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'sourcePortNumber'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-54">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'user'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;EMAIL_ADDRESS&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-55">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'principalIdpObjectId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;GUID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-56">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'browserHostName'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'&lt;WORKSPACE&gt;.azuredatabricks.net'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-57">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'parentOpId'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'RPCClient</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">ef19d61f07f11ae'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-58">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'jettyRpcType'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'InternalDriverBackendMessages$DriverBackendRequest'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb11-59"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'extraContext'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'allowStdin'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-60">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'non_uc_api_token'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">''</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-61">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'commandResultJsonMaxBytes'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20971520</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-62">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'notebook_path'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/sqlsdamples'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-63">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'thresholdForStoringInDbfs'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-64">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'enableStoringResultsInDbfs'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'true'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-65">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'api_url'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'https://eastus-c3.azuredatabricks.net'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-66">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'aclPathOfAclRoot'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'/workspace/&lt;ID&gt;'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-67">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'api_token'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">REDACTED</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb11-68"> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'credentialKeys'</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'adls_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-69">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'adls_gen2_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-70">  <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">'synapse_aad_token'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-71">  </span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-simple callout-tip callout-titled" title="Capture your current notebook cell">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Capture your current notebook cell
</div>
</div>
<div class="callout-body-container callout-body">
<p>See how you can use the <a href="../../tips/Notebook/context.html">Notebook Context</a> to capture the <a href="">current cell of your notebook</a> and leverage it in your applications and logs <a href="">here</a></p>
</div>
</div>
</section>
<section id="detail" class="level1">
<h1>Detail</h1>
<p>The code snippet <code>dbutils.notebook.entry_point.getDbutils().notebook().getContext()</code> may appear intimidating at first glance, but fret not, let’s break it down:</p>
<ol type="1">
<li><p><strong>dbutils</strong>: This is a utility library in Databricks, providing a collection of utility functions and classes that help in interacting with Databricks functionalities.</p></li>
<li><p><strong>notebook</strong>: This refers to the notebook module within <code>dbutils</code>, which contains methods for interacting with the notebook environment.</p></li>
<li><p><strong>entry_point</strong>: The <code>entry_point</code> attribute within the notebook module refers to the starting point of the notebook execution.</p></li>
<li><p><strong>getDbutils</strong>: This is a method call which returns an instance of <code>dbutils</code>.</p></li>
<li><p><strong>notebook()</strong>: This method call, following <code>getDbutils</code>, returns an instance of the notebook module.</p></li>
<li><p><strong>getContext()</strong>: Finally, this method retrieves the current Notebook Context, encapsulating information like notebook path, user, and other contextual details.</p></li>
</ol>
<p>Benefits of Accessing Notebook Context: Accessing the Notebook Context can serve various purposes:</p>
<ul>
<li><p><strong>Identifying User Information</strong>: By accessing the Notebook Context, you can retrieve information about the user executing the notebook. This could be useful for auditing or personalizing the notebook behavior based on the user.</p></li>
<li><p><strong>Fetching Notebook Path</strong>: If your logic depends on the particular notebook or its location within the Databricks workspace, accessing the Notebook Context is invaluable.</p></li>
<li><p><strong>Managing Execution Flow</strong>: Utilizing the Notebook Context can aid in managing the flow of notebook execution, especially in complex, multi-notebook workflows.</p></li>
</ul>
</section>
<section id="references-further-reading" class="level1">
<h1>References &amp; Further Reading</h1>
<section id="related-tweets" class="level3">
<h3 class="anchored" data-anchor-id="related-tweets">Related Tweets</h3>
<blockquote class="twitter-tweet blockquote" data-dnt="true" data-theme="light">
<p lang="en" dir="ltr">
</p><p>⚠ Productionising your <a href="https://twitter.com/databricks?ref_src=twsrc%5Etfw"><span class="citation" data-cites="Databricks">@Databricks</span></a> workloads<br><br>This is an essential tip to enhance your logging and monitoring‼<br><br>❔Did you know you can capture context &amp; metadata in <a href="https://twitter.com/hashtag/databricks?src=hash&amp;ref_src=twsrc%5Etfw">#databricks</a> notebook? <br><br>Information such as<br>📔Current Databricks notebook<br>⛏Job Ids<br>👩‍💻User<br>🌐Browser &amp; more🔽</p>
<p></p>
<p>— Daily Databricks (<span class="citation" data-cites="DailyDatabricks">@DailyDatabricks</span>) <a href="https://twitter.com/DailyDatabricks/status/1535319517609459715?ref_src=twsrc%5Etfw">June 10, 2022</a></p>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</section>
<section id="links" class="level3">
<h3 class="anchored" data-anchor-id="links">Links</h3>
<p><a href="https://stackoverflow.com/questions/69158343/how-to-get-databricks-notebook-context-of-child-notebook">Retriving child notebooks context objects</a></p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>context</category>
  <category>notebook</category>
  <category>dbutils</category>
  <guid>DailyDatabricks.Tips/tips/Notebook/IpythonWidgets.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
  <media:content url="DailyDatabricks.Tips/tips/Notebook/Assets/notebook-context.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Monitoring Your Jobs with Lakeflow System Tables</title>
  <link>DailyDatabricks.Tips/tips/Observability/LakeflowJobsSystemTables.html</link>
  <description><![CDATA[ 





<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>The Lakeflow system tables <code>jobs</code>, <code>job_tasks</code>, <code>job_run_timeline</code>, and <code>job_task_run_timeline</code> are now Generally Available as of January 2026</li>
<li>These tables provide account-wide visibility into all job definitions, run history, and task-level execution metrics</li>
<li>Join with <code>billing.usage</code> to calculate cost per job run for precise spend attribution</li>
</ul>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Understanding what your Databricks jobs are doing — and what they are costing you — has always required stitching together information from the Jobs UI, cluster logs, and billing exports. With the GA release of the Lakeflow system tables in January 2026, Databricks now provides a unified, SQL-queryable record of every job and job run across your entire account.</p>
<p>These tables live in the <code>system.lakeflow</code> schema (previously called <code>system.workflow</code> — the content is identical) and cover all workspaces deployed in the same cloud region. They retain 365 days of data at no additional cost, and they support streaming reads so you can build real-time monitoring pipelines on top of them.</p>
</section>
<section id="what-tables-are-available" class="level2">
<h2 class="anchored" data-anchor-id="what-tables-are-available">What Tables Are Available?</h2>
<p>The <code>system.lakeflow</code> schema contains four GA tables and two in Public Preview:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Table</th>
<th>Description</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>jobs</strong></td>
<td>All job definitions in your account</td>
<td>SCD2 (slowly changing)</td>
</tr>
<tr class="even">
<td><strong>job_tasks</strong></td>
<td>Task definitions within each job</td>
<td>SCD2</td>
</tr>
<tr class="odd">
<td><strong>job_run_timeline</strong></td>
<td>Run-level execution history and metrics</td>
<td>Immutable</td>
</tr>
<tr class="even">
<td><strong>job_task_run_timeline</strong></td>
<td>Task-level execution history and metrics</td>
<td>Immutable</td>
</tr>
<tr class="odd">
<td><em>pipelines</em> (Preview)</td>
<td>Pipeline definitions</td>
<td>SCD2</td>
</tr>
<tr class="even">
<td><em>pipeline_update_timeline</em> (Preview)</td>
<td>Pipeline update history</td>
<td>Immutable</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-simple callout-tip callout-titled" title="Beginner Tip">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Beginner Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>jobs</code> table is a slowly changing dimension (SCD2) table. When a job definition changes, a new row is emitted rather than updating the existing one. This means you get a full audit trail of every configuration change.</p>
</div>
</div>
</section>
<section id="practical-examples" class="level2">
<h2 class="anchored" data-anchor-id="practical-examples">Practical Examples</h2>
<section id="find-failed-jobs-in-the-last-24-hours" class="level3">
<h3 class="anchored" data-anchor-id="find-failed-jobs-in-the-last-24-hours">Find Failed Jobs in the Last 24 Hours</h3>
<p>A quick query to surface recent failures across your account:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span></span>
<span id="cb1-2">    j.name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> job_name,</span>
<span id="cb1-3">    r.run_id,</span>
<span id="cb1-4">    r.result_state,</span>
<span id="cb1-5">    r.termination_code,</span>
<span id="cb1-6">    r.period_start_time,</span>
<span id="cb1-7">    r.period_end_time</span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.job_run_timeline r</span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.jobs j</span>
<span id="cb1-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> r.workspace_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.workspace_id</span>
<span id="cb1-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.job_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.job_id</span>
<span id="cb1-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> r.result_state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'FAILED'</span></span>
<span id="cb1-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.period_start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_timestamp</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">INTERVAL</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span> HOURS</span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> r.period_start_time <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
</section>
<section id="calculate-cost-per-job-run" class="level3">
<h3 class="anchored" data-anchor-id="calculate-cost-per-job-run">Calculate Cost Per Job Run</h3>
<p>One of the most valuable patterns is joining the run timeline with the billing system table to get actual cost per execution:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span></span>
<span id="cb2-2">    j.name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> job_name,</span>
<span id="cb2-3">    r.run_id,</span>
<span id="cb2-4">    r.result_state,</span>
<span id="cb2-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SUM</span>(b.usage_quantity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lp.pricing.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">default</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> estimated_cost</span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.job_run_timeline r</span>
<span id="cb2-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.jobs j</span>
<span id="cb2-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> r.workspace_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.workspace_id</span>
<span id="cb2-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.job_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.job_id</span>
<span id="cb2-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.billing.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">usage</span> b</span>
<span id="cb2-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> r.job_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> b.usage_metadata.job_id</span>
<span id="cb2-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.run_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> b.usage_metadata.job_run_id</span>
<span id="cb2-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.billing.list_prices lp</span>
<span id="cb2-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> b.sku_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lp.sku_name</span>
<span id="cb2-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> b.usage_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lp.price_start_time</span>
<span id="cb2-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> j.name, r.run_id, r.result_state</span>
<span id="cb2-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> estimated_cost <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
<div class="callout callout-style-simple callout-warning callout-titled" title="Important">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Jobs running on all-purpose (interactive) compute share resources with other workloads, so cost attribution will not be precise. For accurate per-job costing, use dedicated job compute or serverless compute.</p>
</div>
</div>
</section>
<section id="monitor-job-duration-trends" class="level3">
<h3 class="anchored" data-anchor-id="monitor-job-duration-trends">Monitor Job Duration Trends</h3>
<p>Track whether your jobs are getting slower over time:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span></span>
<span id="cb3-2">    j.name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> job_name,</span>
<span id="cb3-3">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">DATE</span>(r.period_start_time) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> run_date,</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AVG</span>(r.run_duration_ms) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> avg_duration_seconds,</span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">COUNT</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> run_count</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.job_run_timeline r</span>
<span id="cb3-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">system</span>.lakeflow.jobs j</span>
<span id="cb3-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> r.workspace_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.workspace_id</span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.job_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j.job_id</span>
<span id="cb3-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> r.period_start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_timestamp</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">INTERVAL</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span> DAYS</span>
<span id="cb3-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> r.result_state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SUCCESS'</span></span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> j.name, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">DATE</span>(r.period_start_time)</span>
<span id="cb3-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> j.name, run_date;</span></code></pre></div></div>
</section>
</section>
<section id="access-requirements" class="level2">
<h2 class="anchored" data-anchor-id="access-requirements">Access Requirements</h2>
<p>To query these tables, you need one of:</p>
<ul>
<li><strong>Metastore admin</strong> and <strong>account admin</strong> roles, or</li>
<li>Explicit <code>USE</code> and <code>SELECT</code> grants on the <code>system.lakeflow</code> schema</li>
</ul>
<div class="callout callout-style-simple callout-note callout-titled" title="Regional Scope">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Regional Scope
</div>
</div>
<div class="callout-body-container callout-body">
<p>System tables contain records from all workspaces in the same cloud region. To see jobs from another region, query from a workspace deployed in that region.</p>
</div>
</div>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://docs.databricks.com/aws/en/admin/system-tables/jobs">Jobs System Table Reference</a></li>
<li><a href="https://docs.databricks.com/aws/en/jobs/monitor">Monitoring and Observability for Lakeflow Jobs</a></li>
<li><a href="https://docs.databricks.com/aws/en/jobs/">Lakeflow Jobs Overview</a></li>
<li><a href="https://docs.databricks.com/aws/en/release-notes/product/2026/january">January 2026 Release Notes</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Lakeflow</category>
  <category>Observability</category>
  <category>Data Engineering</category>
  <guid>DailyDatabricks.Tips/tips/Observability/LakeflowJobsSystemTables.html</guid>
  <pubDate>Fri, 04 Sep 2026 19:48:01 GMT</pubDate>
</item>
</channel>
</rss>
