1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::*;
#[inline]
pub fn composite_row_type_make_tuple(row: pg_sys::Datum) -> PgBox<pg_sys::HeapTupleData> {
let htup_header = unsafe { pg_sys::pg_detoast_datum_packed(row as *mut pg_sys::varlena) }
as pg_sys::HeapTupleHeader;
let mut tuple = PgBox::<pg_sys::HeapTupleData>::alloc0();
tuple.t_len = heap_tuple_header_get_datum_length(htup_header) as u32;
tuple.t_data = htup_header;
tuple
}
pub fn deconstruct_row_type<'a>(
tupdesc: &'a PgTupleDesc,
row: pg_sys::Datum,
) -> Array<'a, pg_sys::Datum> {
extern "C" {
fn pgx_deconstruct_row_type(
tupdesc: pg_sys::TupleDesc,
row: pg_sys::Datum,
columns: *mut *mut pg_sys::Datum,
nulls: *mut *mut bool,
);
}
let mut columns = std::ptr::null_mut();
let mut nulls = std::ptr::null_mut();
unsafe {
pgx_deconstruct_row_type(tupdesc.as_ptr(), row, &mut columns, &mut nulls);
Array::over(
columns,
nulls,
tupdesc.iter().filter(|att| !att.is_dropped()).count(),
)
}
}
#[inline]
pub fn heap_tuple_header_get_datum_length(htup_header: pg_sys::HeapTupleHeader) -> usize {
if htup_header.is_null() {
panic!("Attempt to dereference a null HeapTupleHeader");
}
unsafe { crate::varlena::varsize(htup_header as *const pg_sys::varlena) }
}
#[inline]
pub fn heap_tuple_get_datum(heap_tuple: pg_sys::HeapTuple) -> pg_sys::Datum {
unsafe { pg_sys::HeapTupleHeaderGetDatum((*heap_tuple).t_data) }
}
#[inline]
pub unsafe fn heap_tuple_header_get_type_id(htup_header: pg_sys::HeapTupleHeader) -> pg_sys::Oid {
htup_header.as_ref().unwrap().t_choice.t_datum.datum_typeid
}
#[inline]
pub unsafe fn heap_tuple_header_get_typmod(htup_header: pg_sys::HeapTupleHeader) -> i32 {
htup_header.as_ref().unwrap().t_choice.t_datum.datum_typmod
}
extern "C" {
fn pgx_heap_getattr(
tuple: *const pg_sys::HeapTupleData,
attnum: u32,
tupdesc: pg_sys::TupleDesc,
isnull: *mut bool,
) -> pg_sys::Datum;
}
#[inline]
pub fn heap_getattr<T: FromDatum>(
tuple: &PgBox<pg_sys::HeapTupleData>,
attno: usize,
tupdesc: &PgTupleDesc,
) -> Option<T> {
let mut is_null = false;
let datum =
unsafe { pgx_heap_getattr(tuple.as_ptr(), attno as u32, tupdesc.as_ptr(), &mut is_null) };
let typoid = tupdesc.get(attno - 1).expect("no attribute").type_oid();
if is_null {
None
} else {
unsafe { T::from_datum(datum, false, typoid.value()) }
}
}
#[derive(Debug, Clone)]
pub struct DatumWithTypeInfo {
pub datum: pg_sys::Datum,
pub is_null: bool,
pub typoid: PgOid,
pub typlen: i16,
pub typbyval: bool,
}
impl DatumWithTypeInfo {
#[inline]
pub fn into_value<T: FromDatum>(self) -> T {
unsafe { T::from_datum(self.datum, self.is_null, self.typoid.value()).unwrap() }
}
}
#[inline]
pub fn heap_getattr_datum_ex(
tuple: &PgBox<pg_sys::HeapTupleData>,
attno: usize,
tupdesc: &PgTupleDesc,
) -> DatumWithTypeInfo {
let mut is_null = false;
let datum =
unsafe { pgx_heap_getattr(tuple.as_ptr(), attno as u32, tupdesc.as_ptr(), &mut is_null) };
let typoid = tupdesc.get(attno - 1).expect("no attribute").type_oid();
let mut typlen = 0;
let mut typbyval = false;
let mut typalign = 0 as std::os::raw::c_char;
unsafe {
pg_sys::get_typlenbyvalalign(typoid.value(), &mut typlen, &mut typbyval, &mut typalign);
}
DatumWithTypeInfo {
datum,
is_null,
typoid,
typlen,
typbyval,
}
}