Skip to main content

SQL discovery rules

You may set discoveryFilters.rules (Discovery Rules) in the capture configuration to change which tables discovery includes and which collection it recommends for each table. For example, if you have a separate schema for each customer, you can capture every customer's orders table into a shared orders collection.

How discovery applies rules​

The connector first lists the tables that pass its existing discovery filters. For example, the table_patterns glob tenant_*.audit_* matches tenant_a.audit_logins and tenant_b.audit_changes. The filter excludes tenant_a.orders and internal.audit_logins before the connector applies any rules.

Each table on that list starts out included, with the collection name the connector would ordinarily recommend. If you leave the rules empty, discovery uses those defaults. If you supply rules, discovery applies them in order, then groups the included tables by destination. Each table in a discovered group has its own capture binding, even when several bindings write to the same collection.

Each rule has an action and a pattern. The pattern matches source names and is case-sensitive. For example, orders matches a table named orders in any schema. The pattern tenant_*.orders matches the same table name in schemas beginning with tenant_. Use * to match any number of characters, ? to match one, and \. for a period within a name (written as \\. in JSON).

ActionEffect on a matching table
includeIncludes the table, even if an earlier rule excluded it.
excludeExcludes the table unless a later rule includes it again.
mapSets the recommended destination. Requires a collection field.

Inclusion and destination are independent. For example, to discover just orders and invoices in customer schemas, first exclude *, then include tenant_*.orders and tenant_*.invoices. A subsequent map rule matching tenant_*.* can set the destination for both. Other tables in those schemas remain excluded.

Map tables to shared collections​

If each customer schema contains the same set of tables, you can use one collection per table name across all customers. This example also gives the sandbox schema separate collections.

{
"discoveryFilters": {
"rules": [
{ "action": "map", "pattern": "tenant_*.*", "collection": "{table}" },
{ "action": "map", "pattern": "tenant_sandbox.*", "collection": "{schema}/{table}" }
]
}
}

The first rule maps both tenant_a.orders and tenant_b.orders to orders, and both customers' invoices tables to invoices. The sandbox tables match both rules, so the second rule sets their destinations to tenant_sandbox/orders and tenant_sandbox/invoices. Tables outside the tenant_ schemas retain their usual collection names.

Collection names are templates relative to the capture's catalog prefix. For a capture named acme/warehouse/ingest, the destination orders refers to acme/warehouse/orders. A template can contain ASCII letters, digits, hyphens, underscores, and periods, with / between path components. The connector substitutes {schema} and {table} using the same casing and sanitization it uses for default collection names.

Discovery uses the recommended destination when adding a binding. To move a table that already has a binding, edit the binding's target.

Shared collection schema and key​

Tables mapped to the same collection must have the same structure, including their primary key. Discovery uses one table to determine the collection schema and key. It does not check whether the other tables match.

Primary key values are not necessarily unique when multiple tables are mapped into the same output collection. In that case, discovery prepends /_meta/source/schema and /_meta/source/table to the recommended key. For example, if the source tables have id as their primary key, the recommended key is ["/_meta/source/schema", "/_meta/source/table", "/id"].

Discovery adds these fields only when two or more source tables actually map to the collection. If the rule only maps tenant_a.orders to orders, discovery recommends ["/id"]. When you add tenant_b.orders, discovery recommends the three-field key. Flow handles this change as it would a change to a discovered primary key, creating a new version of the collection if automatic evolution is enabled.

When a source table has no suitable primary key, discovery recommends a fallback key and you may choose your own collection key. If a second table later maps to that collection, discovery adds the source schema and table to its recommendation, but the recommendation is still a fallback. Flow keeps the existing collection key if its fields still resolve in the discovered schema.

Before adding a table to a collection that uses a fallback or custom key, check whether rows from different tables can have the same key. If they can, include the source schema and table in your collection key.