Skip to content

Add Kleene nullability narrowing - #312

Open
jongleb wants to merge 1 commit into
ygrek:masterfrom
jongleb:klean
Open

Add Kleene nullability narrowing#312
jongleb wants to merge 1 commit into
ygrek:masterfrom
jongleb:klean

Conversation

@jongleb

@jongleb jongleb commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR adds Kleene-logic nullability narrowing

CREATE TABLE t1 (pk INT NOT NULL, a INT NULL, b INT NULL);
CREATE TABLE t2 (d INT NULL);

A strict predicate is never true when its operand is NULL: NULL > 5 is not true, it is NULL. WHERE and ON keep only true rows. So the columns used in such a predicate are not null in the result, even if they are nullable in the DDL

SQL before after
WHERE a > 5 a: Int? a: Int
WHERE a + 1 = 5 a: Int? a: Int
WHERE a IN (1, 2) a: Int? a: Int
WHERE a = 1 AND b = 2 a, b: Int? a, b: Int
JOIN t2 ON a = d a, d: Int? a, d: Int

No narrowing when NULL can still pass:

SQL stays why
WHERE a = 1 OR b = 2 a, b: Int? other branch can be true
WHERE a <=> 5 a: Int? null-safe comparison
LEFT JOIN t2 ON a = d a, d: Int? padded with NULL

Also fixed on the way: a window function is nullable when its frame can miss a row.

SQL before after
SELECT LAG(pk) OVER (ORDER BY pk) Int Int?

@jongleb
jongleb force-pushed the klean branch 8 times, most recently from e6c8a02 to 6ede843 Compare August 28, 2026 12:52
@jongleb
jongleb marked this pull request as ready for review August 28, 2026 13:21
@jongleb jongleb changed the title Add Kleene Algebra Add Kleene nullability narrowing Aug 28, 2026
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.

1 participant