File size: 9,499 Bytes
ebcde1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Versioned normalized Arrow schemas and their temporal contract.

All timestamps are signed UTC epoch nanoseconds.  ``available_ts_ns`` is the
earliest time at which a row may enter a research information set.  Archive
rows explicitly identify exchange event time as a proxy; live rows use local
receipt time.  Prices and quantities retain exact integer tick/lot columns and
also expose documented floating convenience columns for research consumers.
"""

from __future__ import annotations

from collections.abc import Iterable, Mapping
from typing import Any

import pyarrow as pa  # type: ignore[import-untyped]

SCHEMA_VERSION = "1.0.0"


class SchemaError(ValueError):
    """Raised when a normalized table violates its declared schema."""


def _metadata(name: str) -> dict[bytes, bytes]:
    return {
        b"schema_name": name.encode(),
        b"schema_version": SCHEMA_VERSION.encode(),
        b"timestamp_unit": b"UTC epoch nanoseconds",
        b"temporal_contract": (
            b"available_ts_ns is the information-set clock; event_ts_ns alone is not receipt proof"
        ),
        b"numeric_contract": (
            b"price_ticks and quantity_lots are exact; float columns are convenience units"
        ),
    }


_COMMON_EVENT_FIELDS = [
    pa.field("schema_version", pa.string(), nullable=False),
    pa.field("venue", pa.string(), nullable=False),
    pa.field("symbol", pa.string(), nullable=False),
    pa.field("event_ts_ns", pa.int64(), nullable=False),
    pa.field("received_ts_ns", pa.int64()),
    pa.field("available_ts_ns", pa.int64(), nullable=False),
    pa.field("availability_basis", pa.string(), nullable=False),
    pa.field("capture_seq", pa.int64()),
    pa.field("continuity_id", pa.string()),
]


TRADE_SCHEMA = pa.schema(
    [
        *_COMMON_EVENT_FIELDS,
        pa.field("trade_id", pa.int64(), nullable=False),
        pa.field("first_trade_id", pa.int64()),
        pa.field("last_trade_id", pa.int64()),
        pa.field("price_ticks", pa.int64(), nullable=False),
        pa.field("quantity_lots", pa.int64(), nullable=False),
        pa.field("tick_size", pa.float64(), nullable=False),
        pa.field("lot_size", pa.float64(), nullable=False),
        pa.field("price", pa.float64(), nullable=False),
        pa.field("quantity", pa.float64(), nullable=False),
        pa.field("quote_quantity", pa.float64(), nullable=False),
        pa.field("aggressor_side", pa.string(), nullable=False),
        pa.field("buyer_is_maker", pa.bool_(), nullable=False),
        pa.field("source_artifact_id", pa.string(), nullable=False),
    ],
    metadata=_metadata("trades"),
)


BOOK_OBSERVATION_SCHEMA = pa.schema(
    [
        *_COMMON_EVENT_FIELDS,
        pa.field("sequence_start", pa.int64(), nullable=False),
        pa.field("sequence_end", pa.int64(), nullable=False),
        pa.field("is_valid", pa.bool_(), nullable=False),
        pa.field("best_bid_ticks", pa.int64(), nullable=False),
        pa.field("best_ask_ticks", pa.int64(), nullable=False),
        pa.field("bid_quantity_lots", pa.int64(), nullable=False),
        pa.field("ask_quantity_lots", pa.int64(), nullable=False),
        pa.field("tick_size", pa.float64(), nullable=False),
        pa.field("lot_size", pa.float64(), nullable=False),
        pa.field("best_bid", pa.float64(), nullable=False),
        pa.field("best_ask", pa.float64(), nullable=False),
        pa.field("bid_quantity", pa.float64(), nullable=False),
        pa.field("ask_quantity", pa.float64(), nullable=False),
        pa.field("spread", pa.float64(), nullable=False),
        pa.field("mid_price", pa.float64(), nullable=False),
        pa.field("microprice", pa.float64(), nullable=False),
        pa.field("depth_bid_1", pa.float64(), nullable=False),
        pa.field("depth_ask_1", pa.float64(), nullable=False),
        pa.field("depth_bid_5", pa.float64(), nullable=False),
        pa.field("depth_ask_5", pa.float64(), nullable=False),
        pa.field("depth_bid_10", pa.float64(), nullable=False),
        pa.field("depth_ask_10", pa.float64(), nullable=False),
        pa.field("queue_imbalance_1", pa.float64(), nullable=False),
        pa.field("queue_imbalance_5", pa.float64(), nullable=False),
        pa.field("queue_imbalance_10", pa.float64(), nullable=False),
        pa.field("source_artifact_id", pa.string(), nullable=False),
    ],
    metadata=_metadata("book_observations"),
)


LEVEL_TYPE = pa.struct(
    [
        pa.field("price_ticks", pa.int64(), nullable=False),
        pa.field("quantity_lots", pa.int64(), nullable=False),
    ]
)


DEPTH_DELTA_SCHEMA = pa.schema(
    [
        *_COMMON_EVENT_FIELDS,
        pa.field("first_update_id", pa.int64(), nullable=False),
        pa.field("last_update_id", pa.int64(), nullable=False),
        pa.field("previous_update_id", pa.int64()),
        pa.field("bids", pa.list_(LEVEL_TYPE), nullable=False),
        pa.field("asks", pa.list_(LEVEL_TYPE), nullable=False),
        pa.field("tick_size", pa.float64(), nullable=False),
        pa.field("lot_size", pa.float64(), nullable=False),
        pa.field("source_artifact_id", pa.string(), nullable=False),
    ],
    metadata=_metadata("depth_deltas"),
)


BOOK_SNAPSHOT_SCHEMA = pa.schema(
    [
        pa.field("schema_version", pa.string(), nullable=False),
        pa.field("venue", pa.string(), nullable=False),
        pa.field("symbol", pa.string(), nullable=False),
        pa.field("snapshot_id", pa.string(), nullable=False),
        pa.field("request_ts_ns", pa.int64(), nullable=False),
        pa.field("received_ts_ns", pa.int64(), nullable=False),
        pa.field("available_ts_ns", pa.int64(), nullable=False),
        pa.field("continuity_id", pa.string(), nullable=False),
        pa.field("last_update_id", pa.int64(), nullable=False),
        pa.field("depth_limit", pa.int32(), nullable=False),
        pa.field("bids", pa.list_(LEVEL_TYPE), nullable=False),
        pa.field("asks", pa.list_(LEVEL_TYPE), nullable=False),
        pa.field("tick_size", pa.float64(), nullable=False),
        pa.field("lot_size", pa.float64(), nullable=False),
        pa.field("source_artifact_id", pa.string(), nullable=False),
    ],
    metadata=_metadata("book_snapshots"),
)


SEQUENCE_GAP_SCHEMA = pa.schema(
    [
        pa.field("schema_version", pa.string(), nullable=False),
        pa.field("venue", pa.string(), nullable=False),
        pa.field("symbol", pa.string(), nullable=False),
        pa.field("continuity_id", pa.string(), nullable=False),
        pa.field("expected_sequence", pa.int64(), nullable=False),
        pa.field("observed_sequence_start", pa.int64(), nullable=False),
        pa.field("observed_sequence_end", pa.int64(), nullable=False),
        pa.field("missing_start", pa.int64(), nullable=False),
        pa.field("missing_end", pa.int64(), nullable=False),
        pa.field("detected_ts_ns", pa.int64(), nullable=False),
        pa.field("reason", pa.string(), nullable=False),
        pa.field("source_artifact_id", pa.string(), nullable=False),
    ],
    metadata=_metadata("sequence_gaps"),
)


SCHEMAS: Mapping[str, pa.Schema] = {
    "trades": TRADE_SCHEMA,
    "book_observations": BOOK_OBSERVATION_SCHEMA,
    "depth_deltas": DEPTH_DELTA_SCHEMA,
    "book_snapshots": BOOK_SNAPSHOT_SCHEMA,
    "sequence_gaps": SEQUENCE_GAP_SCHEMA,
}


def get_schema(name: str, version: str = SCHEMA_VERSION) -> pa.Schema:
    """Return a schema by stable name and fail closed on unknown versions."""
    if version != SCHEMA_VERSION:
        raise SchemaError(f"unsupported schema version {version!r}; expected {SCHEMA_VERSION!r}")
    try:
        return SCHEMAS[name]
    except KeyError as exc:
        raise SchemaError(f"unknown normalized schema: {name!r}") from exc


def table_from_records(name: str, records: Iterable[Mapping[str, Any]]) -> pa.Table:
    """Construct a table using the registry rather than inferred Arrow types."""
    schema = get_schema(name)
    try:
        return pa.Table.from_pylist(list(records), schema=schema)
    except (pa.ArrowException, TypeError, ValueError) as exc:
        raise SchemaError(f"records do not conform to {name} {SCHEMA_VERSION}: {exc}") from exc


def ensure_schema(table: pa.Table | pa.RecordBatch, name: str) -> None:
    """Require exact field order/types/nullability; metadata may be absent on batches."""
    expected = get_schema(name)
    actual = table.schema
    if not actual.equals(expected, check_metadata=False):
        raise SchemaError(f"schema mismatch for {name}: expected {expected}, got {actual}")
    metadata = actual.metadata or {}
    declared_name = metadata.get(b"schema_name")
    declared_version = metadata.get(b"schema_version")
    if declared_name is not None and declared_name != name.encode():
        raise SchemaError(
            f"schema metadata name mismatch: expected {name!r}, got {declared_name.decode()}"
        )
    if declared_version is not None and declared_version != SCHEMA_VERSION.encode():
        raise SchemaError(
            "schema metadata version mismatch: "
            f"expected {SCHEMA_VERSION!r}, got {declared_version.decode()}"
        )
    version_column = table.column(actual.get_field_index("schema_version"))
    observed_versions = set(version_column.to_pylist())
    if observed_versions.difference({SCHEMA_VERSION}):
        raise SchemaError(
            f"row schema_version mismatch: expected only {SCHEMA_VERSION!r}, "
            f"got {sorted(observed_versions)!r}"
        )