Skip to content

Fully qualify table name #445

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

Merged
merged 1 commit into from
Oct 21, 2022
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
2 changes: 1 addition & 1 deletion pgml-dashboard/pgml_dashboard/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"OPTIONS": {"options": "-c search_path=pgml,public"},
"OPTIONS": {"options": "-c search_path=public,pgml"},
"NAME": database.path[1:],
"USER": database.username,
"PASSWORD": database.password,
Expand Down
2 changes: 1 addition & 1 deletion pgml-extension/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 pgml-extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgml"
version = "2.0.1"
version = "2.0.2"
edition = "2021"

[lib]
Expand Down
2 changes: 0 additions & 2 deletions pgml-extension/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/bash

# Exit on error, real CI
set -e

echo "Starting Postgres..."
service postgresql start

Expand Down
14 changes: 14 additions & 0 deletions pgml-extension/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ mod tests {
#[pg_test]
fn test_snapshot_lifecycle() {
load_diabetes(Some(25));

let snapshot = Snapshot::create(
"pgml.diabetes",
vec!["target".to_string()],
Expand All @@ -547,6 +548,19 @@ mod tests {
assert!(snapshot.id > 0);
}

#[pg_test]
#[should_panic]
fn test_not_fully_qualified_table() {
load_diabetes(Some(25));

let result = std::panic::catch_unwind(|| {
let _snapshot =
Snapshot::create("diabetes", vec!["target".to_string()], 0.5, Sampling::last);
});

assert!(result.is_err());
}

#[pg_test]
fn test_train_regression() {
load_diabetes(None);
Expand Down
69 changes: 56 additions & 13 deletions pgml-extension/src/orm/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ impl Snapshot {
) -> Snapshot {
let mut snapshot: Option<Snapshot> = None;
let status = Status::in_progress;

// Validate table exists.
let (_schema_name, _table_name) = Self::fully_qualified_table(relation_name);

Spi::connect(|client| {
let result = client.select("INSERT INTO pgml.snapshots (relation_name, y_column_name, test_size, test_sampling, status) VALUES ($1, $2, $3, $4::pgml.sampling, $5::pgml.status) RETURNING id, relation_name, y_column_name, test_size, test_sampling::TEXT, status::TEXT, columns, analysis, created_at, updated_at;",
Some(1),
Expand Down Expand Up @@ -256,22 +260,61 @@ impl Snapshot {
.unwrap() as usize
}

fn fully_qualified_table(relation_name: &str) -> (String, String) {
info!("Validating relation: {}", relation_name);

let parts = relation_name
.split('.')
.map(|name| name.to_string())
.collect::<Vec<String>>();

let (schema_name, table_name) = match parts.len() {
1 => (None, parts[0].clone()),
2 => (Some(parts[0].clone()), parts[1].clone()),
_ => error!(
"Relation name \"{}\" is not parsable into schema name and table name",
relation_name
),
};

match schema_name {
None => {
let table_count = Spi::get_one_with_args::<i64>("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = $1 AND table_schema = 'public'", vec![
(PgBuiltInOids::TEXTOID.oid(), table_name.clone().into_datum())
]).unwrap();

let error = format!("Relation \"{}\" could not be found in the public schema. Please specify the table schema, e.g. pgml.{}", table_name, table_name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't the search path take care of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn analysis


match table_count {
0 => error!("{}", error),
1 => (String::from("public"), table_name),
_ => error!("{}", error),
}
}

Some(schema_name) => {
let exists = Spi::get_one_with_args::<i64>("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = $1 AND table_schema = $2", vec![
(PgBuiltInOids::TEXTOID.oid(), table_name.clone().into_datum()),
(PgBuiltInOids::TEXTOID.oid(), schema_name.clone().into_datum()),
]).unwrap();

if exists == 1 {
(schema_name, table_name)
} else {
error!(
"Relation \"{}\".\"{}\" doesn't exist",
schema_name, table_name
);
}
}
}
}

#[allow(clippy::format_push_string)]
fn analyze(&mut self) {
let (schema_name, table_name) = Self::fully_qualified_table(&self.relation_name);

Spi::connect(|client| {
let parts = self
.relation_name
.split('.')
.map(|name| name.to_string())
.collect::<Vec<String>>();
let (schema_name, table_name) = match parts.len() {
1 => (String::from("public"), parts[0].clone()),
2 => (parts[0].clone(), parts[1].clone()),
_ => error!(
"Relation name {} is not parsable into schema name and table name",
self.relation_name
),
};
let mut columns: Vec<Column> = Vec::new();
client.select("SELECT column_name::TEXT, udt_name::TEXT, is_nullable::BOOLEAN, ordinal_position::INTEGER FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 ORDER BY ordinal_position ASC",
None,
Expand Down