(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to create a Delta table with Map-type columns in Lakehouse: step by step

João Barros 04 de December de 2026 4 min read

This tutorial shows how to create a Delta table in Lakehouse that includes Map-type columns (key-value structure). This is useful when each record has dynamic attributes without a fixed schema and allows efficient querying and transformations using SQL and PySpark.

Prerequisites

  • Account and workspace with Lakehouse (e.g., Azure Databricks or Microsoft Fabric with Delta support).
  • Environment with PySpark and SQL-over-Delta support (notebook or active cluster).
  • Permissions to create tables in the catalog/namespace and write to storage (OneLake or ADLS).

Step 1: Understand why to use Map in Lakehouse

A Map column allows storing key-value pairs within a column, avoiding sparse columns or overly rigid schemas. It is ideal for metadata, optional attributes, or events with variable fields. In Delta, a Map is serialized as a structural type and can be queried with SQL/PySpark functions.

Step 2: Create sample data with Map in PySpark

Let's create a DataFrame with an attributes column of type Map(String, String). This example runs in PySpark notebooks.

# Python / PySpark
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, MapType

spark = SparkSession.builder.getOrCreate()

schema = StructType([
    StructField("id", StringType(), False),
    StructField("attributes", MapType(StringType(), StringType()), True)
])

data = [
    ("1", {"color": "red", "size": "M"}),
    ("2", {"color": "blue"}),
    ("3", {"size": "L", "material": "cotton"})
]

df = spark.createDataFrame(data, schema=schema)
df.show(truncate=False)

Step 3: Write the DataFrame as a Delta table in Lakehouse

Write the DataFrame as a Delta table to a storage path or register it in the catalog as a managed/external table. Here we use an example path; replace with OneLake/ADLS according to your environment.

# gravar em formato Delta num caminho
out_path = "/mnt/datalake/delta/map_table"
df.write.format("delta").mode("overwrite").save(out_path)

# registar a tabela no catálogo (opcional)
spark.sql(f"CREATE TABLE IF NOT EXISTS map_table USING DELTA LOCATION '{out_path}'")

Step 4: Query and filter by Map keys in SQL

In the Lakehouse you can use SQL to access Map elements using bracket notation or functions like map_keys/map_values/element_at.

-- Exemplo SQL
SELECT id, attributes['color'] AS color
FROM map_table
WHERE attributes['size'] = 'M'

-- Obter todas as chaves e valores
SELECT id, map_keys(attributes) AS keys, map_values(attributes) AS values
FROM map_table

Step 5: Update (MERGE/UPDATE) to add/modify pairs in the Map

To update a Map column you can use UPDATE with an expression that merges maps. In PySpark you can use create_map and map concatenation (map_concat in Spark 3.0+). SQL and PySpark examples.

-- SQL: actualizar o map para adicionar uma nova key
UPDATE map_table
SET attributes = map_concat(attributes, map('discount', '10%'))
WHERE id = '1'

# PySpark: adicionar/actualizar chave num DataFrame e sobrescrever
from pyspark.sql.functions import map_concat, create_map, lit

df2 = spark.table('map_table').withColumn(
    'attributes',
    map_concat('attributes', create_map(lit('promo'), lit('yes')))
)
df2.write.format('delta').mode('overwrite').option('replaceWhere', "id IS NOT NULL").save(out_path)

Step 6: Explode Map to normalize into rows/columns

Sometimes it's useful to transform the Map into rows (key, value) for analysis or pivoting. Use explode or inline functions to normalize.

# PySpark: transformar em linhas
from pyspark.sql.functions import explode

exploded = spark.table('map_table').select('id', explode('attributes').alias('key', 'value'))
exploded.show()

-- SQL equivalente
SELECT id, key, value
FROM map_table
LATERAL VIEW EXPLODE(attributes) AS key, value

Verify the result

Confirm that the Delta table contains the Map column and that queries return the expected values:

  • Run SELECT id, attributes FROM map_table to see the raw content.
  • Test attributes['color'] and map_keys(attributes) to validate keys/values.
  • Check updates with SELECT after the UPDATE/MERGE.

Conclusion

Using Map columns in Delta tables in the Lakehouse makes it easier to store flexible attributes and perform dynamic queries without altering the schema. Next steps: create appropriate indexes/partitions, transform maps to fixed columns when necessary, and test query performance on real workloads. Tip: before scaling, validate average Map size to avoid small files or unnecessary reads.