<?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 Concatenating a row to be able to hash in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/concatenating-a-row-to-be-able-to-hash/m-p/125661#M47504</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Sometimes to load data we want to only update a row based on changes in values. SCD1 type scenarios for a data warehouse. One approach is equivalency A=A, B=B etc. Another is generating a hash of all rows of interest, I believe pretty common.&amp;nbsp; I have found several approaches as to how concatenating a row that will always successfully hash.&lt;/P&gt;&lt;P&gt;Firstly lets get concat() out of the way, it doesn't handle nulls. Any null and you get no hash as you end up with null output. Wrapping every column in isNull isn't great when you have 100 columns to deal with, although AI dev tools do help it still ends up looking untidy to me.&lt;/P&gt;&lt;P&gt;Next concat_ws() looks good as an option it can handle nulls, stringifies the row and produces a string output regardless of datatypes&lt;/P&gt;&lt;P&gt;Coalesce() also can, but appears limited to only a certain data types within a construct, so may fail on many typical structures where you have date, int and string data for example in a row. It would require creating strings for say Date data types. This may be a misunderstanding of least common datatype precedence, but someone can maybe explain what creates a typical risk in this case&lt;/P&gt;&lt;P&gt;Finally a but left field, but create a json string using to_json(). This will never fail to create a string with data in it that can be hashed. It may get a bit long in it's construct.&lt;/P&gt;&lt;P&gt;My questions:&lt;/P&gt;&lt;P&gt;Are there any edge cases where concat_ws will fail to produce a string when any single column in the row is populated?&lt;/P&gt;&lt;P&gt;Has anyone done any tests of&amp;nbsp; the performance of any of these to know how one will fail to perform in large data situations?&lt;/P&gt;&lt;P&gt;My crude performance testing shows them very similar, I am sure there are tested results which provides a win for one&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="turagittech_0-1752799736547.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18286i31ABEBC9D20E7144/image-size/medium?v=v2&amp;amp;px=400" role="button" title="turagittech_0-1752799736547.png" alt="turagittech_0-1752799736547.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In the above when I take out scheduling time for the queries concat_ws returned 37 rows/sec and to_json returned 28 rows/sec I have run the test a few times to pick up on any nuances and this result difference looks to be caching. If I run the test with the other query first the second is always fastest.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/concat" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/concat&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/concat_ws" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/concat_ws&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Jul 2025 00:56:04 GMT</pubDate>
    <dc:creator>turagittech</dc:creator>
    <dc:date>2025-07-18T00:56:04Z</dc:date>
    <item>
      <title>Concatenating a row to be able to hash</title>
      <link>https://community.databricks.com/t5/data-engineering/concatenating-a-row-to-be-able-to-hash/m-p/125661#M47504</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Sometimes to load data we want to only update a row based on changes in values. SCD1 type scenarios for a data warehouse. One approach is equivalency A=A, B=B etc. Another is generating a hash of all rows of interest, I believe pretty common.&amp;nbsp; I have found several approaches as to how concatenating a row that will always successfully hash.&lt;/P&gt;&lt;P&gt;Firstly lets get concat() out of the way, it doesn't handle nulls. Any null and you get no hash as you end up with null output. Wrapping every column in isNull isn't great when you have 100 columns to deal with, although AI dev tools do help it still ends up looking untidy to me.&lt;/P&gt;&lt;P&gt;Next concat_ws() looks good as an option it can handle nulls, stringifies the row and produces a string output regardless of datatypes&lt;/P&gt;&lt;P&gt;Coalesce() also can, but appears limited to only a certain data types within a construct, so may fail on many typical structures where you have date, int and string data for example in a row. It would require creating strings for say Date data types. This may be a misunderstanding of least common datatype precedence, but someone can maybe explain what creates a typical risk in this case&lt;/P&gt;&lt;P&gt;Finally a but left field, but create a json string using to_json(). This will never fail to create a string with data in it that can be hashed. It may get a bit long in it's construct.&lt;/P&gt;&lt;P&gt;My questions:&lt;/P&gt;&lt;P&gt;Are there any edge cases where concat_ws will fail to produce a string when any single column in the row is populated?&lt;/P&gt;&lt;P&gt;Has anyone done any tests of&amp;nbsp; the performance of any of these to know how one will fail to perform in large data situations?&lt;/P&gt;&lt;P&gt;My crude performance testing shows them very similar, I am sure there are tested results which provides a win for one&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="turagittech_0-1752799736547.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18286i31ABEBC9D20E7144/image-size/medium?v=v2&amp;amp;px=400" role="button" title="turagittech_0-1752799736547.png" alt="turagittech_0-1752799736547.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In the above when I take out scheduling time for the queries concat_ws returned 37 rows/sec and to_json returned 28 rows/sec I have run the test a few times to pick up on any nuances and this result difference looks to be caching. If I run the test with the other query first the second is always fastest.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/concat" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/concat&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/concat_ws" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/concat_ws&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jul 2025 00:56:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/concatenating-a-row-to-be-able-to-hash/m-p/125661#M47504</guid>
      <dc:creator>turagittech</dc:creator>
      <dc:date>2025-07-18T00:56:04Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating a row to be able to hash</title>
      <link>https://community.databricks.com/t5/data-engineering/concatenating-a-row-to-be-able-to-hash/m-p/125862#M47552</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/74890"&gt;@turagittech&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;concat_ws() is generally the most practical and reliable option here. It handles mixed datatypes well and safely skips nulls. The only edge cases you'd typically run into are with complex or unsupported custom datatypes or if the separator itself is NULL.&lt;/P&gt;&lt;P&gt;The performance difference you noticed is likely due to caching, whichever query runs second tends to benefit from the data already being cached. to_json() is more suited for nested or complex row structures.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jul 2025 12:02:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/concatenating-a-row-to-be-able-to-hash/m-p/125862#M47552</guid>
      <dc:creator>SP_6721</dc:creator>
      <dc:date>2025-07-21T12:02:50Z</dc:date>
    </item>
  </channel>
</rss>

