Skip to content

add support for numerics #1324

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
Feb 22, 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
17 changes: 17 additions & 0 deletions pgml-extension/src/orm/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,12 @@ impl Model {
.unwrap()
.map_or(snapshot::NULL_CATEGORY_KEY.to_string(), |k| k.to_string())
}
pgrx_pg_sys::NUMERICOID => {
let element: Result<Option<AnyNumeric>, TryFromDatumError> = tuple.get_by_index(index);
element
.unwrap()
.map_or(snapshot::NULL_CATEGORY_KEY.to_string(), |k| k.to_string())
}
_ => error!(
"Unsupported type for categorical column: {:?}. oid: {:?}",
column.name, attribute.atttypid
Expand Down Expand Up @@ -992,6 +998,10 @@ impl Model {
let element: Result<Option<f64>, TryFromDatumError> = tuple.get_by_index(index);
features.push(element.unwrap().map_or(f32::NAN, |v| v as f32));
}
pgrx_pg_sys::NUMERICOID => {
let element: Result<Option<AnyNumeric>, TryFromDatumError> = tuple.get_by_index(index);
features.push(element.unwrap().map_or(f32::NAN, |v| v.try_into().unwrap()));
}
// TODO handle NULL to NaN for arrays
pgrx_pg_sys::BOOLARRAYOID => {
let element: Result<Option<Vec<bool>>, TryFromDatumError> =
Expand Down Expand Up @@ -1035,6 +1045,13 @@ impl Model {
features.push(*j as f32);
}
}
pgrx_pg_sys::NUMERICARRAYOID => {
let element: Result<Option<Vec<AnyNumeric>>, TryFromDatumError> =
tuple.get_by_index(index);
for j in element.as_ref().unwrap().as_ref().unwrap() {
features.push(j.clone().try_into().unwrap());
}
}
_ => error!(
"Unsupported type for quantitative column: {:?}. oid: {:?}",
column.name, attribute.atttypid
Expand Down
10 changes: 10 additions & 0 deletions pgml-extension/src/orm/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ impl Snapshot {
"int8" => row[column.position].value::<i64>().unwrap().map(|v| v.to_string()),
"float4" => row[column.position].value::<f32>().unwrap().map(|v| v.to_string()),
"float8" => row[column.position].value::<f64>().unwrap().map(|v| v.to_string()),
"numeric" => row[column.position].value::<AnyNumeric>().unwrap().map(|v| v.to_string()),
"bpchar" | "text" | "varchar" => {
Comment on lines 990 to 994
Copy link

@eeeebbbbrrrr eeeebbbbrrrr Feb 22, 2024

Choose a reason for hiding this comment

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

Hey! @montanalow asked me to take a quick look here.

I feel like these string types names would be better as OID values. pgrx has them all in pg_sys::, like pg_sys::INT8OID, pg_sys::NUMERICOID, pg_sys::NUMERICARRAYOID, and so on. It looks like this is the approach you use up in model.rs already.

I guess you'd have to figure out the type oids wherever you construct the Column entries, but that doesn't seem too difficult. I think in that SELECT statement around line 505 you could write udt_name::text::regtype::oid instead of udt_name::TEXT.

Comparing on Oid value will be a little more performant, I suppose, and it'll future proof you from accidentally making type-os in the code.

row[column.position].value::<String>().unwrap().map(|v| v.to_string())
}
Expand Down Expand Up @@ -1078,6 +1079,14 @@ impl Snapshot {
vector.push(j as f32)
}
}
"numeric[]" => {
let vec = row[column.position].value::<Vec<AnyNumeric>>().unwrap().unwrap();
check_column_size(column, vec.len());

for j in vec {
vector.push(j.rescale::<6,0>().unwrap().try_into().unwrap())
}
}
_ => error!(
"Unhandled type for quantitative array column: {} {:?}",
column.name, column.pg_type
Expand All @@ -1092,6 +1101,7 @@ impl Snapshot {
"int8" => row[column.position].value::<i64>().unwrap().map(|v| v as f32),
"float4" => row[column.position].value::<f32>().unwrap(),
"float8" => row[column.position].value::<f64>().unwrap().map(|v| v as f32),
"numeric" => row[column.position].value::<AnyNumeric>().unwrap().map(|v| v.rescale::<6,0>().unwrap().try_into().unwrap()),
_ => error!(
"Unhandled type for quantitative scalar column: {} {:?}",
column.name, column.pg_type
Comment on lines 1105 to 1107
Copy link

@eeeebbbbrrrr eeeebbbbrrrr Feb 22, 2024

Choose a reason for hiding this comment

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

And now that you have the type oids, you could call its output function here and just get its string representation from Postgres. Who knows if it'd then be something you could otherwise handle, but maybe?

Same thing around line 1090 for the array case.

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy