|
| 1 | +# (C) 2025 GoodData Corporation |
| 2 | +from collections.abc import Iterable |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, Optional |
| 5 | + |
| 6 | +import orjson |
| 7 | +import pyarrow |
| 8 | + |
| 9 | +_DATA_SOURCE_MESSAGES_KEY = b"x-gdc-data-source-messages" |
| 10 | +""" |
| 11 | +Key used in the PyArrow metadata to carry the data source messages. |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +@dataclass(frozen=True) |
| 16 | +class DataSourceMessage: |
| 17 | + """ |
| 18 | + A message sent by the data source. This will be included in the execution results' metadata. |
| 19 | + """ |
| 20 | + |
| 21 | + source: str |
| 22 | + """ |
| 23 | + Identification of the source of the message, typically the id or other identifier of the data source. |
| 24 | + """ |
| 25 | + |
| 26 | + correlation_id: str |
| 27 | + """ |
| 28 | + Unique id of the operation, meant to correlate different messages together. |
| 29 | + """ |
| 30 | + |
| 31 | + type: str |
| 32 | + """ |
| 33 | + Type of the message, currently free-form, we might define some enum for these in the future. |
| 34 | + """ |
| 35 | + |
| 36 | + data: Optional[Any] = None |
| 37 | + """ |
| 38 | + Optional message-specific data. This can be anything that can be JSON-serialized. |
| 39 | + Try to keep this as small as possible: the backend has a quite strict size limit on the messages. |
| 40 | + """ |
| 41 | + |
| 42 | + def to_dict(self) -> dict[str, Any]: |
| 43 | + """ |
| 44 | + Converts this instance to its dictionary representation. |
| 45 | + """ |
| 46 | + return {"source": self.source, "correlation_id": self.correlation_id, "type": self.type, "data": self.data} |
| 47 | + |
| 48 | + |
| 49 | +def add_data_source_messages_metadata( |
| 50 | + data_source_messages: Iterable[DataSourceMessage], original_metadata: Optional[dict] = None |
| 51 | +) -> dict[bytes, bytes]: |
| 52 | + """ |
| 53 | + Given a list of DataSourceMessages, creates a PyArrow-compatible metadata dictionary. |
| 54 | +
|
| 55 | + This is useful when creating a PyArrow Table directly: |
| 56 | +
|
| 57 | + >>> t = pyarrow.table( |
| 58 | + ... [1, 2, 3], |
| 59 | + ... ["test"], |
| 60 | + ... metadata=add_data_source_messages_metadata( |
| 61 | + ... [DataSourceMessage(correlation_id="123", source="test_source", type="info")] |
| 62 | + ... ), |
| 63 | + ... ) |
| 64 | +
|
| 65 | + You can also add other metadata as needed: |
| 66 | + >>> t2 = pyarrow.table( |
| 67 | + ... [1, 2, 3], |
| 68 | + ... ["test"], |
| 69 | + ... metadata=add_data_source_messages_metadata( |
| 70 | + ... [DataSourceMessage(correlation_id="123", source="test_source", type="info")], |
| 71 | + ... original_metadata={b"some": b"extra metadata"}, |
| 72 | + ... ), |
| 73 | + ... ) |
| 74 | +
|
| 75 | + :param data_source_messages: list of DataSourceMessages to include |
| 76 | + :param original_metadata: original medata to add the DataSourceMessages to. |
| 77 | + :return: a new metadata dictionary |
| 78 | + """ |
| 79 | + if original_metadata is None: |
| 80 | + original_metadata = {} |
| 81 | + return {**original_metadata, _DATA_SOURCE_MESSAGES_KEY: orjson.dumps(data_source_messages)} |
| 82 | + |
| 83 | + |
| 84 | +def with_data_source_messages( |
| 85 | + data_source_messages: Iterable[DataSourceMessage], schema: pyarrow.Schema |
| 86 | +) -> pyarrow.Schema: |
| 87 | + """ |
| 88 | + Given a schema and a list of DataSourceMessages, returns a new schema with the DataSourceMessages included. |
| 89 | +
|
| 90 | + This is useful when creating PyArrow RecordBatchReaders: |
| 91 | +
|
| 92 | + >>> s = with_data_source_messages( |
| 93 | + ... [DataSourceMessage(correlation_id="123", source="test_source", type="info")], |
| 94 | + ... pyarrow.schema(...), # the original schema for your RecordBatchReader |
| 95 | + ... ) |
| 96 | + >>> rbr = pyarrow.RecordBatchReader.from_batches(schema=s, batches=...) |
| 97 | +
|
| 98 | + :param schema: the schema to enrich with the DataSourceMessages |
| 99 | + :param data_source_messages: list of DataSourceMessages to include |
| 100 | + """ |
| 101 | + return schema.with_metadata(add_data_source_messages_metadata(data_source_messages, schema.metadata)) |
0 commit comments