Skip to content

Introduce Quantifier.AtMostOne as the base class of Quantifier.Existential - #4657

Merged
robert-brunel merged 2 commits into
mainfrom
apple/robert-brunel/scalar-subqueries-0
Oct 28, 2026
Merged

robert-brunel merged 2 commits into
mainfrom
apple/robert-brunel/scalar-subqueries-0

Conversation

@robert-brunel

@robert-brunel robert-brunel commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
  • Introduce Quantifier.AtMostOne, which serves as an abstract sealed base class of quantifiers that flow exactly one item of the sub-graph (or NULL if it is empty). For now, Quantifier.Existential is its only subclass.
  • Move the behavior that is shared by such quantifiers (the nullable flowed type, the flowed columns, and the pull-up of a max match map) from Quantifier.Existential into Quantifier.AtMostOne.
  • Make CardinalitiesProperty, DistinctRecordsProperty, OrderingProperty and StoredRecordProperty check for Quantifier.AtMostOne instead of Existential. These properties hold for every quantifier that flows exactly one item.
  • Add tests for Quantifier.Existential and Quantifier.AtMostOne, and for the cardinality of a SELECT with an existential quantifier.

In a subsequent PR, a second subclass will be added in order to introduce support for scalar subqueries in SQL.

@robert-brunel robert-brunel self-assigned this Sep 23, 2026
@robert-brunel robert-brunel added the enhancement New feature or request label Sep 23, 2026
@robert-brunel
robert-brunel removed this pull request from stack #4658 September 29, 2026 19:50
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch 2 times, most recently from d34c722 to 2f60654 Compare October 5, 2026 15:28
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from 2f60654 to 1ed01e6 Compare October 9, 2026 16:12
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch 3 times, most recently from 0f3b2f7 to 0f378d0 Compare October 12, 2026 09:51
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from 0f378d0 to 03511fe Compare October 13, 2026 12:30
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from 03511fe to f40fafb Compare October 19, 2026 13:43
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from f40fafb to be0f4fb Compare October 21, 2026 14:01
Base automatically changed from apple/robert-brunel/issue3431 to main October 21, 2026 15:15
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch 2 times, most recently from 12d0db5 to 57514ff Compare October 26, 2026 16:16

@normen662 normen662 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, I know this a bigger change in terms of semantic effect this can have.

  1. Terminology is unclear. What is a scalar quantifier called that is not existential?
  2. Why is the Scalar class abstract. I thought we wanted to use that class as true scalar quantifiers. Maybe that's part of a different PR but it is confusing to me. Even if the Scalar class is made to be not abstract it feels weird to me. I guess what I am suggesting is the classical inverted Y-shape instead: Have an abstract Quantifier.AtMostOne (name TBD), subclassed by non-abstract Quantifier.Scalar and Quantifier.Existential. I think that would give you better reasoning capabilities and less clunkiness throughout the planner. It also aligns better to the casual reader of this code since everyone knows what scalar subqueries are and existential subqueries are NOT scalar subqueries.
  3. Current instanceof checks: I tried to be as precise as I could in my individual comments. My advice: Keep the matching code stick to Existential, be careful with the rest. I am not confident I caught all problems. :-(. I think I will need to go through all the changes that you did swivel over to Scalar to make sure they work. I am also worried about testing that stuff. I would actually suggest to keep most of the changes to the old Existential unless we know exactly what happens AND that we have a test for it. Then, when the other scalar subquery stuff hits, you can broaden the field.
  4. Negative space. I don't think we did this in the past and if we did this, then only maybe in a very few places: I am worried that there are places where we say do something to every quantifier that is not an existential. Now it's in the stars if that can then also be widened to all scalars. I think you should use your AI of choice to figure out if that's done anywhere and react if needed.

@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from 57514ff to 9dad1e5 Compare October 28, 2026 15:58
@robert-brunel robert-brunel changed the title Introduce scalar quantifiers as a generalization of Quantifier.Existential Introduce Quantifier.AtMostOne as the base class of Quantifier.Existential Oct 28, 2026
…stential`

* Introduce `Quantifier.AtMostOne`, which serves as an abstract sealed base class of quantifiers that flow exactly one item of the sub-graph (or `NULL` if it is empty). For now, `Quantifier.Existential` is its only subclass.
* Move the behavior that is shared by such quantifiers (the nullable flowed type, the flowed columns, and the pull-up of a max match map) from `Quantifier.Existential` into `Quantifier.AtMostOne`.
* Make `CardinalitiesProperty`, `DistinctRecordsProperty`, `OrderingProperty` and `StoredRecordProperty` check for `Quantifier.AtMostOne` instead of `Existential`. These properties hold for every quantifier that flows exactly one item.
* Add tests for `Quantifier.Existential` and `Quantifier.AtMostOne`, and for the cardinality of a `SELECT` with an existential quantifier.

In a subsequent PR, a second subclass will be added in order to introduce support for scalar subqueries in SQL.
@robert-brunel
robert-brunel force-pushed the apple/robert-brunel/scalar-subqueries-0 branch from 9dad1e5 to d207de8 Compare October 28, 2026 17:53
* In `DistinctRecordsProperty`, remove the check for an `AtMostOne` quantifier from `DistinctRecordsVisitor.distinctRecordsFromChildren()`, which is only called for comparator and selector plans, and their quantifiers are always physical.
@robert-brunel
robert-brunel merged commit 651dc2e into main Oct 28, 2026
14 checks passed
@robert-brunel
robert-brunel deleted the apple/robert-brunel/scalar-subqueries-0 branch October 28, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants