cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Invalid Characters in Column Names " ,;{}()\n\t="

maxutil
New Contributor II

I'm reading data into a dataframe with

df = spark.read.json("s3://somepath/")

I've tried first creating a delta table using the DeltaTable API with:

DeltaTable.createIfNotExists(spark)\
            .location(target_path)\
            .addColumns(df.schema)\
            .execute()

This is giving me an AnalysisException and says I have invalid characters. I should set the table property 'delta.columnMapping.mode' to 'name'. I've tried:

spark.conf.set("spark.databricks.delta.defaults.columnMapping.mode", "name")

This hasn't worked. I've tried to use the DataFrame API instead:

df.write.format("delta").option("delta.columnMapping.mode", "name").save("s3://anotherpath")

But the same error message is coming up. The erroneous column names are deeply nested in structs.

Is there some other way for me to accomplish this delta table write (without flattening and fixing col names)?

EDIT: I ended up traversing through the schema and changing the problematic column names.

1 ACCEPTED SOLUTION

Accepted Solutions

VZLA
Databricks Employee
Databricks Employee

@jb1z @maxutil Have you tried it like this?

 

import dlt

@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name'})
def netsuite_items_inventory_price():
    return (
        spark.readStream.format('cloudFiles')
        .option('cloudFiles.format', 'json')
        .load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
    )

 

Looking at https://docs.databricks.com/en/delta-live-tables/properties.html#delta-live-tables-table-properties, it says "In addition to the table properties supported by Delta Lake, you can set the following table properties.", so ideally we should be able to provide a key-value pair, columnMapping is listed in https://docs.databricks.com/en/delta/table-properties.html#delta-table-properties:

 

delta.columnMapping.mode

Whether column mapping is enabled for Delta table columns and the corresponding Parquet columns that use different names.

See Rename and drop columns with Delta Lake column mapping.

Note: Enabling delta.columnMapping.mode automatically enables delta.randomizeFilePrefixes.

Data type: DeltaColumnMappingMode

Default: none

 

View solution in original post

6 REPLIES 6

jb1z
New Contributor II

Hi Databricks, it would be great to get a better answer to this 2022 issue, rather than changing column names. Thanks.

Walter_C
Databricks Employee
Databricks Employee

You can use column mapping to bypass this issue:

When column mapping is enabled for a Delta table, you can include spaces and any of these characters in the table’s column names: ,;{}()\n\t=.

https://docs.databricks.com/en/delta/column-mapping.html#supported-characters-in-column-names

jb1z
New Contributor II

@Walter_C thanks for the fast response. I tried using 'delta.columnMapping.mode', 'name' as suggested by @maxutil but this failed with the same error. The documentation you provided only had SQL examples and the DLT pipeline i am running only allows python, so i am trying to set the option correctly. This is the code I have, and the example schema below that, that i also tried which failed with an error about columns not matching, even though the columns exactly match my json data.

import dlt
@dlt.table(table_properties={'quality': 'bronze'})
def netsuite_items_inventory_price():
  return (
     spark.readStream.format('cloudFiles')
     .option('cloudFiles.format', 'json')
     .option('delta.columnMapping.mode', 'name')
     .load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
    )

# param for @dlt.tables(
schema="""
    ID STRING,
    RecordType STRING,
    Name STRING,
    DisplayName STRING,
    BasePrice STRING,
    IsItemUpdated STRING,
    OnHand STRING,
    TotalQuantityOnHand STRING,
    TotalValue STRING,
    AverageCost STRING,
    LastPurchasePrice STRING,
    Location STRING,
    Available STRING,
    UPCCode STRING,
    VendorName STRING,
    VendorCode STRING,
    EachHeight STRING,
    EachLength STRING,
    EachWidth STRING,
    Weight STRING
  """

 

VZLA
Databricks Employee
Databricks Employee

@jb1z @maxutil Have you tried it like this?

 

import dlt

@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name'})
def netsuite_items_inventory_price():
    return (
        spark.readStream.format('cloudFiles')
        .option('cloudFiles.format', 'json')
        .load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
    )

 

Looking at https://docs.databricks.com/en/delta-live-tables/properties.html#delta-live-tables-table-properties, it says "In addition to the table properties supported by Delta Lake, you can set the following table properties.", so ideally we should be able to provide a key-value pair, columnMapping is listed in https://docs.databricks.com/en/delta/table-properties.html#delta-table-properties:

 

delta.columnMapping.mode

Whether column mapping is enabled for Delta table columns and the corresponding Parquet columns that use different names.

See Rename and drop columns with Delta Lake column mapping.

Note: Enabling delta.columnMapping.mode automatically enables delta.randomizeFilePrefixes.

Data type: DeltaColumnMappingMode

Default: none

 

jb1z
New Contributor II

This code worked for me with all the additional table properties for versions. Thanks for your help @VZLA 

import dlt
@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name', 'delta.minReaderVersion': '2', 'delta.minWriterVersion': '5'})
def netsuite_items_inventory_price():
  return (
     spark.readStream.format('cloudFiles')
     .option('cloudFiles.format', 'json')
     .option('delta.columnMapping.mode', 'name')
     .load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
    )

 

VZLA
Databricks Employee
Databricks Employee

Glad it helped @jb1z , happy to help.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group