encode method

Map<String, Object?> encode(
  1. RecordBatch batch
)

Implementation

Map<String, Object?> encode(RecordBatch batch) {
  if (batch.columns.any((column) => column.nestedDictionaries.isNotEmpty)) {
    throw UnsupportedError(
      'Nested dictionaries are supported by standalone Arrow IPC, '
      'not the Polars copied-batch protocol',
    );
  }
  final names = <String>{};
  for (final field in batch.schema.fields) {
    if (field.name.isEmpty || !names.add(field.name)) {
      throw UnsupportedError(
        'Polars copied batches require unique non-empty column names',
      );
    }
    _validateNativeType(field.type.toJson(), field.name);
  }
  final encoded = _codec.toJson(batch);
  final columns = (encoded['columns'] as List)
      .map((raw) {
        final column = Map<String, Object?>.from(raw as Map);
        var dtype = (column['dtype'] as Map).cast<String, Object?>();
        var values = column['values'] as List;
        if (dtype['kind'] == 'extension') {
          dtype = (dtype['storageType'] as Map).cast<String, Object?>();
          values = values
              .map((value) => value is Map ? value['storage'] : value)
              .toList();
        }
        column['dtype'] = _polarsDType(dtype);
        column['values'] = _polarsValues(dtype, values);
        return column;
      })
      .toList(growable: false);
  return {'length': batch.length, 'columns': columns};
}