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

refactor(array): forward method impl on ListValue to ListRef #20057

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 51 additions & 47 deletions src/common/src/array/list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,35 +383,14 @@ impl ListValue {
Self::new(builder.finish())
}

/// Returns the length of the list.
pub fn len(&self) -> usize {
self.values.len()
}

/// Returns `true` if the list has a length of 0.
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}

/// Iterates over the elements of the list.
pub fn iter(&self) -> impl DoubleEndedIterator + ExactSizeIterator<Item = DatumRef<'_>> {
self.values.iter()
}

/// Get the element at the given index. Returns `None` if the index is out of bounds.
pub fn get(&self, index: usize) -> Option<DatumRef<'_>> {
if index < self.len() {
Some(self.values.value_at(index))
} else {
None
/// Returns a mutable slice if the list is of type `int64[]`.
pub fn as_i64_mut_slice(&mut self) -> Option<&mut [i64]> {
match self.values.as_mut() {
ArrayImpl::Int64(array) => Some(array.as_mut_slice()),
_ => None,
}
}

/// Returns the data type of the elements in the list.
pub fn data_type(&self) -> DataType {
self.values.data_type()
}

pub fn memcmp_deserialize(
item_datatype: &DataType,
deserializer: &mut memcomparable::Deserializer<impl Buf>,
Expand All @@ -427,29 +406,38 @@ impl ListValue {
}
Ok(Self::new(builder.finish()))
}
}

// Used to display ListValue in explain for better readibilty.
pub fn display_for_explain(&self) -> String {
// Example of ListValue display: ARRAY[1, 2, null]
format!(
"ARRAY[{}]",
self.iter()
.map(|v| {
match v.as_ref() {
None => "null".into(),
Some(scalar) => scalar.to_text(),
}
})
.format(", ")
)
// Delegate to `ListRef` for these methods.
impl ListValue {
/// Returns the length of the list.
pub fn len(&self) -> usize {
self.as_scalar_ref().len()
}

/// Returns a mutable slice if the list is of type `int64[]`.
pub fn as_i64_mut_slice(&mut self) -> Option<&mut [i64]> {
match self.values.as_mut() {
ArrayImpl::Int64(array) => Some(array.as_mut_slice()),
_ => None,
}
/// Returns `true` if the list has a length of 0.
pub fn is_empty(&self) -> bool {
self.as_scalar_ref().is_empty()
}

/// Iterates over the elements of the list.
pub fn iter(&self) -> impl DoubleEndedIterator + ExactSizeIterator<Item = DatumRef<'_>> {
self.as_scalar_ref().iter()
}

/// Get the element at the given index. Returns `None` if the index is out of bounds.
pub fn get(&self, index: usize) -> Option<DatumRef<'_>> {
self.as_scalar_ref().get(index)
}

/// Returns the data type of the elements in the list.
pub fn data_type(&self) -> DataType {
self.as_scalar_ref().data_type()
}

/// Used to display `ListValue` in explain for better readibilty.
pub fn display_for_explain(&self) -> String {
self.as_scalar_ref().display_for_explain()
}
}

Expand Down Expand Up @@ -615,6 +603,22 @@ impl<'a> ListRef<'a> {
},
)
}

/// Used to display `ListRef` in explain for better readibilty.
pub fn display_for_explain(self) -> String {
// Example: `ARRAY[1, 2, null]`
format!(
"ARRAY[{}]",
self.iter()
.map(|v| {
match v.as_ref() {
None => "null".into(),
Some(scalar) => scalar.to_text(),
}
})
.format(", ")
)
}
}

impl PartialEq for ListRef<'_> {
Expand Down Expand Up @@ -712,7 +716,7 @@ impl<'a> From<&'a ListValue> for ListRef<'a> {
ListRef {
array: &value.values,
start: 0,
end: value.len() as u32,
end: value.values.len() as u32,
}
}
}
Expand Down
Loading