<?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 Auto Loader changes the order of columns when infering JSON schema (sorted lexicographically) in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/88848#M37641</link>
    <description>&lt;P&gt;We are using Auto Loader to read json files from S3 and ingest data into the bronze layer. But it seems auto loader struggles with schema inference and instead of preserving the order of columns from the JSON files, it sorts them lexicographically.&lt;/P&gt;&lt;P&gt;For example, if we have the following json&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{"colB": 1, "colC": 2, "colA": 3}
{"colB": 1, "colC": 2, "colA": 3}
{"colB": 1, "colC": 2, "colA": 3}&lt;/LI-CODE&gt;&lt;P&gt;And the following code&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import dlt

@dlt.table(table_properties={'quality': 'bronze'})
def my_table():
  return (
     spark.readStream.format('cloudFiles')
     .option('cloudFiles.format', 'json')
     .load('s3://my_bucket/my_table/')

)&lt;/LI-CODE&gt;&lt;P&gt;It will create the following table:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colA&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colB&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colC&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;This is a really bad behavior, and it seems there is no option to preserve the order of columns. The only way to preserve the order of tables is to explicitly specify the schema, which defeats the purpose of schema inference.&lt;/P&gt;&lt;P&gt;Looking at Databricks demos, this seems to be the default behavior:&amp;nbsp;&lt;A href="https://notebooks.databricks.com/demos/auto-loader/01-Auto-loader-schema-evolution-Ingestion.html" target="_blank"&gt;https://notebooks.databricks.com/demos/auto-loader/01-Auto-loader-schema-evolution-Ingestion.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Is there any way in Auto Loader to preserve the order of columns, like in most json-to-dataframe libraries?&lt;/P&gt;</description>
    <pubDate>Fri, 06 Sep 2024 10:00:53 GMT</pubDate>
    <dc:creator>arthurburkhardt</dc:creator>
    <dc:date>2024-09-06T10:00:53Z</dc:date>
    <item>
      <title>Auto Loader changes the order of columns when infering JSON schema (sorted lexicographically)</title>
      <link>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/88848#M37641</link>
      <description>&lt;P&gt;We are using Auto Loader to read json files from S3 and ingest data into the bronze layer. But it seems auto loader struggles with schema inference and instead of preserving the order of columns from the JSON files, it sorts them lexicographically.&lt;/P&gt;&lt;P&gt;For example, if we have the following json&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{"colB": 1, "colC": 2, "colA": 3}
{"colB": 1, "colC": 2, "colA": 3}
{"colB": 1, "colC": 2, "colA": 3}&lt;/LI-CODE&gt;&lt;P&gt;And the following code&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import dlt

@dlt.table(table_properties={'quality': 'bronze'})
def my_table():
  return (
     spark.readStream.format('cloudFiles')
     .option('cloudFiles.format', 'json')
     .load('s3://my_bucket/my_table/')

)&lt;/LI-CODE&gt;&lt;P&gt;It will create the following table:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colA&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colB&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;colC&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;This is a really bad behavior, and it seems there is no option to preserve the order of columns. The only way to preserve the order of tables is to explicitly specify the schema, which defeats the purpose of schema inference.&lt;/P&gt;&lt;P&gt;Looking at Databricks demos, this seems to be the default behavior:&amp;nbsp;&lt;A href="https://notebooks.databricks.com/demos/auto-loader/01-Auto-loader-schema-evolution-Ingestion.html" target="_blank"&gt;https://notebooks.databricks.com/demos/auto-loader/01-Auto-loader-schema-evolution-Ingestion.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Is there any way in Auto Loader to preserve the order of columns, like in most json-to-dataframe libraries?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 10:00:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/88848#M37641</guid>
      <dc:creator>arthurburkhardt</dc:creator>
      <dc:date>2024-09-06T10:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Auto Loader changes the order of columns when infering JSON schema (sorted lexicographically)</title>
      <link>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/100854#M40442</link>
      <description>&lt;P&gt;One alternative is to re-order the columns into the order you'd like using the &lt;A href="https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-alter-table-manage-column.html" target="_self"&gt;ALTER COLUMN API&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Taking your example,&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;ALTER TABLE catalog.schema.table ALTER COLUMN colB FIRST;
ALTER TABLE catalog.schema.table ALTER COLUMN colC AFTER colB;
&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 04 Dec 2024 06:51:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/100854#M40442</guid>
      <dc:creator>cgrant</dc:creator>
      <dc:date>2024-12-04T06:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Auto Loader changes the order of columns when infering JSON schema (sorted lexicographically)</title>
      <link>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/101173#M40573</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Auto Loader's default behavior of sorting columns lexicographically during schema inference is indeed a limitation when preserving the original order of JSON fields is important. Unfortunately, there isn't a built-in option in Auto Loader to maintain the original column order from JSON files while using automatic schema inference.&lt;/SPAN&gt;&lt;SPAN&gt;However, there are a few workarounds you can consider:&lt;/SPAN&gt;&lt;/P&gt;
&lt;H2 class="mb-2 mt-6 text-lg first:mt-3"&gt;1. Explicitly Define the Schema&lt;/H2&gt;
&lt;P&gt;&lt;SPAN&gt;While this approach doesn't fully leverage Auto Loader's schema inference capabilities, it allows you to maintain control over the column order:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;from pyspark.sql.types import StructType, StructField, IntegerType&lt;/P&gt;
&lt;P&gt;schema = StructType([&lt;BR /&gt;StructField("colB", IntegerType(), True),&lt;BR /&gt;StructField("colC", IntegerType(), True),&lt;BR /&gt;StructField("colA", IntegerType(), True)&lt;BR /&gt;])&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.table(table_properties={'quality': 'bronze'})&lt;BR /&gt;def my_table():&lt;BR /&gt;return (&lt;BR /&gt;spark.readStream.format('cloudFiles')&lt;BR /&gt;.option('cloudFiles.format', 'json')&lt;BR /&gt;.schema(schema)&lt;BR /&gt;.load('s3://my_bucket/my_table/')&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2 class="mb-2 mt-6 text-lg first:mt-3"&gt;2. Use a Post-Processing Step&lt;/H2&gt;
&lt;P&gt;&lt;SPAN&gt;You can leverage Auto Loader's schema inference and then reorder the columns in a subsequent step:&lt;/SPAN&gt;&lt;BR /&gt;python&lt;BR /&gt;from pyspark.sql.functions import col&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.table(table_properties={'quality': 'bronze'})&lt;BR /&gt;def my_table():&lt;BR /&gt;df = (&lt;BR /&gt;spark.readStream.format('cloudFiles')&lt;BR /&gt;.option('cloudFiles.format', 'json')&lt;BR /&gt;.load('s3://my_bucket/my_table/')&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;# Define the desired column order&lt;BR /&gt;desired_order = ["colB", "colC", "colA"]&lt;BR /&gt;&lt;BR /&gt;# Reorder columns&lt;BR /&gt;return df.select([col(c) for c in desired_order])&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 06:46:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/auto-loader-changes-the-order-of-columns-when-infering-json/m-p/101173#M40573</guid>
      <dc:creator>Sidhant07</dc:creator>
      <dc:date>2024-12-06T06:46:37Z</dc:date>
    </item>
  </channel>
</rss>

