<?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 Reference a single item tuple using .format() in spark.sql() in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9913#M5187</link>
    <description>&lt;P&gt;I'm trying to pass the elements of a tuple into a sql query using .format(). This works fine when I have multiple items in my tuple, but when using a single item in a tuple I get an error.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;tuple1 = (1,2,3)
tuple2 = (5,)
&amp;nbsp;
combo = tuple1 + tuple2
&amp;nbsp;
print(combo)
&amp;nbsp;
query = ("""
&amp;nbsp;
select
case
  when column in {tuple1} then 1
  when column in {tuple2} then 2
end as check
&amp;nbsp;
from table
&amp;nbsp;
where column in {combo}
&amp;nbsp;
""".format(tuple1 = tuple1, tuple2 = tuple2, combo = combo))
&amp;nbsp;
print(query)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Whenever I print the query it keeps the comma at the end of the single tuple which is what I think is causing the error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Result"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/706i8F4FBAAAD2BA8214/image-size/large?v=v2&amp;amp;px=999" role="button" title="Result" alt="Result" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do want to continue to use a tuple because there are scenarios where I need to concatenate multiple tuples together.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Feb 2023 21:34:22 GMT</pubDate>
    <dc:creator>cmilligan</dc:creator>
    <dc:date>2023-02-06T21:34:22Z</dc:date>
    <item>
      <title>Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9913#M5187</link>
      <description>&lt;P&gt;I'm trying to pass the elements of a tuple into a sql query using .format(). This works fine when I have multiple items in my tuple, but when using a single item in a tuple I get an error.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;tuple1 = (1,2,3)
tuple2 = (5,)
&amp;nbsp;
combo = tuple1 + tuple2
&amp;nbsp;
print(combo)
&amp;nbsp;
query = ("""
&amp;nbsp;
select
case
  when column in {tuple1} then 1
  when column in {tuple2} then 2
end as check
&amp;nbsp;
from table
&amp;nbsp;
where column in {combo}
&amp;nbsp;
""".format(tuple1 = tuple1, tuple2 = tuple2, combo = combo))
&amp;nbsp;
print(query)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Whenever I print the query it keeps the comma at the end of the single tuple which is what I think is causing the error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Result"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/706i8F4FBAAAD2BA8214/image-size/large?v=v2&amp;amp;px=999" role="button" title="Result" alt="Result" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do want to continue to use a tuple because there are scenarios where I need to concatenate multiple tuples together.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Feb 2023 21:34:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9913#M5187</guid>
      <dc:creator>cmilligan</dc:creator>
      <dc:date>2023-02-06T21:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9914#M5188</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;can you please add some code or snippet where you are getting error, it will be more clear to understand your problem.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 06:04:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9914#M5188</guid>
      <dc:creator>ChaubeyPravin</dc:creator>
      <dc:date>2023-02-07T06:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9915#M5189</link>
      <description>&lt;P&gt;Could you please post the code and the error that you are getting?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 12:32:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9915#M5189</guid>
      <dc:creator>Lakshay</dc:creator>
      <dc:date>2023-02-07T12:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9916#M5190</link>
      <description>&lt;P&gt;Thanks! I added it in&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 13:34:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9916#M5190</guid>
      <dc:creator>cmilligan</dc:creator>
      <dc:date>2023-02-07T13:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9917#M5191</link>
      <description>&lt;P&gt;@Lakshay Goel​&amp;nbsp;I added it in&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 13:34:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9917#M5191</guid>
      <dc:creator>cmilligan</dc:creator>
      <dc:date>2023-02-07T13:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9918#M5192</link>
      <description>&lt;P&gt;For a single-value tuple, you need to use a check to modify the SQL query as well as how the tuple is passed. The single-value tuples will always have a comma at the end. You need to modify the query to be able to pass only the value from the tuple. The new query would look something like&lt;/P&gt;&lt;P&gt;query = ("""&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select key,&lt;/P&gt;&lt;P&gt;case&lt;/P&gt;&lt;P&gt;&amp;nbsp;when column in {tuple1} then 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;when column in ({tuple2}) then 2&lt;/P&gt;&lt;P&gt;end as check&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;from table&lt;/P&gt;&lt;P&gt;""".format(tuple1 = tuple1, tuple2 = tuple2[0], combo = combo))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 16:48:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9918#M5192</guid>
      <dc:creator>Lakshay</dc:creator>
      <dc:date>2023-02-07T16:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: Reference a single item tuple using .format() in spark.sql()</title>
      <link>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9919#M5193</link>
      <description>&lt;P&gt;Thanks for the reply. I think I'll keep digging around a little trying to find something that a user can update without having to worry if there is one value or multiple&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 20:54:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/reference-a-single-item-tuple-using-format-in-spark-sql/m-p/9919#M5193</guid>
      <dc:creator>cmilligan</dc:creator>
      <dc:date>2023-02-07T20:54:01Z</dc:date>
    </item>
  </channel>
</rss>

