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

ArrayOps should be default for GIN index arrays #3517

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ async fn array_ops(api: &TestApi) -> TestResult {
Ok(())
}

#[test_connector(tags(Postgres), exclude(CockroachDb))]
async fn array_ops_with_native_type(api: &TestApi) -> TestResult {
let schema_name = api.schema_name();
let create_table = format!("CREATE TABLE \"{schema_name}\".\"A\" (id SERIAL PRIMARY KEY, data int[] not null)",);
let create_idx = format!("CREATE INDEX \"A_data_idx\" ON \"{schema_name}\".\"A\" USING GIN (data);",);

api.database().raw_cmd(&create_table).await?;
api.database().raw_cmd(&create_idx).await?;

let expected = expect![[r#"
model A {
id Int @id @default(autoincrement())
data Int[]

@@index([data], type: Gin)
}
"#]];

let result = api.introspect_dml().await?;
expected.assert_eq(&result);

Ok(())
}

#[test_connector(tags(Postgres), exclude(CockroachDb))]
async fn jsonb_ops(api: &TestApi) -> TestResult {
let schema_name = api.schema_name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ fn gin_array_ops(api: TestApi) {
api.schema_push_w_datasource(dm).send().assert_no_steps();
}

#[test_connector(tags(Postgres), exclude(CockroachDb, Postgres9))]
fn gin_array_default_ops(api: TestApi) {
let dm = r#"
model A {
id Int @id @default(autoincrement())
data String[] @db.Uuid

@@index([data], type: Gin)
}
"#;

api.schema_push_w_datasource(dm).send().assert_green();

api.assert_schema().assert_table("A", |table| {
table
.assert_has_column("data")
.assert_index_on_columns(&["data"], |idx| {
idx.assert_algorithm(SqlIndexAlgorithm::Gin)
.assert_column("data", |attrs| attrs.assert_ops(SQLOperatorClassKind::ArrayOps))
})
});

api.schema_push_w_datasource(dm).send().assert_no_steps();
}

#[test_connector(tags(Postgres), exclude(CockroachDb, Postgres9))]
fn gin_array_ops_default(api: TestApi) {
let dm = r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ pub(super) fn generalized_index_validations(
match (&native_type, opclass) {
// Jsonb / JsonbOps + JsonbPathOps
(None, None) if r#type.is_json() => (),

// Array fields + ArrayOps
(_, None) if field.as_index_field().is_list() => (),

(Some(PostgresType::JsonB), Some(JsonbOps | JsonbPathOps) | None) => (),

(None, Some(JsonbOps | JsonbPathOps)) => {
Expand Down
8 changes: 8 additions & 0 deletions psl/parser-database/src/walkers/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ impl<'db> IndexFieldWalker<'db> {
}
}

/// Is the field a list?
pub fn is_list(self) -> bool {
match self.inner {
InnerIndexFieldWalker::Scalar(sf) => sf.is_list(),
InnerIndexFieldWalker::Composite(cf) => cf.arity().is_list(),
}
}

/// Is the type of the field `Unsupported("...")`?
pub fn is_unsupported(self) -> bool {
match self.inner {
Expand Down
5 changes: 5 additions & 0 deletions psl/parser-database/src/walkers/scalar_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ impl<'db> ScalarFieldWalker<'db> {
self.ast_field().arity.is_optional()
}

/// Is the field a list
pub fn is_list(self) -> bool {
self.ast_field().arity.is_list()
}

/// Is there an `@updatedAt` attribute on the field?
pub fn is_updated_at(self) -> bool {
self.attributes().is_updated_at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
datasource test {
provider = "postgresql"
url = env("TEST_DATABASE_URL")
}

model A {
id Int @id
a String[] @test.Uuid

@@index([a], type: Gin)
}