<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Is there something like Oracle's VPD-Feature in Databricks? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/is-there-something-like-oracle-s-vpd-feature-in-databricks/m-p/10492#M5662</link>
    <description>&lt;P&gt;Since I am porting some code from Oracle to Databricks, I have another specific question.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In Oracle there's something called &lt;A href="https://docs.oracle.com/database/121/DBSEG/vpd.htm#DBSEG247" alt="https://docs.oracle.com/database/121/DBSEG/vpd.htm#DBSEG247" target="_blank"&gt;Virtual Private Database&lt;/A&gt;, VPD. It's a simple security feature used to generate a WHERE-clause which the system will add to a user's query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Basically, you set an "Application Context" (something like a tag) when a connection is made. This context is read in a specific function which then returns a string to be attached as the WHERE clause to the actual query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am aware of the current_user()-function in Pyspark/Databricks. However, I can't use it, because it would return the technical username of the BI-tool. That's why the VPD was used in the first place.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Jan 2023 14:01:16 GMT</pubDate>
    <dc:creator>quakenbush</dc:creator>
    <dc:date>2023-01-30T14:01:16Z</dc:date>
    <item>
      <title>Is there something like Oracle's VPD-Feature in Databricks?</title>
      <link>https://community.databricks.com/t5/data-engineering/is-there-something-like-oracle-s-vpd-feature-in-databricks/m-p/10492#M5662</link>
      <description>&lt;P&gt;Since I am porting some code from Oracle to Databricks, I have another specific question.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In Oracle there's something called &lt;A href="https://docs.oracle.com/database/121/DBSEG/vpd.htm#DBSEG247" alt="https://docs.oracle.com/database/121/DBSEG/vpd.htm#DBSEG247" target="_blank"&gt;Virtual Private Database&lt;/A&gt;, VPD. It's a simple security feature used to generate a WHERE-clause which the system will add to a user's query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Basically, you set an "Application Context" (something like a tag) when a connection is made. This context is read in a specific function which then returns a string to be attached as the WHERE clause to the actual query.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am aware of the current_user()-function in Pyspark/Databricks. However, I can't use it, because it would return the technical username of the BI-tool. That's why the VPD was used in the first place.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 14:01:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/is-there-something-like-oracle-s-vpd-feature-in-databricks/m-p/10492#M5662</guid>
      <dc:creator>quakenbush</dc:creator>
      <dc:date>2023-01-30T14:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: Is there something like Oracle's VPD-Feature in Databricks?</title>
      <link>https://community.databricks.com/t5/data-engineering/is-there-something-like-oracle-s-vpd-feature-in-databricks/m-p/10493#M5663</link>
      <description>&lt;P&gt;@Roger Bieri​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In Databricks, you can use the UserDefinedFunction (UDF) feature to create a custom function that will be applied to a DataFrame. You can use this feature to add a WHERE clause to a DataFrame based on the user context. Here's an example of how you can use UDF to apply a WHERE clause to a DataFrame based on the user context:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.functions import udf
from pyspark.sql.types import StringType
&amp;nbsp;
# Define your custom function to add WHERE clause to DataFrame
def add_where_clause(user_context):
    if user_context == "context_1":
        return "WHERE column_1 &amp;gt; 10"
    elif user_context == "context_2":
        return "WHERE column_2 &amp;lt; 100"
    else:
        return ""
&amp;nbsp;
# Create a UDF from the custom function
add_where_clause_udf = udf(add_where_clause, StringType())
&amp;nbsp;
# Apply the UDF to the DataFrame to add WHERE clause based on user context
df = df.select("*", add_where_clause_udf(current_user()).alias("where_clause"))
df_filtered = df.filter(df.where_clause != "")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this example, add_where_clause is a custom function that takes the user context as an input and returns a WHERE clause string based on the context. The add_where_clause_udf function creates a UDF from the custom function. The df DataFrame is then modified by applying the UDF to create a new column where_clause. Finally, the df_filtered DataFrame is created by filtering the rows where where_clause is not an empty string. You can modify the add_where_clause function to suit your specific use case, and apply the UDF to your DataFrame to add a WHERE clause based on the user context.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2023 14:07:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/is-there-something-like-oracle-s-vpd-feature-in-databricks/m-p/10493#M5663</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-10T14:07:58Z</dc:date>
    </item>
  </channel>
</rss>

