ZipLime commited on
Commit
ea05f2a
·
verified ·
1 Parent(s): e5e8db3

Build the settlement records a frame at a time

Browse files

collect_ftd extended one list of dicts across all 411 fails-to-deliver archives -- 22.9 million records, some fifteen gigabytes of Python objects, and the job was OOM-killed at that line on 16 GB. Each archive now becomes a frame as it arrives and they are concatenated once; the same records as Arrow are under two gigabytes. Same rows, same order.

Files changed (1) hide show
  1. recipe/build.py +30 -9
recipe/build.py CHANGED
@@ -45,27 +45,48 @@ def _write(data_dir: Path, table: str, frame: pl.DataFrame) -> int:
45
  return frame.height
46
 
47
 
 
 
 
 
 
 
 
 
 
 
 
48
  async def collect_ftd(client, *, limit: int | None = None) -> pl.DataFrame:
49
- """Every settlement record SEC currently publishes, as CUSIP-ticker pairs."""
 
 
 
 
 
 
 
 
50
  urls = await ftd_archives(client)
51
  if limit:
52
  urls = urls[-limit:]
53
  LOGGER.info("fails-to-deliver archives: %d", len(urls))
54
- rows: list[dict] = []
 
55
  for index, url in enumerate(urls, 1):
56
  try:
57
  payload = await client.get_bytes(url, timeout=180.0)
58
  except Exception as exc: # a withdrawn file is not a reason to stop
59
  LOGGER.warning("skipping %s (%s)", url.rsplit("/", 1)[-1], type(exc).__name__)
60
  continue
61
- rows.extend(parse_ftd(payload, source_ref=url.rsplit("/", 1)[-1]))
 
 
 
62
  if index % 40 == 0:
63
- LOGGER.info(" %d/%d archives, %d records", index, len(urls), len(rows))
64
- if not rows:
65
- return pl.DataFrame(schema={"cusip": pl.String, "ticker": pl.String,
66
- "issuer_name": pl.String, "observed_on": pl.Date,
67
- "source_ref": pl.String})
68
- return pl.DataFrame(rows, strict=False)
69
 
70
 
71
  async def run_build(
 
45
  return frame.height
46
 
47
 
48
+ # What one settlement record carries. Named because `collect_ftd` builds a
49
+ # frame per archive and needs them all to agree before they are concatenated.
50
+ FTD_SCHEMA: dict[str, pl.DataType] = {
51
+ "cusip": pl.String,
52
+ "ticker": pl.String,
53
+ "issuer_name": pl.String,
54
+ "observed_on": pl.Date,
55
+ "source_ref": pl.String,
56
+ }
57
+
58
+
59
  async def collect_ftd(client, *, limit: int | None = None) -> pl.DataFrame:
60
+ """Every settlement record SEC currently publishes, as CUSIP-ticker pairs.
61
+
62
+ Each archive becomes a frame as it arrives, rather than extending one list
63
+ of dicts across all four hundred of them. Twenty-three million records held
64
+ as Python dicts is some fifteen gigabytes and was getting this job
65
+ OOM-killed; the same records as Arrow are under two, because a dict of five
66
+ short strings costs an order of magnitude more than the strings do.
67
+ """
68
+
69
  urls = await ftd_archives(client)
70
  if limit:
71
  urls = urls[-limit:]
72
  LOGGER.info("fails-to-deliver archives: %d", len(urls))
73
+ frames: list[pl.DataFrame] = []
74
+ records = 0
75
  for index, url in enumerate(urls, 1):
76
  try:
77
  payload = await client.get_bytes(url, timeout=180.0)
78
  except Exception as exc: # a withdrawn file is not a reason to stop
79
  LOGGER.warning("skipping %s (%s)", url.rsplit("/", 1)[-1], type(exc).__name__)
80
  continue
81
+ rows = list(parse_ftd(payload, source_ref=url.rsplit("/", 1)[-1]))
82
+ if rows:
83
+ frames.append(pl.DataFrame(rows, schema=FTD_SCHEMA, strict=False))
84
+ records += len(rows)
85
  if index % 40 == 0:
86
+ LOGGER.info(" %d/%d archives, %d records", index, len(urls), records)
87
+ if not frames:
88
+ return pl.DataFrame(schema=FTD_SCHEMA)
89
+ return pl.concat(frames, how="vertical_relaxed")
 
 
90
 
91
 
92
  async def run_build(