Skip to content
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

Upgrade pgx to 0.7.1 #709

Merged
merged 13 commits into from
Feb 16, 2023
4 changes: 2 additions & 2 deletions extension/src/aggregate_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pgx::pg_sys;

// TODO move to func_utils once there are enough function to warrant one
pub unsafe fn get_collation(fcinfo: pg_sys::FunctionCallInfo) -> Option<pg_sys::Oid> {
if (*fcinfo).fncollation == 0 {
if (*fcinfo).fncollation == pg_sys::Oid::INVALID {
None
} else {
Some((*fcinfo).fncollation)
Expand All @@ -13,7 +13,7 @@ pub unsafe fn get_collation(fcinfo: pg_sys::FunctionCallInfo) -> Option<pg_sys::

pub fn get_collation_or_default(fcinfo: pg_sys::FunctionCallInfo) -> Option<pg_sys::Oid> {
if fcinfo.is_null() {
Some(100) // TODO: default OID, there should be a constant for this
Some(unsafe { pg_sys::Oid::from_u32_unchecked(100) }) // TODO: default OID, there should be a constant for this
} else {
unsafe { get_collation(fcinfo) }
}
Expand Down
10 changes: 5 additions & 5 deletions extension/src/datum_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct TextSerializableDatumWriter {

impl TextSerializableDatumWriter {
pub fn from_oid(typoid: Oid) -> Self {
let mut type_output = 0;
let mut type_output = pg_sys::Oid::INVALID;
let mut typ_is_varlena = false;
let mut flinfo = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };

Expand All @@ -81,13 +81,13 @@ impl TextSerializableDatumWriter {

pub struct DatumFromSerializedTextReader {
flinfo: pg_sys::FmgrInfo,
typ_io_param: u32,
typ_io_param: pg_sys::Oid,
}

impl DatumFromSerializedTextReader {
pub fn from_oid(typoid: Oid) -> Self {
let mut type_input = 0;
let mut typ_io_param = 0;
let mut type_input = pg_sys::Oid::INVALID;
let mut typ_io_param = pg_sys::oids::Oid::INVALID;
let mut flinfo = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
unsafe {
pg_sys::getTypeInputInfo(typoid, &mut type_input, &mut typ_io_param);
Expand Down Expand Up @@ -237,7 +237,7 @@ impl Serialize for DatumHashBuilder {
where
S: serde::Serializer,
{
let collation = if self.collation == 0 {
let collation = if self.collation == pg_sys::oids::Oid::INVALID {
None
} else {
Some(PgCollationId(self.collation))
Expand Down
23 changes: 14 additions & 9 deletions extension/src/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ pub mod toolkit_experimental {

build! {
SpaceSavingAggregate {
type_oid: trans.type_oid() as _,
type_oid: trans.type_oid().into(),
num_values: trans.entries.len() as _,
values_seen: trans.total_vals,
freq_param: trans.freq_param,
Expand All @@ -466,14 +466,14 @@ pub mod toolkit_experimental {
let mut trans = if agg.topn == 0 {
SpaceSavingTransState::freq_agg_from_type_id(
agg.freq_param,
agg.type_oid,
unsafe { Oid::from_u32_unchecked(agg.type_oid) },
collation,
)
} else {
SpaceSavingTransState::topn_agg_from_type_id(
agg.freq_param,
agg.topn as u32,
agg.type_oid,
unsafe { Oid::from_u32_unchecked(agg.type_oid) },
collation,
)
};
Expand Down Expand Up @@ -1287,14 +1287,19 @@ pub fn freq_iter<'a>(
),
> {
unsafe {
if ty.oid() != agg.type_oid {
if ty.oid().as_u32() != agg.type_oid {
pgx::error!("mischatched types")
}
let counts = agg.counts.slice().iter().zip(agg.overcounts.slice().iter());
TableIterator::new(agg.datums.clone().into_iter().zip(counts).map_while(
move |(value, (&count, &overcount))| {
let total = agg.values_seen as f64;
let value = AnyElement::from_polymorphic_datum(value, false, agg.type_oid).unwrap();
let value = AnyElement::from_polymorphic_datum(
value,
false,
Oid::from_u32_unchecked(agg.type_oid),
)
.unwrap();
let min_freq = (count - overcount) as f64 / total;
let max_freq = count as f64 / total;
Some((value, min_freq, max_freq))
Expand Down Expand Up @@ -1394,7 +1399,7 @@ pub fn topn(
ty: Option<AnyElement>,
) -> SetOfIterator<AnyElement> {
// If called with a NULL, assume type matches
if ty.is_some() && ty.unwrap().oid() != agg.type_oid {
if ty.is_some() && ty.unwrap().oid().as_u32() != agg.type_oid {
pgx::error!("mischatched types")
}

Expand All @@ -1418,7 +1423,7 @@ pub fn topn(
)
// TODO Shouldn't failure to convert to AnyElement cause error, not early stop?
.map_while(move |value| unsafe {
AnyElement::from_polymorphic_datum(value, false, type_oid)
AnyElement::from_polymorphic_datum(value, false, Oid::from_u32_unchecked(type_oid))
}),
)
}
Expand Down Expand Up @@ -1527,7 +1532,7 @@ pub fn max_frequency(agg: SpaceSavingAggregate<'_>, value: AnyElement) -> f64 {
match agg
.datums
.iter()
.position(|datum| value == (datum, agg.type_oid).into())
.position(|datum| value == (datum, unsafe { Oid::from_u32_unchecked(agg.type_oid) }).into())
{
Some(idx) => agg.counts.slice()[idx] as f64 / agg.values_seen as f64,
None => 0.,
Expand All @@ -1540,7 +1545,7 @@ pub fn min_frequency(agg: SpaceSavingAggregate<'_>, value: AnyElement) -> f64 {
match agg
.datums
.iter()
.position(|datum| value == (datum, agg.type_oid).into())
.position(|datum| value == (datum, unsafe { Oid::from_u32_unchecked(agg.type_oid) }).into())
{
Some(idx) => {
(agg.counts.slice()[idx] - agg.overcounts.slice()[idx]) as f64 / agg.values_seen as f64
Expand Down
22 changes: 18 additions & 4 deletions extension/src/hyperloglog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ mod tests {
// Unable to build the hyperloglog through hyperloglog_trans, as that requires a valid fcinfo to determine OIDs.

// FIXME: use named constant for default correlation oid
syvb marked this conversation as resolved.
Show resolved Hide resolved
let hasher = DatumHashBuilder::from_type_id(pg_sys::TEXTOID, Some(100));
let hasher = DatumHashBuilder::from_type_id(
pg_sys::TEXTOID,
Some(crate::serialization::collations::DEFAULT_COLLATION_OID),
);
let mut control = HyperLogLogTrans {
logger: HLL::new(6, hasher),
};
Expand All @@ -644,7 +647,11 @@ mod tests {
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 136, 136, 9, 7,
8, 74, 76, 47, 200, 231, 53, 25, 3, 0, 0, 0, 0, 0, 0, 0, 6, 9, 0, 0, 0, 1,
];
bincode::serialize_into(&mut expected, &PgCollationId(100)).unwrap();
bincode::serialize_into(
&mut expected,
&PgCollationId(crate::serialization::collations::DEFAULT_COLLATION_OID),
)
.unwrap();
assert_eq!(buffer, expected);

let expected = pgx::varlena::rust_byte_slice_to_bytea(&expected);
Expand All @@ -670,7 +677,11 @@ mod tests {
49, 2, 8, 65, 131, 24, 32, 133, 12, 50, 66, 12, 48, 197, 12, 81, 130, 255, 58, 6,
255, 255, 255, 255, 255, 255, 255, 3, 9, 0, 0, 0, 1,
];
bincode::serialize_into(&mut expected, &PgCollationId(100)).unwrap();
bincode::serialize_into(
&mut expected,
&PgCollationId(crate::serialization::collations::DEFAULT_COLLATION_OID),
)
.unwrap();
assert_eq!(buffer, expected);

let expected = pgx::varlena::rust_byte_slice_to_bytea(&expected);
Expand Down Expand Up @@ -749,7 +760,10 @@ mod tests {
.first()
.get_one::<String>();

let default_collation = ron::to_string(&PgCollationId(100)).unwrap();
let default_collation = ron::to_string(&PgCollationId(
crate::serialization::collations::DEFAULT_COLLATION_OID,
))
.unwrap();
let expected = format!(
"(\
version:1,\
Expand Down
2 changes: 1 addition & 1 deletion extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod lttb;
pub mod nmost;
pub mod range;
pub mod saturation;
pub(crate) mod serialization;
pub mod state_aggregate;
pub mod stats_agg;
pub mod tdigest;
Expand All @@ -38,7 +39,6 @@ mod duration;
mod palloc;
mod pg_any_element;
mod raw;
mod serialization;
mod stabilization_info;
mod stabilization_tests;
mod type_builder;
Expand Down
2 changes: 1 addition & 1 deletion extension/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use pgx::pg_sys;
use std::ffi::CStr;

mod collations;
pub(crate) mod collations;
mod functions;
mod types;

Expand Down
41 changes: 26 additions & 15 deletions extension/src/serialization/collations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl PgCollationId {
#[allow(non_upper_case_globals)]
const Anum_pg_collation_oid: u32 = 1;
// https://github.com/postgres/postgres/blob/e955bd4b6c2bcdbd253837f6cf4c7520b98e69d4/src/include/catalog/pg_collation.dat
const DEFAULT_COLLATION_OID: u32 = 100;
pub(crate) const DEFAULT_COLLATION_OID: Oid = unsafe { pg_sys::Oid::from_u32_unchecked(100) };

#[allow(non_camel_case_types)]
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -168,7 +168,7 @@ impl<'de> Deserialize<'de> for PgCollationId {

let collation = <Option<(&str, &str)>>::deserialize(deserializer)?;
let (namespace, name) = match collation {
None => return Ok(Self(0)),
None => return Ok(Self(pg_sys::Oid::INVALID)),
Some(qualified_name) => qualified_name,
};

Expand Down Expand Up @@ -229,7 +229,7 @@ impl<'de> Deserialize<'de> for PgCollationId {
// The default collation doesn't necessarily exist in the
// collations catalog, so check that specially
if name == &**DEFAULT_COLLATION_NAME {
return Ok(PgCollationId(100));
return Ok(PgCollationId(DEFAULT_COLLATION_OID));
}
return Err(D::Error::custom(format!(
"invalid collation {:?}.{:?}",
Expand All @@ -254,22 +254,33 @@ unsafe fn get_struct<T>(tuple: pg_sys::HeapTuple) -> *mut T {
mod tests {

use super::PgCollationId;
use pgx::{pg_guard, pg_sys, pg_test};
use pgx::{pg_sys, pg_test};

const COLLATION_ID_950: PgCollationId =
PgCollationId(unsafe { pg_sys::Oid::from_u32_unchecked(950) });
const COLLATION_ID_951: PgCollationId =
PgCollationId(unsafe { pg_sys::Oid::from_u32_unchecked(951) });

// TODO is there a way we can test more of this without making it flaky?
#[pg_test]
fn test_pg_collation_id_serialize_default_collation_ron() {
let serialized = ron::to_string(&PgCollationId(100)).unwrap();
let serialized = ron::to_string(&PgCollationId(
crate::serialization::collations::DEFAULT_COLLATION_OID,
))
.unwrap();
let deserialized: PgCollationId = ron::from_str(&serialized).unwrap();
assert_ne!(deserialized.0, 0);
let serialized = ron::to_string(&PgCollationId(100)).unwrap();
assert_ne!(deserialized.0, pg_sys::Oid::INVALID);
let serialized = ron::to_string(&PgCollationId(
crate::serialization::collations::DEFAULT_COLLATION_OID,
))
.unwrap();
let deserialized2: PgCollationId = ron::from_str(&serialized).unwrap();
assert_eq!(deserialized2.0, deserialized.0);
}

#[pg_test]
fn test_pg_collation_id_serialize_c_collation() {
let serialized = bincode::serialize(&PgCollationId(950)).unwrap();
let serialized = bincode::serialize(&COLLATION_ID_950).unwrap();
assert_eq!(
serialized,
vec![
Expand All @@ -278,21 +289,21 @@ mod tests {
]
);
let deserialized: PgCollationId = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized.0, 950);
assert_eq!(deserialized.0, COLLATION_ID_950.0);
}

// TODO this test may be too flaky depending on what the default collation actually is
#[pg_test]
fn test_pg_collation_id_serialize_c_collation_ron() {
let serialized = ron::to_string(&PgCollationId(950)).unwrap();
let serialized = ron::to_string(&COLLATION_ID_950).unwrap();
assert_eq!(&*serialized, "Some((\"pg_catalog\",\"C\"))",);
let deserialized: PgCollationId = ron::from_str(&serialized).unwrap();
assert_eq!(deserialized.0, 950);
assert_eq!(deserialized.0, COLLATION_ID_950.0);
}

#[pg_test]
fn test_pg_collation_id_serialize_posix_collation() {
let serialized = bincode::serialize(&PgCollationId(951)).unwrap();
let serialized = bincode::serialize(&COLLATION_ID_951).unwrap();
assert_eq!(
serialized,
vec![
Expand All @@ -301,15 +312,15 @@ mod tests {
]
);
let deserialized: PgCollationId = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized.0, 951);
assert_eq!(deserialized.0, COLLATION_ID_951.0);
}

// TODO this test may be too flaky depending on what the default collation actually is
#[pg_test]
fn test_pg_collation_id_serialize_posix_collation_ron() {
let serialized = ron::to_string(&PgCollationId(951)).unwrap();
let serialized = ron::to_string(&COLLATION_ID_951).unwrap();
assert_eq!(&*serialized, "Some((\"pg_catalog\",\"POSIX\"))",);
let deserialized: PgCollationId = ron::from_str(&serialized).unwrap();
assert_eq!(deserialized.0, 951);
assert_eq!(deserialized.0, COLLATION_ID_951.0);
}
}
2 changes: 1 addition & 1 deletion extension/src/serialization/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ impl<'de> Deserialize<'de> for PgProcId {
)
};

Ok(Self(oid.value() as _))
Ok(Self(unsafe { Oid::from_u32_unchecked(oid.value() as _) }))
}
}
8 changes: 4 additions & 4 deletions extension/src/serialization/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use pgx::*;
/// that these types can be stored more compactly if desired.
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct ShortTypeId(pub u32);
pub struct ShortTypeId(pub Oid);

impl_flat_serializable!(ShortTypeId);

impl From<u32> for ShortTypeId {
fn from(id: u32) -> Self {
impl From<Oid> for ShortTypeId {
fn from(id: Oid) -> Self {
Self(id)
}
}

impl From<ShortTypeId> for u32 {
impl From<ShortTypeId> for Oid {
fn from(id: ShortTypeId) -> Self {
id.0
}
Expand Down
17 changes: 12 additions & 5 deletions extension/src/time_vector/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ pub fn arrow_add_unstable_element<'p>(
)]
pub unsafe fn pipeline_support(input: Internal) -> Internal {
pipeline_support_helper(input, |old_pipeline, new_element| {
let new_element =
UnstableTimevectorPipeline::from_polymorphic_datum(new_element, false, 0).unwrap();
let new_element = UnstableTimevectorPipeline::from_polymorphic_datum(
new_element,
false,
pg_sys::Oid::INVALID,
)
.unwrap();
arrow_add_unstable_element(old_pipeline, new_element)
.into_datum()
.unwrap()
Expand Down Expand Up @@ -252,9 +256,12 @@ pub(crate) unsafe fn pipeline_support_helper(

let new_element_const: *mut pg_sys::Const = arg2.cast();

let old_pipeline =
UnstableTimevectorPipeline::from_polymorphic_datum((*old_const).constvalue, false, 0)
.unwrap();
let old_pipeline = UnstableTimevectorPipeline::from_polymorphic_datum(
(*old_const).constvalue,
false,
pg_sys::Oid::INVALID,
)
.unwrap();
let new_pipeline = make_new_pipeline(old_pipeline, (*new_element_const).constvalue);

let new_const = pg_sys::palloc(size_of::<pg_sys::Const>()).cast();
Expand Down
Loading