Skip to content

Commit

Permalink
fix clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Oct 19, 2024
1 parent 3f5c377 commit 09d16ef
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions extension/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::search::{self, init_table};
use crate::transformers::generic::env_interpolate_string;
use crate::transformers::transform;
use crate::types;
use crate::util::pg_oid_to_table_name;

use anyhow::Result;
use pgrx::pg_sys::PgOid;
use pgrx::prelude::*;
use vectorize_core::types::Model;

Expand All @@ -28,7 +28,7 @@ fn table(
schedule: default!(&str, "'* * * * *'"),
) -> Result<String> {
let model = Model::new(transformer)?;
let table_name_str = table_name.to_regclass()?.to_string();
let table_name_str = pg_oid_to_table_name(table_name);
init_table(
job_name,
&table_name_str,
Expand Down
5 changes: 1 addition & 4 deletions extension/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn append_embedding_column(job_name: &str, schema: &str, table: &str, col_type:
)
}

pub fn get_column_datatype(schema: &str, table: &str, column: &str) -> Result<String> {
pub fn get_column_datatype(table: &str, column: &str) -> Result<String> {
Spi::get_one_with_args(
"
SELECT data_type
Expand All @@ -250,23 +250,20 @@ pub fn get_column_datatype(schema: &str, table: &str, column: &str) -> Result<St
AND column_name = $3
",
vec![
(PgBuiltInOids::TEXTOID.oid(), schema.into_datum()),
(PgBuiltInOids::TEXTOID.oid(), table.into_datum()),
(PgBuiltInOids::TEXTOID.oid(), column.into_datum()),
],
)
.map_err(|_| {
anyhow!(
"One of schema:`{}`, table:`{}`, column:`{}` does not exist.",
schema,
table,
column
)
})?
.ok_or_else(|| {
anyhow!(
"An unknown error occurred while fetching the data type for column `{}` in `{}.{}`.",
schema,
table,
column
)
Expand Down
9 changes: 5 additions & 4 deletions extension/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::init;
use crate::job::{create_event_trigger, create_trigger_handler, initalize_table_job};
use crate::transformers::openai;
use crate::transformers::transform;
use crate::util;
use crate::util::*;

use anyhow::{Context, Result};
use pgrx::prelude::*;
Expand All @@ -15,8 +15,7 @@ use vectorize_core::types::{self, Model, ModelSource, TableMethod, VectorizeMeta
#[allow(clippy::too_many_arguments)]
pub fn init_table(
job_name: &str,
schema: &str,
table: &str,
table_name: PgOid,
columns: Vec<String>,
primary_key: &str,
update_col: Option<String>,
Expand All @@ -28,14 +27,16 @@ pub fn init_table(
// cron-like for a cron based update model, or 'realtime' for a trigger-based
schedule: &str,
) -> Result<String> {
let table_name_str = pg_oid_to_table_name(table_name);

// validate table method
// realtime is only compatible with the join method
if schedule == "realtime" && table_method != TableMethod::join {
error!("realtime schedule is only compatible with the join table method");
}

// get prim key type
let pkey_type = init::get_column_datatype(schema, table, primary_key)?;
let pkey_type = init::get_column_datatype(&table_name_str, primary_key)?;
init::init_pgmq()?;

let guc_configs = get_guc_configs(&transformer.source);
Expand Down
9 changes: 9 additions & 0 deletions extension/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use anyhow::Result;
use pgrx::pg_sys::{regclassout, Oid};
use pgrx::spi::SpiTupleTable;
use pgrx::*;
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
use sqlx::{Pool, Postgres};
use std::env;
use std::ffi::CStr;
use url::{ParseError, Url};

use crate::guc;
Expand Down Expand Up @@ -204,6 +206,13 @@ pub fn get_pg_options(cfg: Config) -> Result<PgConnectOptions> {
}
}

pub fn pg_oid_to_table_name(oid: PgOid) -> String {
unsafe {
let regclass_cstring = regclassout(oid.value() as Oid);
CStr::from_ptr(regclass_cstring).to_string_lossy().into_owned()
}
}

pub async fn ready(conn: &Pool<Postgres>) -> bool {
sqlx::query_scalar(
"SELECT EXISTS (
Expand Down

0 comments on commit 09d16ef

Please sign in to comment.