<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Complete Fabrication]]></title><description><![CDATA[Complete Fabrication]]></description><link>https://vtinsights.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 29 Nov 2026 20:21:06 GMT</lastBuildDate><atom:link href="https://vtinsights.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[OneLake RLS in Microsoft Fabric Spark: Why My Spark SQL Join Worked but My PySpark Join Didn't]]></title><description><![CDATA[Microsoft Fabric OneLake RLS: a PySpark join fails while the equivalent Spark SQL works. Setup, findings and workaround.]]></description><link>https://vtinsights.hashnode.dev/onelake-rls-pyspark-join-issues</link><guid isPermaLink="true">https://vtinsights.hashnode.dev/onelake-rls-pyspark-join-issues</guid><category><![CDATA[microsoft fabric]]></category><category><![CDATA[PySpark]]></category><category><![CDATA[onelake]]></category><category><![CDATA[Data security]]></category><category><![CDATA[row-level-security]]></category><dc:creator><![CDATA[Vishal]]></dc:creator><pubDate>Sat, 14 Nov 2026 23:38:11 GMT</pubDate><content:encoded><![CDATA[<p><em>Hello Fellow Fabricators 👋 — this is my first Microsoft Fabric post, and honestly I didn't plan for it to be a debugging story. But here we are.</em></p>
<p>I've been putting OneLake security through its paces as part of a bigger piece of work: figuring out <strong>where row-level security actually belongs in an enterprise data engineering architecture</strong>. Not "does the feature exist" — it does — but "does it survive contact with the way engineers really build pipelines?"</p>
<p>Spoiler: mostly yes. With one rather specific exception that cost me an afternoon and taught me more about Fabric Spark internals than I expected.</p>
<blockquote>
<p><strong>TL;DR</strong> A OneLake RLS-secured Lakehouse table joined to a normal Delta table <strong>fails when I use the PySpark DataFrame API</strong>, but the <strong>logically identical Spark SQL join works fine</strong>. RLS itself is enforcing correctly in both cases. I've raised it with Microsoft and I'll update this post when I know more.</p>
</blockquote>
<hr />
<h2>What I was actually trying to do</h2>
<p>The basic requirement was simple:</p>
<ul>
<li>keep one shared Lakehouse table in secured data workspace;</li>
<li>apply country-based OneLake RLS;</li>
<li>let an engineer work only with the rows they're entitled to from a dedicated code workspace accessing Lakehouse through item-level permissions;</li>
<li>let them keep using <em>normal</em> Spark engineering patterns on the filtered data.</li>
</ul>
<p>That last bullet is the one that matters, and it's the one that's where the rest of the article expands on challenges I faced</p>
<p>For Reference here is how the architecture would look like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6af86fb76273b650736de55c/eda46be2-4b58-4ebf-9ef6-999b0c25fbc0.png" alt="" style="display:block;margin:0 auto" />

<p>The first three worked exactly as advertised. Then I joined the secured table to an ordinary Delta lookup table using the DataFrame API, and things got interesting.</p>
<hr />
<h2>The test setup</h2>
<p>Nothing exotic — a small synthetic People dataset.</p>
<p>The secured table:</p>
<pre><code class="language-text">dbo.employee
</code></pre>
<p>with fields such as:</p>
<pre><code class="language-text">employee_key
employee_name
country_code
department_key
job_title
job_grade
employment_status
</code></pre>
<p>A OneLake security role restricted employee rows by <code>country_code</code>.</p>
<p>The lookup table:</p>
<pre><code class="language-text">dbo.department
</code></pre>
<p>containing:</p>
<pre><code class="language-text">department_key
department_name
business_unit
</code></pre>
<p>No RLS on <code>dbo.department</code>.</p>
<p>One important detail that's easy to trip over: <strong>my test user was deliberately <em>not</em> a Workspace Admin, Member or Contributor in the Data Workspace where the lakehouse is.</strong> Those roles bypass OneLake RLS/CLS entirely, so if you test as yourself with Admin rights you'll happily conclude everything is fine and prove nothing.</p>
<hr />
<h2>Step 1: RLS worked.</h2>
<p>Reading the secured table directly:</p>
<pre><code class="language-python">employee = spark.table("dbo.employee")

display(employee)
employee.count()
</code></pre>
<p>The engineer saw only the rows their OneLake role allowed. Row counts lined up. No complaints.</p>
<p>The department table behaved like any other Delta table:</p>
<pre><code class="language-python">department = spark.table("dbo.department")

display(department)
</code></pre>
<p>Great. Security config correct, confidence high. On to the actual transformation.</p>
<hr />
<h2>Step 2: The PySpark join that didn't</h2>
<p>This is about as vanilla as a silver-layer enrichment gets:</p>
<pre><code class="language-python">from pyspark.sql import functions as F

silver_employee = (
    employee.alias("e")
    .join(
        department.alias("d"),
        F.col("e.department_key")
        == F.col("d.department_key"),
        "left"
    )
    .select(
        "e.*",
        F.col("d.department_name"),
        F.col("d.business_unit")
    )
)
</code></pre>
<p>Because Spark is lazy, defining this succeeded and lulled me into a false sense of security (pun fully intended).</p>
<p>Then I triggered an action:</p>
<pre><code class="language-python">display(silver_employee)
</code></pre>
<p>or:</p>
<pre><code class="language-python">silver_employee.count()
</code></pre>
<p>And got:</p>
<pre><code class="language-text">java.io.NotSerializableException:
org.apache.spark.sql.delta.files.ShallowSnapshotDescriptor
</code></pre>
<p>A later run failed differently — inside the OneLake security execution path, with an internal Spark optimization error.</p>
<p>Here's the part that made me stop and stare at the screen: <strong>both DataFrames worked perfectly on their own.</strong> Read the secured table — fine. Read the lookup table — fine. Join them — boom.</p>
<p>It was the <em>combination</em> that failed.</p>
<hr />
<h2>Step 3: The Workaround: Spark SQL</h2>
<p>So I expressed exactly the same logical transformation in SQL:</p>
<pre><code class="language-python">silver_employee = spark.sql("""
    SELECT
        e.*,
        d.department_name,
        d.business_unit
    FROM dbo.employee e
    LEFT JOIN dbo.department d
        ON e.department_key = d.department_key
""")

display(silver_employee)
</code></pre>
<p>This worked. RLS-filtered employee rows, correctly enriched with department attributes. Same logic, same tables, same user, same session — different result.</p>
<p>Which leaves us here:</p>
<pre><code class="language-text">RLS-secured employee
        +
ordinary department
        |
        |-- PySpark DataFrame join  -&gt; fails
        |
        `-- Spark SQL join          -&gt; works
</code></pre>
<p>I'll be honest: "works in SQL, fails in DataFrame" is the kind of inconsistency that makes you re-run everything twice because you assume you've made a typo. I hadn't.</p>
<hr />
<h2>Why this is more than a curiosity</h2>
<p>Microsoft's documentation says Spark support for OneLake security covers:</p>
<ul>
<li>Spark SQL;</li>
<li>PySpark/Scala DataFrame APIs;</li>
<li>joins;</li>
<li>aggregations;</li>
<li>downstream transformations.</li>
</ul>
<p>So this isn't a case of "joins on secured tables aren't supported." They're meant to be.</p>
<p>What the evidence <em>suggests</em> is that the same logical operation is landing on a different execution or optimization path depending on how it's expressed — and something in the DataFrame path isn't playing nicely with the secured relation.</p>
<p>I'm deliberately <strong>not</strong> calling that a root cause. It's a hypothesis that fits the symptoms. It needs confirmation from Microsoft, and I'd rather publish an honest "I don't know yet" than a confident guess.</p>
<hr />
<h2>What I tried next</h2>
<p>My first instinct was join planning, so I went after broadcast joins:</p>
<pre><code class="language-python">spark.conf.set(
    "spark.sql.autoBroadcastJoinThreshold",
    "-1"
)
</code></pre>
<p>Issue remained.</p>
<p>Then Adaptive Query Execution:</p>
<pre><code class="language-python">spark.conf.set(
</code></pre>
<p>Still remained.</p>
<p>Still on my list to work through:</p>
<ul>
<li>forcing a sort-merge join;</li>
<li>repartitioning both DataFrames on the join key;</li>
<li>projecting a narrower column set <em>before</em> joining;</li>
<li>diffing the logical and physical plans of the SQL vs DataFrame versions;</li>
<li>testing a DataFrame → temp view → SQL transition to see exactly where the behaviour flips.</li>
</ul>
<p>That plan comparison is the one I'm most curious about — it should tell us whether the two paths genuinely diverge or whether something is being serialized differently along the way.</p>
<hr />
<h2>A workaround I actively dislike</h2>
<p>You <em>can</em> materialise the small lookup table locally:</p>
<pre><code class="language-python">department_local = spark.createDataFrame(
    department.collect(),
    schema=department.schema
)
</code></pre>
<p>And yes, it still doesn't fix the pyspark serialisation issue. But I'm not going to pretend <code>collect()</code> is an engineering solution. It drags everything through the driver and falls over the moment your "small lookup" turns out to be a large reference data</p>
<p>The fact that <strong>Spark SQL works</strong> is a far more useful workaround while the DataFrame behaviour is being investigated. It's expressible, reviewable, and doesn't quietly introduce a scalability cliff.</p>
<hr />
<h2>Bonus lesson: OneLake RLS changes how Spark executes</h2>
<p>This one was a genuinely useful side discovery.</p>
<p>A secured query doesn't just run inside your normal user Spark context. Fabric spins up a <strong>Microsoft-managed security context</strong> — you can see it in the Monitoring Hub as a <code>SparkSecurityControl</code> job — which applies the effective OneLake security policy before filtered data is handed back to your Spark execution.</p>
<p>That architectural detail matters a lot when you're sizing smaller capacities.</p>
<p>In my testing, an <strong>F2 capacity initially struggled to allocate both contexts.</strong> Constraining the user Spark pool to a <strong>Small, 1–1 node</strong> configuration left enough headroom for the security context, and basic RLS-secured queries started running.</p>
<p>Completely separate from the join failure — but if you're prototyping OneLake security on an F2 and getting resource errors before you even get to your query, this is probably why. Worth knowing before you spend an hour blaming your code.</p>
<hr />
<h2>So would I avoid OneLake RLS?</h2>
<p>No. And I want to be clear about that, because "I hit a bug" posts have a habit of being read as "don't use the feature."</p>
<p>The RLS policy itself <strong>worked.</strong> It filtered correctly on direct reads, and it filtered correctly through an equivalent Spark SQL join. The security model did its job.</p>
<p>My conclusion is narrower than a thumbs up or down:</p>
<blockquote>
<p>OneLake RLS is viable, but I'd validate the <strong>exact Spark engineering patterns a team actually uses</strong> before making RLS a default control on engineering-layer tables.</p>
</blockquote>
<hr />
<h2>What I've asked Microsoft</h2>
<p>I've raised the scenario with Fabric support and I'm after clarification on:</p>
<ol>
<li>whether the DataFrame behaviour is a known OneLake Security/Spark issue;</li>
<li>why the equivalent Spark SQL plan succeeds;</li>
<li>whether there's a supported Spark configuration or DataFrame pattern that avoids it;</li>
<li>whether Catalyst optimization, join strategy or secured-relation serialization is involved;</li>
</ol>
<p>If I get a confirmed root cause or a proper workaround, <strong>I'll update this post</strong>.</p>
<hr />
<h2>Final takeaway</h2>
<p>The most valuable lesson here wasn't technical — it was about where I stopped testing.</p>
<p>It's very easy to validate:</p>
<blockquote>
<p>"RLS works."</p>
</blockquote>
<p>…tick the box, and move on. For an engineering platform, the better question is:</p>
<blockquote>
<p>"Does RLS work with the way engineers actually build transformations?"</p>
</blockquote>
<p>In this case the answer is genuinely nuanced:</p>
<ul>
<li>the security policy enforces correctly ✅</li>
<li>Spark SQL joins work ✅</li>
<li>the equivalent PySpark DataFrame join currently doesn't, in my test setup ❌</li>
</ul>
<p>That distinction is the whole gap between a security proof-of-concept and a production engineering platform.</p>
<hr />
<p><strong>Have you hit this?</strong> If you've run OneLake RLS with PySpark DataFrame joins in Fabric — working <em>or</em> broken — I'd really like to hear your approach was and whether you saw the same <code>NotSerializableException</code>. Drop a comment; I'll fold anything useful into the update.</p>
<p>Thanks for reading my first Fabric post 🧵 — more to come as I keep tinkering!.</p>
]]></content:encoded></item></channel></rss>