From 4cc5a21498185180d2a360519d4eea9362238dcc Mon Sep 17 00:00:00 2001 From: Eric Ridge Date: Fri, 13 Sep 2024 12:20:35 -0400 Subject: [PATCH] fix: segfault with fields of type '::json' (#1654) --- pg_search/src/postgres/types.rs | 2 +- pg_search/tests/json.rs | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 pg_search/tests/json.rs diff --git a/pg_search/src/postgres/types.rs b/pg_search/src/postgres/types.rs index fe1639fb8a..401ea2d88a 100644 --- a/pg_search/src/postgres/types.rs +++ b/pg_search/src/postgres/types.rs @@ -165,7 +165,7 @@ impl TantivyValue { Ok(Self::json_value_to_tantivy_value(json_value)) } PgBuiltInOids::JSONOID => { - let pgrx_value = pgrx::JsonB::from_datum(datum, false) + let pgrx_value = pgrx::Json::from_datum(datum, false) .ok_or(TantivyValueError::DatumDeref)?; let json_value: Value = serde_json::from_slice(&serde_json::to_vec(&pgrx_value.0)?)?; diff --git a/pg_search/tests/json.rs b/pg_search/tests/json.rs new file mode 100644 index 0000000000..904a181318 --- /dev/null +++ b/pg_search/tests/json.rs @@ -0,0 +1,51 @@ +// Copyright (c) 2023-2024 Retake, Inc. +// +// This file is part of ParadeDB - Postgres for Search and Analytics +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +mod fixtures; + +use fixtures::*; +use rstest::*; +use sqlx::PgConnection; + +// In addition to checking whether all the expected types work for keys, make sure to include tests for anything that +// is reliant on keys (e.g. stable_sort, alias) + +#[rstest] +fn json_datatype(mut conn: PgConnection) { + r#" + CREATE TABLE test_table ( + id serial8, + value json + ); + + INSERT INTO test_table (value) VALUES ('{"currency_code": "USD", "salary": 120000 }'); + INSERT INTO test_table (value) VALUES ('{"currency_code": "USD", "salary": 75000 }'); + INSERT INTO test_table (value) VALUES ('{"currency_code": "USD", "salary": 140000 }'); + "# + .execute(&mut conn); + + // if we don't segfault postgres here, we're good + r#" + CALL paradedb.create_bm25( + table_name => 'test_table', + index_name => 'test_index', + key_field => 'id', + json_fields => paradedb.field('value', indexed => true, fast => true) + ); + "# + .execute(&mut conn); +}