Skip to content

Commit

Permalink
chore: Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Weijun-H committed Oct 16, 2024
1 parent eeecfd1 commit 8889b26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pgrx = "0.12.6"
serde = "1.0.210"
serde_json = "1.0.128"
signal-hook = "0.3.17"
sqlparser = { version = "0.51.0", features = ["visitor"] }
sqlparser = "0.50.0"
strum = { version = "0.26.3", features = ["derive"] }
supabase-wrappers = { git = "https://github.com/paradedb/wrappers.git", default-features = false, rev = "f5ecb8d" }
thiserror = "1.0.63"
Expand Down
26 changes: 10 additions & 16 deletions src/hooks/utility/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use anyhow::Result;
use pg_sys::{get_relname_relid, RangeVarGetCreationNamespace};
use std::ffi::CStr;

use pgrx::*;

Expand All @@ -26,21 +25,20 @@ use crate::{duckdb::connection::execute, hooks::query::is_duckdb_query};
use super::set_search_path_by_pg;

pub fn view_query(query_string: &core::ffi::CStr, stmt: *mut pg_sys::ViewStmt) -> Result<bool> {
if analyze_query(stmt)? {
let query = unsafe { (*stmt).query as *mut pg_sys::SelectStmt };
let from_clause = unsafe { (*query).fromClause };

if analyze_from_clause(from_clause)? {
// Push down the view creation query to DuckDB
set_search_path_by_pg()?;
execute(query_string.to_str()?, [])?;
} else {
fallback_warning!("relation is not a foreign table from DuckDB");
}
Ok(true)
}

fn analyze_query(stmt: *mut pg_sys::ViewStmt) -> Result<bool> {
let query = unsafe { (*stmt).query as *mut pg_sys::SelectStmt };
let from_clause = unsafe { (*query).fromClause };

analyze_from_clause(from_clause)
}

/// Analyze the from clause to find the RangeVar node to check if it's a DuckDB query
fn analyze_from_clause(from_clause: *mut pg_sys::List) -> Result<bool> {
unsafe {
let elements = (*from_clause).elements;
Expand All @@ -61,26 +59,21 @@ fn analyze_from_clause(from_clause: *mut pg_sys::List) -> Result<bool> {
Ok(false)
}

/// Check if the RangeVar is a DuckDB query
fn analyze_range_var(rv: *mut pg_sys::RangeVar) -> Result<bool> {
let pg_relation = unsafe {
let schema_id = RangeVarGetCreationNamespace(rv);
let relid = get_relname_relid((*rv).relname, schema_id);

pgrx::warning!(
"Relation table name: {:?}",
CStr::from_ptr((*rv).relname).to_str()
);

let relation = pg_sys::RelationIdGetRelation(relid);
PgRelation::from_pg_owned(relation)
};

Ok(is_duckdb_query(&[pg_relation]))
}

/// Check if the JoinExpr is a DuckDB query
fn analyze_join_expr(join_expr: *mut pg_sys::JoinExpr) -> Result<bool> {
pgrx::warning!("Analyzing JoinExpr");

unsafe {
let ltree = (*join_expr).larg;
let rtree = (*join_expr).rarg;
Expand All @@ -89,6 +82,7 @@ fn analyze_join_expr(join_expr: *mut pg_sys::JoinExpr) -> Result<bool> {
}
}

/// Analyze the tree recursively to find the RangeVar node to check if it's a DuckDB query
fn analyze_tree(mut tree: *mut pg_sys::Node) -> Result<bool> {
while !tree.is_null() {
unsafe {
Expand Down

0 comments on commit 8889b26

Please sign in to comment.