|
| 1 | +use pgrx::{aggregate::*, pg_aggregate, pg_sys}; |
| 2 | + |
| 3 | +use crate::typeid::TypeID; |
| 4 | + |
| 5 | +pub struct TypeIDMin; |
| 6 | +pub struct TypeIDMax; |
| 7 | + |
| 8 | +#[pg_aggregate] |
| 9 | +impl Aggregate for TypeIDMin { |
| 10 | + const NAME: &'static str = "min"; |
| 11 | + type Args = TypeID; |
| 12 | + type State = Option<TypeID>; |
| 13 | + |
| 14 | + fn state( |
| 15 | + current: Self::State, |
| 16 | + arg: Self::Args, |
| 17 | + _fcinfo: pg_sys::FunctionCallInfo, |
| 18 | + ) -> Self::State { |
| 19 | + match current { |
| 20 | + None => Some(arg), |
| 21 | + Some(current) => Some(if arg < current { arg } else { current }), |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[pg_aggregate] |
| 27 | +impl Aggregate for TypeIDMax { |
| 28 | + const NAME: &'static str = "max"; |
| 29 | + type Args = TypeID; |
| 30 | + type State = Option<TypeID>; |
| 31 | + |
| 32 | + fn state( |
| 33 | + current: Self::State, |
| 34 | + arg: Self::Args, |
| 35 | + _fcinfo: pg_sys::FunctionCallInfo, |
| 36 | + ) -> Self::State { |
| 37 | + match current { |
| 38 | + None => Some(arg), |
| 39 | + Some(current) => Some(if arg > current { arg } else { current }), |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(any(test, feature = "pg_test"))] |
| 45 | +#[pgrx::pg_schema] |
| 46 | +mod tests { |
| 47 | + use super::*; |
| 48 | + use pgrx::prelude::*; |
| 49 | + |
| 50 | + #[pg_test] |
| 51 | + fn test_typeid_min_max_aggregates() { |
| 52 | + Spi::connect(|mut client| { |
| 53 | + // Create a temporary table |
| 54 | + client |
| 55 | + .update("CREATE TEMPORARY TABLE test_typeid (id typeid)", None, None) |
| 56 | + .unwrap(); |
| 57 | + |
| 58 | + // Insert some test data |
| 59 | + client.update("INSERT INTO test_typeid VALUES (typeid_generate('user')), (typeid_generate('user')), (typeid_generate('user'))", None, None).unwrap(); |
| 60 | + |
| 61 | + // Test min aggregate |
| 62 | + let result = client |
| 63 | + .select("SELECT min(id) FROM test_typeid", None, None) |
| 64 | + .unwrap(); |
| 65 | + |
| 66 | + assert_eq!(result.len(), 1); |
| 67 | + let min_typeid: TypeID = result |
| 68 | + .first() |
| 69 | + .get_one() |
| 70 | + .unwrap() |
| 71 | + .expect("didnt get min typeid"); |
| 72 | + |
| 73 | + // Test max aggregate |
| 74 | + let result = client |
| 75 | + .select("SELECT max(id) FROM test_typeid", None, None) |
| 76 | + .unwrap(); |
| 77 | + assert_eq!(result.len(), 1); |
| 78 | + let max_typeid: TypeID = result |
| 79 | + .first() |
| 80 | + .get_one() |
| 81 | + .unwrap() |
| 82 | + .expect("didnt get max typeid"); |
| 83 | + |
| 84 | + // Verify that max is greater than min |
| 85 | + assert!(max_typeid > min_typeid); |
| 86 | + |
| 87 | + // Test with empty table |
| 88 | + client.update("TRUNCATE test_typeid", None, None).unwrap(); |
| 89 | + let result = client |
| 90 | + .select("SELECT min(id), max(id) FROM test_typeid", None, None) |
| 91 | + .unwrap(); |
| 92 | + assert_eq!(result.len(), 1); |
| 93 | + |
| 94 | + let (min_typeid, max_typeid): (Option<TypeID>, Option<TypeID>) = |
| 95 | + result.first().get_two().unwrap(); |
| 96 | + assert_eq!(min_typeid, None); |
| 97 | + assert_eq!(max_typeid, None); |
| 98 | + |
| 99 | + // Test with single value |
| 100 | + client |
| 101 | + .update( |
| 102 | + "INSERT INTO test_typeid VALUES (typeid_generate('user'))", |
| 103 | + None, |
| 104 | + None, |
| 105 | + ) |
| 106 | + .unwrap(); |
| 107 | + let result = client |
| 108 | + .select("SELECT min(id), max(id) FROM test_typeid", None, None) |
| 109 | + .unwrap(); |
| 110 | + assert_eq!(result.len(), 1); |
| 111 | + let (min_typeid, max_typeid): (Option<TypeID>, Option<TypeID>) = |
| 112 | + result.first().get_two().unwrap(); |
| 113 | + |
| 114 | + assert_eq!(min_typeid.unwrap(), max_typeid.unwrap()); |
| 115 | + |
| 116 | + // Test with multiple prefixes |
| 117 | + client.update("TRUNCATE test_typeid", None, None).unwrap(); |
| 118 | + client.update("INSERT INTO test_typeid VALUES (typeid_generate('user')), (typeid_generate('post')), (typeid_generate('comment'))", None, None).unwrap(); |
| 119 | + let result = client |
| 120 | + .select("SELECT min(id), max(id) FROM test_typeid", None, None) |
| 121 | + .unwrap(); |
| 122 | + assert_eq!(result.len(), 1); |
| 123 | + let (min_typeid, max_typeid): (Option<TypeID>, Option<TypeID>) = |
| 124 | + result.first().get_two().unwrap(); |
| 125 | + |
| 126 | + assert!(min_typeid.unwrap().type_prefix() == "comment"); |
| 127 | + assert!(max_typeid.unwrap().type_prefix() == "user"); |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments