Engineering · 1 min read
Spark SQL Data Types: Primitives, Nested, and struct vs map
Spark SQL's everyday data types (string, long, double, decimal(20,0), binary, timestamp), plus the nested trio array, map, and struct, and how struct differs from map.
The set of Spark types you’ll actually run into in real projects:
string
integer
array
struct
long
boolean
double
map
binary
float
decimal(20,0)
timestamp
date
Common types
| Type | Spark SQL name | Description |
|---|---|---|
| string | STRING / StringType | variable-length Unicode text |
| integer | INT / IntegerType | 4-byte signed integer |
| long | BIGINT / LongType | 8-byte signed integer |
| boolean | BOOLEAN / BooleanType | true / false |
| double | DOUBLE / DoubleType | 8-byte IEEE 754 double-precision float |
| float | FLOAT / FloatType | 4-byte single-precision float |
| decimal(20,0) | DECIMAL(20,0) | 20-digit exact number, 0 decimal places (i.e. an exact 20-digit integer), good for financial/precise values |
| timestamp | TIMESTAMP / TimestampType | date + time, microsecond precision, usually UTC |
| date | DATE / DateType | year-month-day only |
| binary | BINARY / BinaryType | variable-length byte array (raw binary) |
Nested types
| Type | Spark SQL name | Description |
|---|---|---|
| array | ARRAY<T> | ordered list, all elements the same type T |
| map | MAP<K,V> | key-value pairs; all keys share a type, all values share a type |
| struct | STRUCT<...> | a composite record with named fields, each field with its own type; each key-value pair can have its own type; more flexible than map (a subset) |
References
- Spark SQL Data Types: https://spark.apache.org/docs/latest/sql-ref-datatypes.html
Related: back to the Cross-Engine DB Query overview, or on to Spark array functions and collect.