Work with Map (Dictionary) data in Parquet
masterThe Parquet Map logical type is a special case of nested data. Because ParquetSharp's API works with leaf-level columns, .NET Dictionary objects cannot be represented as a single column. Instead, a Map is represented as two separate columns:
- A column containing arrays of keys.
- A column containing arrays of values.
For a valid Map, the arrays in both columns must have the same length for every row.
Writing Map data
To write a dictionary, you must split your Dictionary<K, V> collection into two arrays (keys and values) and define a schema with a GroupNode annotated with LogicalType.Map() containing a repeated key_value group.
Reading Map data
To read a Map, read the key column and the value column separately using LogicalReader, then use LINQ (e.g., Zip) to reconstruct the Dictionary objects in memory.
// Writing: Split dictionaries into keys and values
var dictionaries = new[] { new Dictionary<string, int>{{"a", 0}} };
var keys = dictionaries.Select(d => d.Keys.ToArray()).ToArray();
var values = dictionaries.Select(d => d.Values.ToArray()).ToArray();
// Schema for Map
using var keyNode = new PrimitiveNode("key", Repetition.Required, LogicalType.String(), PhysicalType.ByteArray);
using var valueNode = new PrimitiveNode("value", Repetition.Required, LogicalType.None(), PhysicalType.Int32);
using var keyValueNode = new GroupNode("key_value", Repetition.Repeated, new Node[] {keyNode, valueNode});
using var mapNode = new GroupNode("map_column", Repetition.Required, new Node[] {keyValueNode}, LogicalType.Map());
using var schema = new GroupNode("schema", Repetition.Required, new Node[] {mapNode});
// Reading: Reconstruct dictionaries
var readDictionaries = readKeys.Zip(
readValues, (dictKeys, dictValues) => new Dictionary<string, int>(
dictKeys.Zip(dictValues, (k, v) => new KeyValuePair<string, int>(k, v)))
).ToArray();