kaxil commented on code in PR #77478:
URL: https://github.com/apache/airflow/pull/77478#discussion_r4741450251
##########
providers/common/ai/docs/operators/document_loader.rst:
##########
@@ -95,6 +95,16 @@ including any tables nested in it, would be longer than
about 1,000,000
characters. If a skipped table is nested inside a cell, only that table is left
out.
+Reading stops with a warning once the text passes about 10,000,000 characters,
+or the document has more than 1,000,000 elements: paragraphs, runs of text,
+hyperlinks, tables, table rows and cells, including the empty cells that pad
out
+a row. The text read up to that point is kept, the rest of the document is left
+out, and the document's metadata gets ``"truncated": True``. A table that
reading
Review Comment:
`truncated` only appears when reading stops, so
`metadata_fields={"truncated": True}` fills it in on every complete document
too, and a downstream filter can't tell the two apart. Worth a line under
"Metadata precedence" saying not to use that key?
##########
providers/common/ai/src/airflow/providers/common/ai/operators/document_loader.py:
##########
@@ -56,6 +56,57 @@
# cells count too. 1M characters is about 6000 rows of 10 columns; longer
tables are skipped.
_DOCX_TABLE_MAX_CHARS = 1_000_000
+# Unlike the table limits above, these end the whole document: the text read
so far is kept and
+# the rest is left out with a warning. python-docx handles each paragraph,
run, hyperlink, table,
+# row and cell in Python, at about 10 us each even when it holds no text, so
each is counted as an
+# element before it is walked. The largest of about 2,300 real documents has
87,000 elements.
+_DOCX_MAX_CHARS = 10_000_000
+_DOCX_MAX_ELEMENTS = 1_000_000
+
+
+class _DocxLimitReachedError(Exception):
+ """Stop reading a DOCX. Not a ValueError, which only skips the table being
read."""
+
+
+class _DocxBudget:
+ """Count the characters and elements read from a DOCX against the document
limits."""
+
+ def __init__(self) -> None:
+ from docx.oxml.ns import nsmap
+ from lxml import etree
+
+ self.chars = 0
+ self.elements = 0
+ # Paragraph.text runs an XPath query for each w:r and w:hyperlink
child and each w:r in a
+ # hyperlink, so they are counted in C before it is read. The children
of a run cost about
+ # 0.4 us each and are not counted. The counts are added rather than
taken over a union,
+ # which libxml2 merges in quadratic time: 7M runs in hyperlinks took
98 s to count that way.
+ self._count_paragraph_parts = etree.XPath(
+ "count(w:r) + count(w:hyperlink) + count(w:hyperlink/w:r)",
namespaces=nsmap
+ )
+ # Rows and their cells are walked before any is read: once to find the
column count of a
+ # table without w:tblGrid, and once per row to sum the w:gridSpan of
its cells.
+ self._count_rows_and_cells = etree.XPath("count(w:tr) +
count(w:tr/w:tc)", namespaces=nsmap)
+
+ def charge(self, *, chars: int = 0, elements: int = 0) -> None:
+ self.chars += chars
+ self.elements += elements
+ if self.chars > _DOCX_MAX_CHARS:
+ raise _DocxLimitReachedError(f"its text exceeds {_DOCX_MAX_CHARS}
characters")
+ if self.elements > _DOCX_MAX_ELEMENTS:
+ raise _DocxLimitReachedError(
+ f"it has more than {_DOCX_MAX_ELEMENTS} paragraphs, runs,
tables, rows and cells"
+ )
+
+ def charge_table(self, table: Table) -> None:
+ self.charge(elements=1 + int(self._count_rows_and_cells(table._tbl)))
+
+ def get_paragraph_text(self, paragraph: Paragraph) -> str:
+ self.charge(elements=1 +
int(self._count_paragraph_parts(paragraph._p)))
+ text = paragraph.text
Review Comment:
Counting runs was my idea but it doesn't cover the unions python-docx reads
through (`w:r | w:hyperlink` here, a 6-way one in `CT_R.text`, `./w:p |
./w:tbl` in `iter_inner_content()`). libxml2 merges those quadratically. E.g.
one run with 160K `<w:t>x</w:t><w:tab/>` pairs is charged 2 elements and took
63 s in breeze, not truncated; mixing `w:p`/`w:tbl` in the body or
runs/hyperlinks in a paragraph is about as bad. Could these be walked with
`iterchildren(qn("w:r"), qn("w:hyperlink"))` etc., with `str(child)` for run
children?
--
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]