Skip to content

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_LAYOUT of the table in the business dictionary (JSON), like the agent's FEATURE_GROUPS;
  • each tag is a FS_BUSINESS_DICTIONARY_COLUMNS row of the table whose COLUMN_NAME is 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.

DEEP_LAYOUT_SECTION module-attribute

DEEP_LAYOUT_SECTION = 'DEEP_LAYOUT'

RESERVED_SECTION_NAMES module-attribute

RESERVED_SECTION_NAMES = ('FEATURE_GROUPS', DEEP_LAYOUT_SECTION, 'EXPOSES', 'ATTACHED')

_COLUMN_NAME_MAX module-attribute

_COLUMN_NAME_MAX = 128

_ROLES module-attribute

_ROLES = ('id_columns', 'tag_column', 'payload_column', 'timestamp_column')

_COMMENT_RE module-attribute

_COMMENT_RE = compile('--[^\\n]*|/\\*.*?\\*/', S)

_STRING_RE module-attribute

_STRING_RE = compile("'((?:[^']|'')*)'")

_NUMBER_RE module-attribute

_NUMBER_RE = compile('(?<![\\w.])(\\d+(?:\\.\\d+)?)(?![\\w.])')

_UNIT_IN_NAME_RE module-attribute

_UNIT_IN_NAME_RE = compile('[(\\[]\\s*([^()\\[\\]]{1,12}?)\\s*[)\\]]\\s*$')

_UNIT_IN_TEXT_RE module-attribute

_UNIT_IN_TEXT_RE = compile('\\b(?:units?|unit[eé]s?)\\s*[:=]\\s*([^\\s,;.)]+)', IGNORECASE)

_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

logger_safe(level, message, *args, **kwargs)

Wrapper around the global logger that only emits logs when tdfs4ds.DISPLAY_LOGS is True. level is a string like "info", "error", etc.

qualified

qualified(database, object_name, force=False)

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

sql_literal(value)

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

reserved_sections_sql() -> str

'FEATURE_GROUPS', 'DEEP_LAYOUT' for a SECTION_NAME NOT IN (...) predicate.

_names

_names(v) -> List[str]

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.

render_layout

render_layout(layout: dict) -> str

parse_layout

parse_layout(text) -> Optional[dict]

The layout stored in the DEEP_LAYOUT section, or None (absent / unreadable).

describe_layout

describe_layout(layout: dict, table_label: str = '') -> str

One sentence for a reader or an LLM: the grain, where the value is, the time zone.

tag_name_problem

tag_name_problem(name: str, physical: Iterable[str]) -> Optional[str]

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

sql_literals(sql: str) -> Set[str]

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

tags_named(sql: str, tag_names: Iterable[str]) -> List[str]

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

undocumented_tags_named(consumer_sql: str, layout: dict, documented: Iterable[str]) -> List[str]

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

format_deep_context_lines(ctx: dict) -> List[str]

Prompt lines for a deep table (used by _format_upstream_context).

read_layout

read_layout(db: str, obj: str) -> Optional[dict]

The stored layout of db.obj, or None. Fail-open (None) on any error.

physical_columns

physical_columns(db: str, obj: str) -> List[str]

The table's columns (DBC), in order. Raises when the object does not exist.

deep_context

deep_context(ctx: Optional[dict], consumer_sql: str) -> Optional[dict]

ctx scoped for consumer_sql when its object is a deep table; else ctx unchanged.

tag_unit

tag_unit(tag, description) -> Optional[str]

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

deep_table_tag_documentation(database: str, table: str)

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

documented_columns(db: str, obj: str) -> Dict[str, str]

{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

_catalogue_rows(catalogue) -> List[dict]

[{tag, description, unit}] from the frame of :func:deep_table_tag_documentation, a {tag: description} dict, or a list of such dicts.

_meaning_words

_meaning_words(text) -> Set[str]

resolve_tags

resolve_tags(question: str, catalogue, *, max_results: int = 5) -> List[dict]

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.