xlsongc opened a new pull request, #77478: URL: https://github.com/apache/airflow/pull/77478
--- related: #77259 This is the second of two follow-ups from #77259, after #77475: a limit for the whole DOCX document. The DOCX table limits from #73926 apply per table, so the output still grows with the number of tables: 100 copies of an 8-row merge nested 6 deep fit in a 39 KB file and produce 78.6M characters. And as noted in #77475, 7 million empty paragraphs take about a minute to read while producing no text at all. Reading now stops with a warning when the text passes 10,000,000 characters, the same default as Apache POI's `ZipSecureFile.DEFAULT_MAX_TEXT_SIZE`, or when the document has more than 1,000,000 paragraphs and table cells. The text read up to that point is kept and the rest of the document is left out. This follows the existing skip-with-warning behaviour for tables instead of failing the task, so one oversized file in directory mode does not lose the other files. In about 1,900 real files (the Apache POI and LibreOffice test corpora plus local Word files), the most were 454K characters and 29K paragraphs and cells, and none of their output changed. As suggested in the #73926 review, the budget is charged per cell rather than per row, so a cell holding many nested tables stops as soon as the budget is spent rather than after all of its tables are built. Each paragraph's text is counted as it is read, and each table cell counts toward the paragraph-and-cell limit. A vertically merged cell counts its text again on every row it repeats on, since that is where small files grow. Each table row also counts 3 characters of separators per cell, as the per-table limit already does, so padded empty cells count. The per-table limits are unchanged, since they skip only the table and go on reading. The stop is raised as a private exception that is not a `ValueError`, because `ValueError` is what the table reader catches to skip just the current table. Measured with Python 3.12, median of 3 runs, peak memory excluding the input bytes: | File | Before | After | |---|---|---| | 300 pages of text | 0.12 s, 19 MB, 1,023,933 chars | 0.10 s, 19 MB, same output | | 6173 x 10 table | 2.52 s, 110 MB, 710,078 chars | 2.40 s, 112 MB, same output | | Largest real file found (POI `bug65649.docx`) | 0.70 s, 231 MB, 453,824 chars | 0.72 s, 230 MB, same output | | 100 copies of an 8-row merge nested 6 deep | 0.16 s, 168 MB, 78,645,698 chars | 0.05 s, 34 MB, 9,437,482 chars | | The same 100 tables inside one cell | 0.16 s, 167 MB, 0 chars (outer table skipped) | 0.05 s, 25 MB, 0 chars | | 7 million empty paragraphs | 64.4 s, 1365 MB | 11.7 s, 1362 MB | | 2 million empty table cells | 52.5 s, 590 MB | 14.3 s, 590 MB | The memory for the empty paragraphs goes to parsing the XML, which #77475 bounds. The tests patch both limits down and check where reading stops: text over the limit, the limit reached inside a table (the paragraph after it is left out too), empty paragraphs, table cells, text repeated by a vertical merge, and a cell whose second nested table spends the budget, so its other tables are never read. Each test fails without the change. This PR and #77475 both add code after `_DOCX_TABLE_MAX_CHARS` and a paragraph at the same place in the docs. Merged, they conflict only there, and keeping both blocks passes all 102 document loader tests. Verification: the document loader tests pass on the host with Python 3.12 and in breeze with Python 3.11. `prek` pre-commit and manual hooks and `breeze run mypy` pass on the changed files. `breeze build-docs common.ai` builds with no spelling errors; the only warnings are for the existing mermaid diagrams, because mermaid-cli would not install in my local CI image. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes (please specify the tool below) Generated-by: Claude Code (Opus 5.5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
