Skip to content

Fixes #37541: Doris primary keys follow the table model - #37618

Merged
akashverma0786 merged 6 commits into
open-metadata:mainfrom
morningman:doris-primary-key-constraints
Nov 25, 2026
Merged

akashverma0786 merged 6 commits into
open-metadata:mainfrom
morningman:doris-primary-key-constraints

Conversation

@morningman

@morningman morningman commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #37541

The Doris connector marked a column PRIMARY KEY when SHOW FULL COLUMNS reported Key=YES. Doris sets that flag on the key columns of every table model, including the sort key of a DUPLICATE KEY table, which can repeat, and on every column of an external table. So the first sort column of a DUPLICATE KEY table became a primary key, and a composite key lost both the table-level constraint and the NOT NULL of its columns.

This change reads TABLE_MODEL and TABLE_MODEL_KEY from information_schema.table_options, once per schema:

  • UNIQUE KEY and AGGREGATE KEY tables get a primary key on their key columns: on the column for a single key column, and as a table-level constraint for a composite key, the same split SqlColumnHandlerMixin makes for other connectors. An AGGREGATE KEY table merges the rows that share a key, so its key is unique as well.
  • The legacy PRIMARY KEY model (PRI) is treated the same way.
  • DUPLICATE KEY tables get no primary key.
  • If table_options cannot be read (for example a Doris version without it), a warning is logged and the schema's tables are ingested without primary keys instead of failing. The thread's connection is rolled back, as the partition lookups do, so a lost connection does not fail the statements that follow.
  • The table models are cached with one entry per ingestion thread, since each of the threads workers ingests its own schema.
  • The columns of a composite key keep NULL / NOT NULL.
  • The connector matches TABLE_MODEL_KEY against the complete leading column names from SHOW FULL COLUMNS in schema order. This preserves commas inside key column names.

The query uses DISTINCT because Doris master returns one row per partition in table_options (apache/doris#56122); released versions return one row per table.

Part of #37537.

Type of change:

  • Bug fix

High-level design:

N/A — small change.

Tests:

Use cases covered

  • A UNIQUE KEY table with the composite key (region, user_id) gets a table-level PRIMARY KEY, and its key columns stay NOT NULL.
  • An AGGREGATE KEY table gets a table-level PRIMARY KEY on its key columns.
  • A DUPLICATE KEY table gets no primary key, with one sort column or several.
  • A single-column UNIQUE KEY still gets the column-level PRIMARY_KEY constraint.
  • Single-column and composite keys preserve names such as x,y, including when the table also has separate x and y columns.
  • A PRIMARY KEY (PRI) model table gets a table-level PRIMARY KEY.
  • A failing table_options query keeps the tables, without primary keys, and is not retried per table.
  • A lost connection during the table_options query is rolled back, so the next table's statements still run.

Unit tests

  • I added unit tests for the new/changed logic.
  • File added: ingestion/tests/unit/source/database/doris/test_doris_constraints.py
    • a table-driven test that runs DorisSource.get_columns_and_constraints with SHOW FULL COLUMNS and information_schema.table_options response rows (only the database connection is replaced), for UNIQUE, AGGREGATE and DUPLICATE KEY tables;
    • a test that information_schema.table_options is queried once per schema, also with two threads;
    • a test that a failing table_options query falls back to no primary keys;
    • a test that a lost connection during that query is rolled back, so the next table is still read.
  • Two tests merged after this PR was opened are adjusted to it:
    • test_doris_partitions.py: _get_columns returns the columns only (the partition code that calls it is updated the same way);
    • test_doris_column_types.py: the IPV4/IPV6 test answered every statement with SHOW FULL COLUMNS rows, the new table_options query included; it now gets no row for that query.
  • On main, 8 of the original 10 tests fail. The lost-connection test passes on main, which does not query table_options. Without the rollback, it fails with PendingRollbackError.
  • Both new tests for key column names containing commas fail on f05c77ba6a4 and pass with the fix.
  • pytest tests/unit/source/database/doris tests/unit/topology/database/test_doris.py: 129 passed.
  • Coverage for doris/metadata.py: 92%.

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  1. Started a local Apache Doris master build with a test database covering the three table models (UNIQUE, AGGREGATE, DUPLICATE KEY), partitioned and unpartitioned tables, views and an asynchronous materialized view. Also checked that Doris 4.1.3 returns the same TABLE_MODEL / TABLE_MODEL_KEY values, one row per table.
  2. Ran the Doris source's yield_table against it with a mocked OpenMetadata client, before and after this change:
    • before: no table-level constraints; the first sort column of six DUPLICATE KEY tables (one of them the materialized view) was marked PRIMARY_KEY; the key columns of t_uniq (region, user_id), t_agg (dt, site) and t_types (k_date, k_id) had no constraint at all;
    • after: t_uniq has PRIMARY KEY (region, user_id) and t_agg PRIMARY KEY (dt, site); no DUPLICATE KEY table has a primary key; every key column is NOT NULL; nothing else in the output changed.
  3. An account with only SELECT_PRIV sees the same table_options rows.
  4. make py_format_check and make static-checks pass (0 type errors).

Known limitation: on Doris master, a table that has no partition yet (for example an empty AUTO PARTITION table) has no row in table_options, so it gets no primary key until that is fixed on the Doris side. Released versions are not affected.

UI screen recording:

Not applicable — ingestion-only bug fix with no UI change.

Checklist:

  • I have read the CONTRIBUTING document.

  • My PR title is Fixes <issue-number>: <short explanation>

  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.

  • I have commented on my code, particularly in hard-to-understand areas.

  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. (N/A: no schema change)

  • For a UI feature or feature task: as part of PR creation, I ran the PR build in Docker with sample data and embedded a verified GitHub-hosted recording plus build/health evidence above (or explained why this requirement is not applicable). (N/A: not a UI change)

  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

  • Self-review: I reviewed the final diff against REVIEW.md and fixed every 🔴 finding, or answered it on the line below.

  • Decision check: if this PR decides something the next contributor could undo, it adds a record under docs/decisions/. (N/A: bug fix, no durable decision)

  • I have added a test that covers the exact scenario we are fixing. For complex issues, comment the issue number in the test for future reference.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous disconnect issue is fixed.

Summary

Doris primary keys now follow the table model instead of the SHOW FULL COLUMNS key flag.

  • Doris tables now get primary keys from their table model.

No new actionable issues found.

Reviews (2) · Last reviewed commit: "Roll back the Doris connection after a f..." · Reviewed by Greptile

@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

morningman and others added 3 commits November 20, 2026 15:35
The Doris connector took the Key flag of SHOW FULL COLUMNS as the
primary key. Doris sets that flag on the key columns of every table
model, including the sort key of a DUPLICATE KEY table, and on every
column of an external table.

Read TABLE_MODEL and TABLE_MODEL_KEY from
information_schema.table_options once per schema instead. UNIQUE KEY
and AGGREGATE KEY tables get a primary key on their key columns,
DUPLICATE KEY tables get none, and the columns of a composite key keep
NULL / NOT NULL.
The table models were cached for 32 schemas, a size with no decision
record behind it. Each of the `threads` workers ingests one schema at
a time, so keep one entry per thread.

Log the warning for an unreadable table_options with lazy %-style
arguments: the suppression check of make py_format_check rejects a new
logging f-string (G004).
@morningman
morningman force-pushed the doris-primary-key-constraints branch from e3286be to 1b4a1cb Compare November 20, 2026 07:39
A lost connection leaves the thread's connection refusing every
statement until it is rolled back. When the table_options query lost
it, the table's description and definition could not be read, and the
schema's partition lookup failed and cached no partitions, so every
table of the schema was ingested without partition details.

Roll back the connection in that handler, as the partition lookups do.
@morningman

Copy link
Copy Markdown
Contributor Author

hi @ayush-shah PTAL

Match the table model key against complete leading column names in schema order. Cover single and composite keys with commas in their names.
@akashverma0786
akashverma0786 enabled auto-merge (squash) November 25, 2026 07:20
@gitar-bot

gitar-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 3 closed / 3 findings

Changing Doris constraint ingestion to use table models could misstate catalog primary keys if model metadata is missing or parsed incorrectly.

Doris primary keys now come from the table model in information_schema.table_options instead of the SHOW FULL COLUMNS key flag, so DUPLICATE KEY tables no longer get a primary key and UNIQUE, AGGREGATE and PRI model tables get one on their key columns. Resolved this session: a failing table_options query now keeps the schema's tables without primary keys, the primary key cache is sized by ingestion threads, and the connection is rolled back after a failed lookup. No issues remain open.

✅ 3 closed
✅ Edge Case: table_options query failure now drops every table in the schema

📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:247-258 📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:278
_get_primary_key_columns runs information_schema.table_options without error handling, and get_columns_and_constraints calls it for every table (metadata.py:278). If the query fails (an older Doris without table_options, missing privileges, a column renamed in a later version), the exception reaches yield_table in common_db_source.py and that table is not ingested. Because nothing is cached when the query fails, the query runs again and fails for every other table in the schema. Before this change, a missing primary key cost nothing, and now it costs the whole schema's tables. The fix is to catch the error, log a warning, and cache an empty mapping so ingestion goes ahead without primary keys.

✅ Quality: Unrecorded cap: Doris primary-key cache holds 32 schemas

📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:83-84 📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:184
The PR adds a new cache cap, PRIMARY_KEY_CACHE_SIZE = 32, and docs/decisions/ has no record covering it. No existing record mentions Doris primary keys or this cache. The value is a judgment call: per the comment at metadata.py:83, it is sized so each threads worker can keep its own schema. If a run uses more than 32 threads, workers evict each other's entries and table_options gets queried again. The rule a record would state: "The Doris connector caches each schema's table-model primary keys in an LRU of 32 schemas, enough for one entry per ingestion worker thread; information_schema.table_options is read once per schema." That sentence comes from the description's "information_schema.table_options is queried once per schema" and the code comment "with threads, each worker processes its own schema". Either add that record or tie the cap to the configured thread count.

✅ Bug: table_options failure doesn't roll back; next tables fail

📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:279-284 📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:387-395 📄 ingestion/src/metadata/ingestion/source/database/doris/metadata.py:256
When the DORIS_GET_TABLE_KEY_MODELS query fails, for example because the connection drops, the new except branch logs a warning and caches {}, but it does not call self._rollback_failed_statement(). _get_schema_partitions and get_table_partition_details both make that call. Its docstring explains why it matters: without the rollback, this thread's connection stays unusable, the next table's SHOW FULL COLUMNS (line 256) raises PendingRollbackError, and that table is dropped from the run. markDeletedTables can then delete it. This defeats the stated goal of ingesting the schema's tables without primary keys. Add the rollback call in the except branch.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@akashverma0786
akashverma0786 merged commit fb5e8e5 into open-metadata:main Nov 25, 2026
47 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Doris: key columns are mapped to PRIMARY KEY incorrectly and composite keys are lost

4 participants