Skip to content

Commit e9f1cc4

Browse files
chore: implemented all get methods for GenericRow (#82)
1 parent 12d464e commit e9f1cc4

File tree

2 files changed

+116
-16
lines changed

2 files changed

+116
-16
lines changed

crates/fluss/src/row/datum.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub enum Datum<'a> {
4444
#[display("{0}")]
4545
Bool(bool),
4646
#[display("{0}")]
47+
Int8(i8),
48+
#[display("{0}")]
4749
Int16(i16),
4850
#[display("{0}")]
4951
Int32(i32),
@@ -78,6 +80,13 @@ impl Datum<'_> {
7880
_ => panic!("not a string: {self:?}"),
7981
}
8082
}
83+
84+
pub fn as_blob(&self) -> &[u8] {
85+
match self {
86+
Self::Blob(blob) => blob.as_ref(),
87+
_ => panic!("not a blob: {self:?}"),
88+
}
89+
}
8190
}
8291

8392
// ----------- implement from
@@ -95,6 +104,20 @@ impl<'a> From<i64> for Datum<'a> {
95104
}
96105
}
97106

107+
impl<'a> From<i8> for Datum<'a> {
108+
#[inline]
109+
fn from(i: i8) -> Datum<'a> {
110+
Datum::Int8(i)
111+
}
112+
}
113+
114+
impl<'a> From<i16> for Datum<'a> {
115+
#[inline]
116+
fn from(i: i16) -> Datum<'a> {
117+
Datum::Int16(i)
118+
}
119+
}
120+
98121
impl<'a> From<&'a str> for Datum<'a> {
99122
#[inline]
100123
fn from(s: &'a str) -> Datum<'a> {
@@ -134,6 +157,18 @@ impl TryFrom<&Datum<'_>> for i32 {
134157
}
135158
}
136159

160+
impl TryFrom<&Datum<'_>> for i16 {
161+
type Error = ();
162+
163+
#[inline]
164+
fn try_from(from: &Datum) -> std::result::Result<Self, Self::Error> {
165+
match from {
166+
Datum::Int16(i) => Ok(*i),
167+
_ => Err(()),
168+
}
169+
}
170+
}
171+
137172
impl TryFrom<&Datum<'_>> for i64 {
138173
type Error = ();
139174

@@ -146,6 +181,42 @@ impl TryFrom<&Datum<'_>> for i64 {
146181
}
147182
}
148183

184+
impl TryFrom<&Datum<'_>> for f32 {
185+
type Error = ();
186+
187+
#[inline]
188+
fn try_from(from: &Datum) -> std::result::Result<Self, Self::Error> {
189+
match from {
190+
Datum::Float32(f) => Ok(f.into_inner()),
191+
_ => Err(()),
192+
}
193+
}
194+
}
195+
196+
impl TryFrom<&Datum<'_>> for f64 {
197+
type Error = ();
198+
199+
#[inline]
200+
fn try_from(from: &Datum) -> std::result::Result<Self, Self::Error> {
201+
match from {
202+
Datum::Float64(f) => Ok(f.into_inner()),
203+
_ => Err(()),
204+
}
205+
}
206+
}
207+
208+
impl TryFrom<&Datum<'_>> for bool {
209+
type Error = ();
210+
211+
#[inline]
212+
fn try_from(from: &Datum) -> std::result::Result<Self, Self::Error> {
213+
match from {
214+
Datum::Bool(b) => Ok(*b),
215+
_ => Err(()),
216+
}
217+
}
218+
}
219+
149220
impl<'a> TryFrom<&Datum<'a>> for &'a str {
150221
type Error = ();
151222

@@ -158,6 +229,25 @@ impl<'a> TryFrom<&Datum<'a>> for &'a str {
158229
}
159230
}
160231

232+
impl TryFrom<&Datum<'_>> for i8 {
233+
type Error = ();
234+
235+
#[inline]
236+
fn try_from(from: &Datum) -> std::result::Result<Self, Self::Error> {
237+
match from {
238+
Datum::Int8(i) => Ok(*i),
239+
_ => Err(()),
240+
}
241+
}
242+
}
243+
244+
impl<'a> From<bool> for Datum<'a> {
245+
#[inline]
246+
fn from(b: bool) -> Datum<'a> {
247+
Datum::Bool(b)
248+
}
249+
}
250+
161251
pub trait ToArrow {
162252
fn append_to(&self, builder: &mut dyn ArrayBuilder) -> Result<()>;
163253
}
@@ -184,6 +274,7 @@ impl Datum<'_> {
184274

185275
match self {
186276
Datum::Null => {
277+
append_null_to_arrow!(Int8Builder);
187278
append_null_to_arrow!(BooleanBuilder);
188279
append_null_to_arrow!(Int16Builder);
189280
append_null_to_arrow!(Int32Builder);
@@ -194,6 +285,7 @@ impl Datum<'_> {
194285
append_null_to_arrow!(BinaryBuilder);
195286
}
196287
Datum::Bool(v) => append_value_to_arrow!(BooleanBuilder, *v),
288+
Datum::Int8(v) => append_value_to_arrow!(Int8Builder, *v),
197289
Datum::Int16(v) => append_value_to_arrow!(Int16Builder, *v),
198290
Datum::Int32(v) => append_value_to_arrow!(Int32Builder, *v),
199291
Datum::Int64(v) => append_value_to_arrow!(Int64Builder, *v),

crates/fluss/src/row/mod.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ impl<'a> InternalRow for GenericRow<'a> {
8585
false
8686
}
8787

88-
fn get_boolean(&self, _pos: usize) -> bool {
89-
todo!()
88+
fn get_boolean(&self, pos: usize) -> bool {
89+
self.values.get(pos).unwrap().try_into().unwrap()
9090
}
9191

92-
fn get_byte(&self, _pos: usize) -> i8 {
93-
todo!()
92+
fn get_byte(&self, pos: usize) -> i8 {
93+
self.values.get(pos).unwrap().try_into().unwrap()
9494
}
9595

96-
fn get_short(&self, _pos: usize) -> i16 {
97-
todo!()
96+
fn get_short(&self, pos: usize) -> i16 {
97+
self.values.get(pos).unwrap().try_into().unwrap()
9898
}
9999

100100
fn get_int(&self, pos: usize) -> i32 {
@@ -105,28 +105,36 @@ impl<'a> InternalRow for GenericRow<'a> {
105105
self.values.get(_pos).unwrap().try_into().unwrap()
106106
}
107107

108-
fn get_float(&self, _pos: usize) -> f32 {
109-
todo!()
108+
fn get_float(&self, pos: usize) -> f32 {
109+
self.values.get(pos).unwrap().try_into().unwrap()
110110
}
111111

112-
fn get_double(&self, _pos: usize) -> f64 {
113-
todo!()
112+
fn get_double(&self, pos: usize) -> f64 {
113+
self.values.get(pos).unwrap().try_into().unwrap()
114114
}
115115

116-
fn get_char(&self, _pos: usize, _length: usize) -> String {
117-
todo!()
116+
fn get_char(&self, pos: usize, length: usize) -> String {
117+
let value = self.get_string(pos);
118+
if value.len() != length {
119+
panic!(
120+
"Length mismatch for fixed-size char: expected {}, got {}",
121+
length,
122+
value.len()
123+
);
124+
}
125+
value.to_string()
118126
}
119127

120128
fn get_string(&self, pos: usize) -> &str {
121129
self.values.get(pos).unwrap().try_into().unwrap()
122130
}
123131

124-
fn get_binary(&self, _pos: usize, _length: usize) -> Vec<u8> {
125-
todo!()
132+
fn get_binary(&self, pos: usize, _length: usize) -> Vec<u8> {
133+
self.values.get(pos).unwrap().as_blob().to_vec()
126134
}
127135

128-
fn get_bytes(&self, _pos: usize) -> Vec<u8> {
129-
todo!()
136+
fn get_bytes(&self, pos: usize) -> Vec<u8> {
137+
self.values.get(pos).unwrap().as_blob().to_vec()
130138
}
131139
}
132140

0 commit comments

Comments
 (0)