Map Types
Map types store key-value pairs within a single column. Support varies by database.
Database Supportβ
| Database | Mechanism | Status |
|---|---|---|
| PostgreSQL | hstore extension | Mapped to Map<String, String> |
| DuckDB | MAP(key, value) | Full support with typed Map<K, V> |
| MariaDB | - | No map support |
| Oracle | - | No map support |
| SQL Server | - | No map support |
| DB2 | - | No map support |
PostgreSQL hstoreβ
PostgreSQL's hstore extension provides key-value storage with string keys and values:
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE TABLE product (
id SERIAL PRIMARY KEY,
attributes hstore
);
INSERT INTO product (attributes)
VALUES ('color => red, size => large');
Typr maps hstore to Map<String, String>:
// Java
Map<String, String> attributes = product.attributes();
// Kotlin
val attributes: Map<String, String>? = product.attributes
// Scala
val attributes: Option[Map[String, String]] = product.attributes
DuckDB MAPβ
DuckDB supports typed MAP columns with arbitrary key and value types:
CREATE TABLE sensor_readings (
sensor_id UUID PRIMARY KEY,
hourly_temps MAP(INTEGER, DOUBLE), -- hour -> temperature
daily_counts MAP(DATE, INTEGER) -- date -> count
);
Typr generates typed Map<K, V> with full read/write support:
// Java - typed map access
Map<Integer, Double> temps = reading.hourlyTemps();
Double noonTemp = temps.get(12);
Map<LocalDate, Integer> counts = reading.dailyCounts();
// Kotlin
val temps: Map<Int, Double> = reading.hourlyTemps
val noonTemp = temps[12]
// Scala
val temps: Map[Int, Double] = reading.hourlyTemps
val noonTemp = temps.get(12)
DuckDB MAPs support any combination of key and value types that DuckDB supports.
Databases Without Map Supportβ
For databases without native map types, alternatives include:
- JSON columns - Store maps as JSON objects
- Separate tables - Use a key-value table with foreign keys
- Delimited strings - Simple but limited (no type safety)