Skip to content

feat(core-spec): make relationship cardinality explicit - #794

Open
christianeu-db wants to merge 6 commits into
apache:mainfrom
christianeu-db:relationship-cardinality
Open

christianeu-db wants to merge 6 commits into
apache:mainfrom
christianeu-db:relationship-cardinality

Conversation

@christianeu-db

@christianeu-db christianeu-db commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A relationship may declare cardinality: ManyToOne, OneToMany, or OneToOne, stated from from to to. An absent value means ManyToOne, so existing documents keep their meaning. There is no many-to-many value.

Layer 2 semantics are unchanged; its mapping text now maps cardinality. Layer 3 paths follow every step that does not increase granularity: from from to to for ManyToOne and OneToOne, and from to to from for OneToMany and OneToOne. Filter propagation stays out of scope.

The ost lowering of #803 follows the same steps. The retail fixture now declares customers_to_visits from customers as OneToMany, so wide table case a03 and the new a22 cross it from visits to customers, and the new e26 checks that customers does not reach visits.

The validator checks key coverage on each one side of the cardinality. The schema, spec.yaml, the spec type generator, and the Python SDK gain the Cardinality enum.

Discussion: https://lists.apache.org/thread/1kggjhy5tjqjyvlf6rsvs9ygxt6x9zkr

Related Issues

#50, apache/ossie-converters#293 to update converters

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have a [DISCUSS] thread on dev@ with a stated end date, and reach lazy consensus (or pass a [VOTE]) before merge
  • Breaking changes to the spec are clearly called out in the summary - n/a
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed

Validation

  • Validation rules in validation/ are updated if the spec changed
  • New validation cases are covered by tests

Documentation

  • docs/ is updated to reflect any user-facing changes
  • New features or behaviors are documented with examples where appropriate

Tests

  • All existing tests pass (pytest / CI green)
  • New functionality is covered by tests

Compliance

  • ASF license headers are present on all new source files
  • New third-party dependencies follow Third-Party Dependencies
  • If third-party source code is vendored/copied (not just declared as a dependency), NOTICE and/or LICENSE have been updated per ASF policy

A relationship may declare `cardinality`: ManyToOne, OneToMany, or
OneToOne, stated from `from` to `to`. An absent value means ManyToOne, so
existing documents keep their meaning. There is no many-to-many value.

Layer 2 semantics are unchanged; its mapping text now maps `cardinality`.
Layer 3 paths follow every step that does not increase granularity: from
`from` to `to` for ManyToOne and OneToOne, and from `to` to `from` for
OneToMany and OneToOne. Filter propagation stays out of scope.

The validator checks key coverage on each one side of the cardinality.
The schema, spec.yaml, the spec type generator, and the Python SDK gain
the Cardinality enum.

Discussion: https://lists.apache.org/thread/1kggjhy5tjqjyvlf6rsvs9ygxt6x9zkr

Co-authored-by: Isaac <no-reply@databricks.com>

@kayemkim kayemkim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The glossary in relational_semantics.md still has the old direction: "Relationship (Ossie). … from is the many side, to the one" (line 1242). The mapping table and Section 11.3 in the same file are updated, and this sentence is not.

The **relationship graph** contains one node per dataset and one edge per relationship. Each relationship equates its `from_columns` with its `to_columns`. Its `cardinality` is its **declared cardinality** (core specification, Relationships); the data need not satisfy it (Layer 2 Section 11.3).

- A **step** crosses a relationship from `from` to `to`. If the data satisfies the declared cardinality, a step matches at most one `to` row for each `from` row.
- A **step** crosses a relationship from `from` to `to` if its declared cardinality is `ManyToOne` or `OneToOne`, and from `to` to `from` if it is `OneToMany` or `OneToOne`. If the data satisfies the declared cardinality, a step matches at most one row where it ends for each row where it starts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#803's lowering walks a relationship from from to to only and doesn't read cardinality (edges and paths() in ost/src/ossie_ost/lowering.py). With both branches merged, a relationship declared from the other dataset as OneToMany goes the wrong way:

from: customers, to: orders, cardinality: OneToMany
orders.total_revenue by customers.region       E_NO_PATH
customers.customer_count by orders.order_id    lowers, one row per order

Declared from orders with no cardinality, the first gives EAST 150 and WEST 30 and the second is E_NO_PATH. Whichever of the two lands second needs the other's rule, and a wide table case with a OneToMany relationship would hold it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. A fixture here would be helpful. I think @ekut is planning an update to #803 soon, and I'd prefer to land that first since it feels more foundational to the initial Ossie payload. I'm happy to update this PR once that's merged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done now that #803 has landed (d0fb147). The lowering reads cardinality: a step crosses a relationship from from to to for ManyToOne and OneToOne, and from to to from for OneToMany and OneToOne. Instead of adding a model, retail_model now declares customers_to_visits from customers as OneToMany. Your two examples are the new a22 (visit count by customer region: NORTH 2, WEST 1) and e26 (customer count per visit: E_NO_PATH). Both fail with the #803 lowering, and a03 lowers as before.

Comment thread validation/validate.py Outdated
"from": (from_ds, from_dataset, from_columns, missing_from),
"to": (to_ds, to_dataset, to_columns, missing_to),
}
for side in _ONE_SIDES.get(rel.get("cardinality", "ManyToOne"), ()):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Called on a document the schema has not vetted, a cardinality that is a list or a mapping raises TypeError: unhashable type here, where main returns its findings. The CLI is not affected, since it stops at the schema error. An isinstance(cardinality, str) check before the lookup keeps this to the shape guards the docstring above describes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in b52dc54: a non-string cardinality now skips key coverage the same way an unknown value does, and the unknown-cardinality test now covers a list and a mapping.

Also fixed the glossary entry in relational_semantics.md in b52dc54. It now names the optional cardinality instead of saying that from is the many side.

validate_references skips key coverage when `cardinality` is not a
string, as it already does for an unknown value. A list or a mapping
raised TypeError when the checks ran on a document that failed the
schema.

The relational_semantics.md glossary no longer says that `from` is the
many side. It names the optional `cardinality` instead.

Co-authored-by: Isaac <no-reply@databricks.com>
@djwaldo

djwaldo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

+1 on the placement, and on keeping filter flow out of scope.

The placement is the part that matters most to a converter. A join is a fact about
two datasets, not about a metric. If it is stated once on the relationship, a
converter reads it once. If it moves into the metrics, a converter has to collect it
from every metric, remove duplicates, and decide what to do when two of them
disagree. The document gives it nothing to decide with. Every target I write to holds
the join at the model level, so this shape maps straight across.

One thing I would like to raise before the 28th, because it touches the validation
this PR adds rather than the attribute itself.

The PR says the validator checks key coverage on each one side of the cardinality.
Gareth's three GASP relationships do not cover the key, and he said so when he posted
them. So they would fail that check. The point I want to make is that no value of
the new attribute makes them pass
. What they are missing is not cardinality. It is
the rest of the join condition.

That is worth separating now, so the attribute does not get blamed later for a case it
was never meant to carry.

On what the condition should look like, I have a measurement rather than an opinion.

I built his case on two platforms this week. Four fact rows against a six-row period
dimension, with the warehouse as the oracle. The dimension holds three qualifying rows
per key, which is the shape his does. Hours summed by period:

equality on the id only          Apr 32, May 32, Jun 24   total 88
the same plus a date range       May 8,  Jun 24           total 32

32 is the true figure. On Snowflake the range form is written on the relationship, as
relationship_columns[].type: range with a right_range. On ThoughtSpot it is written
in the join condition, and the generated SQL carries it through unchanged:

ON ( t.CONSULTANT_ID = c.CONSULTANT_ID
     AND t.ENTRY_DATE >= c.PERIOD_START
     AND t.ENTRY_DATE <  c.PERIOD_END )

Both platforms gave the same two numbers, and both matched the warehouse.

The part I would ask the list to note is which condition form travels. Snowflake also
offers an as-of join, which picks only the latest row at or before a date.
ThoughtSpot cannot express that at all.

I want to be careful here, because the operator is not the problem. A bare >= is a
sound join condition where the data holds one qualifying row per key. It returns the
right number, and plenty of models are built that way.

The problem is using >= to mean as-of. As-of selects the latest of several; >=
only filters. They agree only when one row qualifies, which is the case as-of does
not need to exist for. So the substitution moves the correctness burden from the query
to the data, and nothing in the document records which kind of data it got. We have
measured the bad half twice: 3,800 against a true 2,100 in a round-trip study in
September, and 88 against 32 this week.

So if a condition vocabulary is ever added beside the cardinality value, a half-open
range is the member that more consumers can honour. It states the bound in the
document, rather than relying on the data to hold only one match. An as-of is not
portable today. A spec that offers it without saying so would produce the silent kind
of wrong answer this proposal is trying to remove.

For Gareth's case specifically, this is also a practical answer. His consultant dataset
carries period_start and no end date, which is an as-of shape. Give it an end column,
or the next period's start. It then becomes a range that both platforms consume, and it
covers the key.

None of this needs to ride on this PR. I would rather it merged as it stands than grew.
I am raising it so the key-coverage rule lands knowing which cases it will catch and
which of those a cardinality value cannot fix.

Happy to contribute the probe as a compliance case if it is useful. It is ten rows of
data, two documents, and both figures.

christianeu-db and others added 3 commits November 25, 2026 06:45
…ality

# Conflicts:
#	python/src/ossie/__init__.py
#	python/src/ossie/models.py
#	python/tests/test_models.py
#	validation/validate.py
The cardinality tests build an OssieSemanticModel, which replaced
OssieDocument as the semantic model class in apache#651. A coverage warning
is wrapped to the line limit that ruff checks since apache#818.

Co-authored-by: Isaac <no-reply@databricks.com>
A step of the Layer 3 lowering now crosses a relationship from `from` to
`to` if its cardinality is ManyToOne, the default, or OneToOne, and from
`to` to `from` if it is OneToMany or OneToOne (Section 4). The join
condition starts from the dataset where the step starts.

retail_model declares customers_to_visits from customers, as OneToMany,
so a03 crosses it from visits to customers and lowers as before. a22
groups visits by customer region, and e26 checks that customers does not
reach visits. With the lowering of apache#803, a22 raises E_NO_PATH and e26
lowers to one row per visit.

Co-authored-by: Isaac <no-reply@databricks.com>
@christianeu-db

Copy link
Copy Markdown
Contributor Author

+1 on the placement, and on keeping filter flow out of scope.

The placement is the part that matters most to a converter. A join is a fact about two datasets, not about a metric. If it is stated once on the relationship, a converter reads it once. If it moves into the metrics, a converter has to collect it from every metric, remove duplicates, and decide what to do when two of them disagree. The document gives it nothing to decide with. Every target I write to holds the join at the model level, so this shape maps straight across.

One thing I would like to raise before the 28th, because it touches the validation this PR adds rather than the attribute itself.

The PR says the validator checks key coverage on each one side of the cardinality. Gareth's three GASP relationships do not cover the key, and he said so when he posted them. So they would fail that check. The point I want to make is that no value of the new attribute makes them pass. What they are missing is not cardinality. It is the rest of the join condition.

That is worth separating now, so the attribute does not get blamed later for a case it was never meant to carry.

On what the condition should look like, I have a measurement rather than an opinion.

I built his case on two platforms this week. Four fact rows against a six-row period dimension, with the warehouse as the oracle. The dimension holds three qualifying rows per key, which is the shape his does. Hours summed by period:

equality on the id only          Apr 32, May 32, Jun 24   total 88
the same plus a date range       May 8,  Jun 24           total 32

32 is the true figure. On Snowflake the range form is written on the relationship, as relationship_columns[].type: range with a right_range. On ThoughtSpot it is written in the join condition, and the generated SQL carries it through unchanged:

ON ( t.CONSULTANT_ID = c.CONSULTANT_ID
     AND t.ENTRY_DATE >= c.PERIOD_START
     AND t.ENTRY_DATE <  c.PERIOD_END )

Both platforms gave the same two numbers, and both matched the warehouse.

The part I would ask the list to note is which condition form travels. Snowflake also offers an as-of join, which picks only the latest row at or before a date. ThoughtSpot cannot express that at all.

I want to be careful here, because the operator is not the problem. A bare >= is a sound join condition where the data holds one qualifying row per key. It returns the right number, and plenty of models are built that way.

The problem is using >= to mean as-of. As-of selects the latest of several; >= only filters. They agree only when one row qualifies, which is the case as-of does not need to exist for. So the substitution moves the correctness burden from the query to the data, and nothing in the document records which kind of data it got. We have measured the bad half twice: 3,800 against a true 2,100 in a round-trip study in September, and 88 against 32 this week.

So if a condition vocabulary is ever added beside the cardinality value, a half-open range is the member that more consumers can honour. It states the bound in the document, rather than relying on the data to hold only one match. An as-of is not portable today. A spec that offers it without saying so would produce the silent kind of wrong answer this proposal is trying to remove.

For Gareth's case specifically, this is also a practical answer. His consultant dataset carries period_start and no end date, which is an as-of shape. Give it an end column, or the next period's start. It then becomes a range that both platforms consume, and it covers the key.

None of this needs to ride on this PR. I would rather it merged as it stands than grew. I am raising it so the key-coverage rule lands knowing which cases it will catch and which of those a cardinality value cannot fix.

Happy to contribute the probe as a compliance case if it is useful. It is ten rows of data, two documents, and both figures.

Thanks for the feedback @djwaldo. I think addressing the complex conditions in #4 would be a great extension

@djwaldo

djwaldo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks @christianeu-db — Discussion #4 is the right home, and I have written the
concrete proposal up there now. Agreed it does not belong on this PR.

One thing on this PR itself, though, before the 28th. Section 4 now contradicts
itself. The new bullet reads:

A step crosses a relationship from from to to if its declared
cardinality is ManyToOne or OneToOne, and from to to from if it is
OneToMany or OneToOne.

Two lines below it, unchanged by the diff:

Only many-to-one steps are supported. Layer 2's AGG() also handles joins in
the reverse direction (Layer 2 Section 8.3), but those paths are outside this
version's shared-dimension scope.

The hunk ends at the third bullet, so that paragraph was never revisited. On
d0fb147 the section both adds reverse steps and says they are not supported.

Also minor: the #50 under Related Issues 404s.

Section 4 still said that only many-to-one steps are supported, which
contradicted the new step rule for OneToMany and OneToOne. The paragraph
now excludes steps in other directions, and Section 1.2 refers to
many-to-one steps instead of many-to-one relationships.

Co-authored-by: Isaac <no-reply@databricks.com>
@christianeu-db

Copy link
Copy Markdown
Contributor Author

Thanks @christianeu-db — Discussion #4 is the right home, and I have written the concrete proposal up there now. Agreed it does not belong on this PR.

One thing on this PR itself, though, before the 28th. Section 4 now contradicts itself. The new bullet reads:

A step crosses a relationship from from to to if its declared
cardinality is ManyToOne or OneToOne, and from to to from if it is
OneToMany or OneToOne.

Two lines below it, unchanged by the diff:

Only many-to-one steps are supported. Layer 2's AGG() also handles joins in
the reverse direction (Layer 2 Section 8.3), but those paths are outside this
version's shared-dimension scope.

The hunk ends at the third bullet, so that paragraph was never revisited. On d0fb147 the section both adds reverse steps and says they are not supported.

Also minor: the #50 under Related Issues 404s.

Good call - I've updated the diff to incorporate that feedback

@kayemkim kayemkim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d0fb147 closes the lowering point. The model from my earlier comment, declared from customers as OneToMany, now gives EAST 150 and WEST 30 and raises E_NO_PATH the other way, the same as when it is declared from orders. I also ran it with different column names on the two sides and with a OneToOne relationship crossed from each end, and the join conditions and paths come out as Section 4 says.

The glossary sentence and the guard are in as well, so this looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants