tdfs4ds.genai.deep_tables¶
tdfs4ds.genai.deep_tables
¶
Deep (historian) source tables: documenting the table as if it were pivoted.
A deep table stores one row per (id columns, timestamp, tag) with the value in a payload column - a historian, an entity-attribute-value table. What a reader needs documented is each tag (each value of the tag column: a sensor, a signal), as if it were a column of the pivoted table.
Nothing here adds a table or alters one:
- the layout - which columns are the id columns, the tag, the payload and the
timestamp, and the timestamp's time zone - is a reserved section
DEEP_LAYOUTof the table in the business dictionary (JSON), like the agent'sFEATURE_GROUPS; - each tag is a
FS_BUSINESS_DICTIONARY_COLUMNSrow of the table whoseCOLUMN_NAMEis the tag value. A row is a tag when the table has a layout and the name is not a physical column. Provenance (IMPORTED / MANUAL), the log, the index and protection from LLM runs apply as to any column.
A table without a layout behaves exactly as before everywhere.
The LLM documenting a process that reads a deep table receives only the tags the
process's SQL names (its string and number literals), never the whole tag list:
see :func:scope_deep_context.
RESERVED_SECTION_NAMES
module-attribute
¶
RESERVED_SECTION_NAMES = ('FEATURE_GROUPS', DEEP_LAYOUT_SECTION, 'EXPOSES', 'ATTACHED', 'DOC_FINGERPRINT', 'DOC_CONFIDENCE')
_ROLES
module-attribute
¶
_UNIT_IN_NAME_RE
module-attribute
¶
_UNIT_IN_TEXT_RE
module-attribute
¶
_RESOLVE_NOISE
module-attribute
¶
_RESOLVE_NOISE = {'the', 'a', 'an', 'of', 'for', 'in', 'on', 'at', 'to', 'and', 'or', 'by', 'is', 'are', 'what', 'which', 'show', 'give', 'me', 'with', 'from', 'plot', 'chart', 'le', 'la', 'les', 'de', 'du', 'des', 'un', 'une', 'et', 'ou', 'pour', 'sur', 'par', 'dans', 'quel', 'quelle', 'montre'}
logger_safe
¶
Wrapper around the global logger that only emits logs when
tdfs4ds.DISPLAY_LOGS is True. level is a string like "info", "error", etc.
qualified
¶
Build db.obj with each part quoted only as needed.
qualified('TDB', 'FS_X') == 'TDB.FS_X' (unchanged for ordinary names);
qualified('SVC@CORP', 'FS_X') == '"SVC@CORP".FS_X'. An empty/None
database yields the bare object name, so unqualified callers keep working.
force=True quotes both parts unconditionally — only for the sites that
have always emitted quotes and whose exact text is already persisted (dataset
view DDL, returned volatile-table names).
sql_literal
¶
Escape value for use inside a single-quoted SQL string literal.
For the DBC probes and catalog predicates that compare a name as data
(WHERE DatabaseName = '…'). The caller supplies the surrounding quotes.
reserved_sections_sql
¶
'FEATURE_GROUPS', 'DEEP_LAYOUT' for a SECTION_NAME NOT IN (...) predicate.
build_layout
¶
build_layout(physical: Iterable[str], *, existing: Optional[dict] = None, id_columns=None, tag_column=None, payload_column=None, timestamp_column=None, timestamp_timezone: Optional[str] = None) -> dict
The layout of a deep table, checked against its physical columns.
Values not given are taken from existing (a later call can change one role). The tag, payload and timestamp columns are required; the id columns may be empty. Column names are returned with the table's own spelling.
parse_layout
¶
The layout stored in the DEEP_LAYOUT section, or None (absent / unreadable).
describe_layout
¶
One sentence for a reader or an LLM: the grain, where the value is, the time zone.
tag_name_problem
¶
Why name cannot be stored as a tag, or None.
tags_from_input
¶
tags_from_input(tags, tag_name_column=None, tag_text_column=None, unit_column=None) -> Dict[str, str]
{tag: text} from a dict or a DataFrame (tag, description [, unit]).
sql_literals
¶
Every string literal and bare number of sql, upper-cased, comments removed.
This is deliberately shape-blind: a tag can be named in a CASE, an IN list, a PIVOT, a join condition or a QUALIFY - the literal is what they share.
tags_named
¶
The documented tags that sql names, in the documented spelling.
scope_deep_context
¶
scope_deep_context(ctx: dict, consumer_sql: str, layout: dict, physical: Iterable[str], max_tags: Optional[int] = None) -> dict
The upstream context of a deep table, reduced to what consumer_sql uses.
Physical columns keep their documentation; tags are kept only when the SQL
names them. When it names none (tags chosen through a join, or the tag kept
as a row), at most max_tags tags are kept so the LLM sees the shape without
the whole list. Returns a new dict with deep set; the input is unchanged.
undocumented_tags_named
¶
String literals compared with the tag column (=, <>, IN (...)) that have no documentation.
Best effort, for a log line telling the user which tags to document next; it never decides what the LLM receives.
format_deep_context_lines
¶
Prompt lines for a deep table (used by _format_upstream_context).
read_layout
¶
The stored layout of db.obj, or None. Fail-open (None) on any error.
physical_columns
¶
The table's columns (DBC), in order. Raises when the object does not exist.
deep_context
¶
ctx scoped for consumer_sql when its object is a deep table; else ctx unchanged.
tag_unit
¶
The unit a tag's documentation states: in brackets at the end of its name ("Length (m)"), or
written "unit: m" in its description. None when it states none. Pure.
deep_table_tag_documentation
¶
Each documented tag of a deep table with its description and unit: a frame TAG_NAME,
DESCRIPTION, UNIT. Reads the documentation and DBC only, never the table itself, so it costs
the same on any historian (:func:deep_table_tags scans the table, and returns no description).
documented_columns
¶
{column or tag: description} from the business dictionary. A table can have its tags
documented and no description of its own, so this does not go through the object's row.
_catalogue_rows
¶
[{tag, description, unit}] from the frame of :func:deep_table_tag_documentation, a
{tag: description} dict, or a list of such dicts.
resolve_tags
¶
The tags question means, from the documentation in catalogue (no database read, no language
model): [{tag, description, unit, score, matched, how}], best first.
A tag the question names wins (how='name', :func:tdfs4ds.agent.plot_skill.match_tags). Otherwise
(how='description') a tag scores on the question's words found in its name or description, a word
counting more the fewer tags have it; a word every tag has counts for nothing. score is relative
to the best (1.0), and tags below half of it are left out. Several results of similar score mean
the question does not decide: ask. Empty when nothing matches. Pure.
deep_table_tags
¶
deep_table_tags(database: str, table: str, *, since=None, until=None, documented: Optional[bool] = None)
The tags present in a deep table, with their row counts and first/last timestamps.
One GROUP BY over the table, bounded by since / until on the timestamp
column when given (interpreted in the layout's time zone, UTC by default).
This reads the table: on a large historian, give a window. It is never run
by tdfs4ds on its own.
documented=False keeps the tags without documentation, True the
documented ones; None keeps all, with a DOCUMENTED column.