Skip to content

Commit

Permalink
refactor(common): do not leak types on row (risingwavelabs#9053)
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao authored Apr 7, 2023
1 parent 1aa1278 commit de6c5a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 80 deletions.
69 changes: 0 additions & 69 deletions src/common/src/row/owned_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,75 +89,6 @@ impl OwnedRow {
}
}

impl OwnedRow {
pub fn is_null(&self, idx: usize) -> bool {
self[idx].is_none()
}

pub fn get_int16(&self, idx: usize) -> i16 {
match self[idx].as_ref().unwrap() {
ScalarImpl::Int16(num) => *num,
_ => unreachable!("type is not int16 at index: {}", idx),
}
}

pub fn get_int32(&self, idx: usize) -> i32 {
match self[idx].as_ref().unwrap() {
ScalarImpl::Int32(num) => *num,
_ => unreachable!("type is not int32 at index: {}", idx),
}
}

pub fn get_int64(&self, idx: usize) -> i64 {
match self[idx].as_ref().unwrap() {
ScalarImpl::Int64(num) => *num,
_ => unreachable!("type is not int64 at index: {}", idx),
}
}

pub fn get_f32(&self, idx: usize) -> f32 {
match self[idx].as_ref().unwrap() {
ScalarImpl::Float32(num) => num.into_inner(),
_ => unreachable!("type is not float32 at index: {}", idx),
}
}

pub fn get_f64(&self, idx: usize) -> f64 {
match self[idx].as_ref().unwrap() {
ScalarImpl::Float64(num) => num.into_inner(),
_ => unreachable!("type is not float64 at index: {}", idx),
}
}

pub fn get_bool(&self, idx: usize) -> bool {
match self[idx].as_ref().unwrap() {
ScalarImpl::Bool(num) => *num,
_ => unreachable!("type is not boolean at index: {}", idx),
}
}

pub fn get_utf8(&self, idx: usize) -> &str {
match self[idx].as_ref().unwrap() {
ScalarImpl::Utf8(s) => s.as_ref(),
_ => unreachable!("type is not utf8 at index: {}", idx),
}
}

pub fn get_datetime(&self, idx: usize) -> &Timestamp {
match self[idx].as_ref().unwrap() {
ScalarImpl::Timestamp(dt) => dt,
_ => unreachable!("type is not NaiveDateTime at index: {}", idx),
}
}

pub fn get_decimal(&self, idx: usize) -> &Decimal {
match self[idx].as_ref().unwrap() {
ScalarImpl::Decimal(d) => d,
_ => unreachable!("type is not NaiveDateTime at index: {}", idx),
}
}
}

impl EstimateSize for OwnedRow {
fn estimated_heap_size(&self) -> usize {
// FIXME(bugen): this is not accurate now as the heap size of some `Scalar` is not counted.
Expand Down
61 changes: 50 additions & 11 deletions src/java_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use once_cell::sync::OnceCell;
use prost::{DecodeError, Message};
use risingwave_common::array::{ArrayError, StreamChunk};
use risingwave_common::hash::VirtualNode;
use risingwave_common::row::OwnedRow;
use risingwave_common::row::{OwnedRow, Row};
use risingwave_storage::error::StorageError;
use thiserror::Error;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -388,7 +388,7 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowIsNull<'a>(
idx: jint,
) -> jboolean {
execute_and_catch(env, move || {
Ok(pointer.as_ref().is_null(idx as usize) as jboolean)
Ok(pointer.as_ref().datum_at(idx as usize).is_none() as jboolean)
})
}

Expand All @@ -398,7 +398,13 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetInt16Value
pointer: Pointer<'a, JavaBindingRow>,
idx: jint,
) -> jshort {
execute_and_catch(env, move || Ok(pointer.as_ref().get_int16(idx as usize)))
execute_and_catch(env, move || {
Ok(pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_int16())
})
}

#[no_mangle]
Expand All @@ -407,7 +413,13 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetInt32Value
pointer: Pointer<'a, JavaBindingRow>,
idx: jint,
) -> jint {
execute_and_catch(env, move || Ok(pointer.as_ref().get_int32(idx as usize)))
execute_and_catch(env, move || {
Ok(pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_int32())
})
}

#[no_mangle]
Expand All @@ -416,7 +428,13 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetInt64Value
pointer: Pointer<'a, JavaBindingRow>,
idx: jint,
) -> jlong {
execute_and_catch(env, move || Ok(pointer.as_ref().get_int64(idx as usize)))
execute_and_catch(env, move || {
Ok(pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_int64())
})
}

#[no_mangle]
Expand All @@ -425,7 +443,14 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetFloatValue
pointer: Pointer<'a, JavaBindingRow>,
idx: jint,
) -> jfloat {
execute_and_catch(env, move || Ok(pointer.as_ref().get_f32(idx as usize)))
execute_and_catch(env, move || {
Ok(pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_float32()
.into())
})
}

#[no_mangle]
Expand All @@ -434,7 +459,14 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetDoubleValu
pointer: Pointer<'a, JavaBindingRow>,
idx: jint,
) -> jdouble {
execute_and_catch(env, move || Ok(pointer.as_ref().get_f64(idx as usize)))
execute_and_catch(env, move || {
Ok(pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_float64()
.into())
})
}

#[no_mangle]
Expand All @@ -444,7 +476,7 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetBooleanVal
idx: jint,
) -> jboolean {
execute_and_catch(env, move || {
Ok(pointer.as_ref().get_bool(idx as usize) as jboolean)
Ok(pointer.as_ref().datum_at(idx as usize).unwrap().into_bool() as jboolean)
})
}

Expand All @@ -455,7 +487,7 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetStringValu
idx: jint,
) -> JString<'a> {
execute_and_catch(env, move || {
Ok(env.new_string(pointer.as_ref().get_utf8(idx as usize))?)
Ok(env.new_string(pointer.as_ref().datum_at(idx as usize).unwrap().into_utf8())?)
})
}

Expand All @@ -468,7 +500,9 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetTimestampV
execute_and_catch(env, move || {
let millis = pointer
.as_ref()
.get_datetime(idx as usize)
.datum_at(idx as usize)
.unwrap()
.into_timestamp()
.0
.timestamp_millis();
let (ts_class_ref, constructor) = pointer
Expand All @@ -494,7 +528,12 @@ pub extern "system" fn Java_com_risingwave_java_binding_Binding_rowGetDecimalVal
idx: jint,
) -> JObject<'a> {
execute_and_catch(env, move || {
let value = pointer.as_ref().get_decimal(idx as usize).to_string();
let value = pointer
.as_ref()
.datum_at(idx as usize)
.unwrap()
.into_decimal()
.to_string();
let string_value = env.new_string(value)?;
let (decimal_class_ref, constructor) = pointer
.as_ref()
Expand Down

0 comments on commit de6c5a3

Please sign in to comment.