Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NULLS NOT DISTINCT clause #532

Merged
merged 3 commits into from
Dec 2, 2022
Merged

Add support for NULLS NOT DISTINCT clause #532

merged 3 commits into from
Dec 2, 2022

Conversation

rstular
Copy link
Contributor

@rstular rstular commented Nov 27, 2022

PostgreSQL 15 added support (section E.2.3.1.2. Indexes) for the NULLS NOT DISTINCT clause in index creation, which, as the name suggests, causes nulls to not be treated as distinct values when evaluating the UNIQUE constraint.

However, using this feature on an unsupported Postgres version will cause an SQL error - I'm not sure how/if this is handled in this project.

Also closes #509

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Hey @rstular, thanks for contributing!!

I think we miss a edge case (this should be added as test case):

assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(ColumnDef::new(Glyph::Image).json())
        .col(ColumnDef::new(Glyph::Aspect).json_binary())
        .index(
            Index::create()
                .unique()
                .nulls_not_distinct()
                .name("idx-glyph-aspect-image")
                .table(Glyph::Table)
                .col(Glyph::Aspect)
                .col(Glyph::Image)
        )
        .to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "glyph" ("#,
        r#""image" json,"#,
        r#""aspect" jsonb,"#,
        r#"CONSTRAINT "idx-glyph-aspect-image" UNIQUE NULLS NOT DISTINCT ("aspect", "image")"#,
        r#")"#,
    ]
    .join(" ")
);

Hence, we need to modify this part as well:

fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut dyn SqlWriter) {
if create.primary {
write!(sql, "PRIMARY KEY ").unwrap();
}
if create.unique {
write!(sql, "UNIQUE ").unwrap();
}
}

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Awesome!! Thanks! @rstular

@rstular
Copy link
Contributor Author

rstular commented Dec 2, 2022

@billy1624 Glad to help with the project :)

I've resolved the merge conflicts now, so this should (hopefully) be ready to merge.

@ikrivosheev ikrivosheev merged commit 4b22aee into SeaQL:master Dec 2, 2022
@billy1624
Copy link
Member

See you around on GitHub @rstular :)
Thanks again!

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.

Support NULLS NOT DISTINCT
3 participants