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

chore: Migrate changes from monorepo #36

Merged
merged 1 commit into from
Aug 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ brew install make gcc pkg-config openssl

# Ubuntu
sudo apt-get install -y make gcc pkg-config libssl-dev

# Arch Linux
sudo pacman -S core/openssl
```

### Install Postgres
Expand All @@ -179,6 +182,9 @@ brew install postgresql@16
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt-get update && sudo apt-get install -y postgresql-16 postgresql-server-dev-16

# Arch Linux
sudo pacman -S extra/postgresql
```

If you are using Postgres.app to manage your macOS PostgreSQL, you'll need to add the `pg_config` binary to your path before continuing:
Expand All @@ -203,12 +209,25 @@ cargo pgrx init --pg16=/usr/local/opt/postgresql@16/bin/pg_config

# Ubuntu
cargo pgrx init --pg16=/usr/lib/postgresql/16/bin/pg_config

# Arch Linux
cargo pgrx init --pg16=/usr/bin/pg_config
```

If you prefer to use a different version of Postgres, update the `--pg` flag accordingly.

Note: While it is possible to develop using pgrx's own Postgres installation(s), via `cargo pgrx init` without specifying a `pg_config` path, we recommend using your system package manager's Postgres as we've observed inconsistent behaviours when using pgrx's.

`pgrx` requires `libclang`. To install it:

```bash
# Ubuntu
sudo apt install libclang-dev

# Arch Linux
sudo pacman -S extra/clang
```

### Running Tests

We use `cargo test` as our runner for `pg_analytics` tests. Tests are conducted using [testcontainers](https://github.com/testcontainers/testcontainers-rs) to manage testing containers like [LocalStack](https://hub.docker.com/r/localstack/localstack). `testcontainers` will pull any Docker images that it requires to perform the test.
Expand Down
8 changes: 5 additions & 3 deletions src/fdw/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ unsafe fn auto_create_schema_impl(fcinfo: pg_sys::FunctionCallInfo) -> Result<()
.get("preserve_casing")
.map_or(false, |s| s.eq_ignore_ascii_case("true"));
let alter_table_statement =
construct_alter_table_statement(table_name, schema_rows, preserve_casing);
construct_alter_table_statement(schema_name, table_name, schema_rows, preserve_casing);
Spi::run(alter_table_statement.as_str())?;

Ok(())
Expand Down Expand Up @@ -231,6 +231,7 @@ fn duckdb_type_to_pg(column_name: &str, duckdb_type: &str) -> Result<String> {

#[inline]
fn construct_alter_table_statement(
schema_name: &str,
table_name: &str,
columns: Vec<(String, String)>,
preserve_casing: bool,
Expand All @@ -252,8 +253,9 @@ fn construct_alter_table_statement(
.collect();

format!(
"ALTER TABLE {} {}",
table_name,
"ALTER TABLE {}.{} {}",
spi::quote_identifier(schema_name),
spi::quote_identifier(table_name),
column_definitions.join(", ")
)
}
1 change: 0 additions & 1 deletion tests/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ async fn test_arrow_types_s3_listing(#[future(awt)] s3: S3, mut conn: PgConnecti
}

#[rstest]
#[ignore = "bug in DuckDB delta_scan over custom endpoints"]
async fn test_arrow_types_s3_delta(
#[future(awt)] s3: S3,
mut conn: PgConnection,
Expand Down
70 changes: 70 additions & 0 deletions tests/table_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,73 @@ async fn test_preserve_casing(mut conn: PgConnection, tempdir: TempDir) -> Resul

Ok(())
}

#[rstest]
async fn test_preserve_casing_table_name(mut conn: PgConnection, tempdir: TempDir) -> Result<()> {
let stored_batch = primitive_record_batch()?;
let parquet_path = tempdir.path().join("test_arrow_types.parquet");
let parquet_file = File::create(&parquet_path)?;

let mut writer = ArrowWriter::try_new(parquet_file, stored_batch.schema(), None).unwrap();
writer.write(&stored_batch)?;
writer.close()?;

primitive_setup_fdw_local_file_listing(parquet_path.as_path().to_str().unwrap(), "MyTable")
.execute(&mut conn);

format!(
r#"CREATE FOREIGN TABLE "PrimitiveTable" () SERVER parquet_server OPTIONS (files '{}', preserve_casing 'true')"#,
parquet_path.to_str().unwrap()
).execute(&mut conn);

let count: (i64,) = r#"SELECT COUNT(*) FROM "PrimitiveTable" LIMIT 1"#.fetch_one(&mut conn);
assert_eq!(count.0, 3);

Ok(())
}

#[rstest]
async fn test_table_with_custom_schema(mut conn: PgConnection, tempdir: TempDir) -> Result<()> {
let stored_batch = primitive_record_batch()?;
let parquet_path = tempdir.path().join("test_arrow_types.parquet");
let parquet_file = File::create(&parquet_path)?;

let mut writer = ArrowWriter::try_new(parquet_file, stored_batch.schema(), None).unwrap();
writer.write(&stored_batch)?;
writer.close()?;

primitive_setup_fdw_local_file_listing(parquet_path.as_path().to_str().unwrap(), "MyTable")
.execute(&mut conn);

// test quoted schema name
"CREATE SCHEMA \"MY_SCHEMA\"".to_string().execute(&mut conn);
match
format!( "CREATE FOREIGN TABLE \"MY_SCHEMA\".\"MyTable\" () SERVER parquet_server OPTIONS (files '{}', preserve_casing 'true')",
parquet_path.to_str().unwrap()
).execute_result(&mut conn) {
Ok(_) => {}
Err(error) => {
panic!(
"should have successfully created table with custom schema \"MY_SCHEMA\".\"MyTable\": {}",
error
);
}
}

// test non-quoted schema name
"CREATE SCHEMA MY_SCHEMA".to_string().execute(&mut conn);
match
format!( "CREATE FOREIGN TABLE MY_SCHEMA.MyTable () SERVER parquet_server OPTIONS (files '{}', preserve_casing 'true')",
parquet_path.to_str().unwrap()
).execute_result(&mut conn) {
Ok(_) => {}
Err(error) => {
panic!(
"should have successfully created table with custom schema MY_SCHEMA.MyTable: {}",
error
);
}
}

Ok(())
}