Skip to content

Commit

Permalink
test: add search path test
Browse files Browse the repository at this point in the history
comment: add test comment
  • Loading branch information
kysshsy committed Oct 4, 2024
1 parent d6e6d9a commit df5d685
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
26 changes: 23 additions & 3 deletions tests/fixtures/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ fn array_data() -> ArrayData {
}

fn single_array_data() -> ArrayData {
let values: [u8; 5] = *b"hello";
let offsets: [i32; 2] = [0, 5];
ArrayData::builder(DataType::Binary)
.len(1)
.add_buffer(Buffer::from("hello"))
.add_buffer(Buffer::from_slice_ref(&offsets[..]))
.add_buffer(Buffer::from_slice_ref(&values[..]))
.build()
.unwrap()
}
Expand Down Expand Up @@ -75,9 +78,12 @@ fn binary_array_data() -> ArrayData {
}

fn single_binary_array_data() -> ArrayData {
ArrayData::builder(DataType::Binary)
let values: [u8; 5] = *b"hello";
let offsets: [i64; 2] = [0, 5];
ArrayData::builder(DataType::LargeBinary)
.len(1)
.add_buffer(Buffer::from("hello"))
.add_buffer(Buffer::from_slice_ref(&offsets[..]))
.add_buffer(Buffer::from_slice_ref(&values[..]))
.build()
.unwrap()
}
Expand Down Expand Up @@ -457,6 +463,20 @@ pub fn setup_local_file_listing_with_casing(local_file_path: &str, table: &str)
)
}

pub fn setup_parquet_wrapper_and_server() -> String {
let create_foreign_data_wrapper = primitive_create_foreign_data_wrapper(
"parquet_wrapper",
"parquet_fdw_handler",
"parquet_fdw_validator",
);
let create_server = primitive_create_server("parquet_server", "parquet_wrapper");
format!(
"{create_foreign_data_wrapper};
{create_server};
"
)
}

fn valid(data_type: &DataType, oid: u32) -> bool {
let oid = match PgBuiltInOids::from_u32(oid) {
Ok(oid) => oid,
Expand Down
13 changes: 11 additions & 2 deletions tests/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::fixtures::arrow::{
primitive_create_table, primitive_create_user_mapping_options, primitive_record_batch,
primitive_record_batch_single, primitive_setup_fdw_local_file_delta,
primitive_setup_fdw_local_file_listing, primitive_setup_fdw_s3_delta,
primitive_setup_fdw_s3_listing,
primitive_setup_fdw_s3_listing, setup_parquet_wrapper_and_server,
};
use crate::fixtures::db::Query;
use crate::fixtures::{conn, duckdb_conn, s3, tempdir, S3};
Expand Down Expand Up @@ -593,6 +593,11 @@ async fn test_prepare_stmt_execute(#[future(awt)] s3: S3, mut conn: PgConnection
Ok(())
}

// Note: PostgreSQL will replan the query when certain catalog changes occur,
// such as changes to the search path or when a table is deleted.
// In contrast, DuckDB does not replan when the search path is changed.
// If there are two foreign tables in different schemas and the prepared statements do not specify the schemas,
// it may lead to ambiguity or errors when referencing the tables.
#[rstest]
async fn test_prepare_search_path(mut conn: PgConnection, tempdir: TempDir) -> Result<()> {
let stored_batch = primitive_record_batch()?;
Expand All @@ -612,9 +617,13 @@ async fn test_prepare_search_path(mut conn: PgConnection, tempdir: TempDir) -> R
writer.write(&stored_batch_less)?;
writer.close()?;

// In this example, we create two tables with identical structures and names, but in different schemas.
// We expect that when the search path is changed, the correct table (the one in the current schema) will be referenced in DuckDB.
"CREATE SCHEMA tpch1".execute(&mut conn);
"CREATE SCHEMA tpch2".execute(&mut conn);

setup_parquet_wrapper_and_server().execute(&mut conn);

let file_path = parquet_path.as_path().to_str().unwrap();
let file_less_path = less_parquet_path.as_path().to_str().unwrap();

Expand All @@ -626,7 +635,7 @@ async fn test_prepare_search_path(mut conn: PgConnection, tempdir: TempDir) -> R

"SET search_path TO tpch1".execute(&mut conn);

"PREPARE q1 AS SELECT * FROM t1 where boolean_col = $1".execute(&mut conn);
"PREPARE q1 AS SELECT * FROM t1 WHERE boolean_col = $1".execute(&mut conn);

let result: Vec<(bool,)> = "EXECUTE q1(true)".fetch_collect(&mut conn);
assert_eq!(result.len(), 2);
Expand Down

0 comments on commit df5d685

Please sign in to comment.