Skip to content

Commit

Permalink
not using unwraps to avoid panic while acquiring locks
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhawvipul committed Aug 8, 2024
1 parent 6de836d commit 40691a5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/api/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use anyhow::Result;
use anyhow::{anyhow, Result};
use duckdb::types::Value;
use pgrx::*;

Expand Down Expand Up @@ -71,7 +71,9 @@ fn sniff_csv_impl(files: &str, sample_size: Option<i64>) -> Result<Vec<SniffCsvR
.collect::<Vec<String>>()
.join(", ");
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
let query = format!("SELECT * FROM sniff_csv({schema_str})");
let mut stmt = conn.prepare(&query)?;

Expand Down
6 changes: 4 additions & 2 deletions src/api/duckdb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use pgrx::*;

use crate::duckdb::connection;
Expand Down Expand Up @@ -38,7 +38,9 @@ pub fn duckdb_settings() -> iter::TableIterator<
#[inline]
fn duckdb_settings_impl() -> Result<Vec<DuckdbSettingsRow>> {
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
let mut stmt = conn.prepare("SELECT * FROM duckdb_settings()")?;

Ok(stmt
Expand Down
6 changes: 4 additions & 2 deletions src/api/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use anyhow::Result;
use anyhow::{anyhow, Result};
use pgrx::*;

use crate::duckdb::utils;
Expand Down Expand Up @@ -89,7 +89,9 @@ pub fn parquet_schema(
fn parquet_schema_impl(files: &str) -> Result<Vec<ParquetSchemaRow>> {
let schema_str = utils::format_csv(files);
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
let query = format!("SELECT * FROM parquet_schema({schema_str})");
let mut stmt = conn.prepare(&query)?;

Expand Down
8 changes: 6 additions & 2 deletions src/duckdb/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ pub fn get_batches() -> Result<Vec<RecordBatch>> {

pub fn execute<P: Params>(sql: &str, params: P) -> Result<usize> {
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
conn.execute(sql, params).map_err(|err| anyhow!("{err}"))
}

pub fn drop_relation(table_name: &str, schema_name: &str) -> Result<()> {
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
let mut statement = conn.prepare(format!("SELECT table_type from information_schema.tables WHERE table_schema = '{schema_name}' AND table_name = '{table_name}' LIMIT 1").as_str())?;
if let Ok(Some(row)) = statement.query([])?.next() {
let table_type: String = row.get(0)?;
Expand Down
6 changes: 4 additions & 2 deletions src/fdw/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use pgrx::*;
use std::collections::HashMap;
use std::ffi::CStr;
Expand Down Expand Up @@ -145,7 +145,9 @@ unsafe fn auto_create_schema_impl(fcinfo: pg_sys::FunctionCallInfo) -> Result<()

// Get DuckDB schema
let conn = get_global_connection()?;
let conn = conn.lock().unwrap();
let conn = conn
.lock()
.map_err(|e| anyhow!("Failed to acquire lock: {}", e))?;
let query = format!("DESCRIBE {schema_name}.{table_name}");
let mut stmt = conn.prepare(&query)?;

Expand Down

0 comments on commit 40691a5

Please sign in to comment.