Skip to content

Commit

Permalink
Get rid of duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
suharev7 committed Nov 11, 2023
1 parent b4e54a5 commit 1bffb32
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
20 changes: 6 additions & 14 deletions src/types/column/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use crate::{
errors::Result,
types::{
column::{
column_data::BoxColumnData, column_data::ColumnData, list::List,
nullable::NullableColumnData, BoxColumnWrapper, ColumnFrom, ColumnWrapper,
VectorColumnData,
column_data::{BoxColumnData, ColumnData},
list::List,
nullable::NullableColumnData,
util::extract_nulls_and_values,
BoxColumnWrapper, ColumnFrom, ColumnWrapper, VectorColumnData,
},
decimal::{Decimal, NoBits},
from_sql::FromSql,
Expand Down Expand Up @@ -263,17 +265,7 @@ impl<K: ColumnType> ColumnData for NullableDecimalAdapter<K> {
}

fn save(&self, encoder: &mut Encoder, start: usize, end: usize) {
let size = end - start;
let mut nulls = vec![0; size];
let mut values: Vec<Option<Decimal>> = vec![None; size];

for (i, index) in (start..end).enumerate() {
values[i] = Option::from_sql(self.at(index)).unwrap();
if values[i].is_none() {
nulls[i] = 1;
}
}

let (nulls, values) = extract_nulls_and_values::<Decimal, Self>(self, start, end).unwrap();
encoder.write_bytes(nulls.as_ref());

for value in values {
Expand Down
18 changes: 5 additions & 13 deletions src/types/column/fixed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::{
binary::{Encoder, ReadEx},
errors::Result,
types::{
column::column_data::{BoxColumnData, LowCardinalityAccessor},
from_sql::*,
column::{
column_data::{BoxColumnData, LowCardinalityAccessor},
util::extract_nulls_and_values,
},
value::get_str_buffer,
Column, ColumnType, SqlType, Value, ValueRef,
},
Expand Down Expand Up @@ -171,17 +173,7 @@ impl<K: ColumnType> ColumnData for NullableFixedStringAdapter<K> {
}

fn save(&self, encoder: &mut Encoder, start: usize, end: usize) {
let size = end - start;
let mut nulls = vec![0; size];
let mut values: Vec<Option<&[u8]>> = vec![None; size];

for (i, index) in (start..end).enumerate() {
values[i] = Option::from_sql(self.at(index)).unwrap();
if values[i].is_none() {
nulls[i] = 1;
}
}

let (nulls, values) = extract_nulls_and_values::<&[u8], Self>(self, start, end).unwrap();
encoder.write_bytes(nulls.as_ref());

let mut buffer = Vec::with_capacity(self.str_len);
Expand Down
1 change: 1 addition & 0 deletions src/types/column/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod numeric;
mod simple_agg_func;
mod string;
mod string_pool;
mod util;

/// Represents Clickhouse Column
pub struct Column<K: ColumnType> {
Expand Down
28 changes: 28 additions & 0 deletions src/types/column/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::{
types::{column::ColumnData, FromSql},
Result,
};

pub(crate) fn extract_nulls_and_values<'a, T, C>(
column_data: &'a C,
start: usize,
end: usize,
) -> Result<(Vec<u8>, Vec<Option<T>>)>
where
C: ColumnData,
T: FromSql<'a>,
{
let size = end - start;
let mut nulls = vec![0_u8; size];
let mut values: Vec<Option<T>> = Vec::with_capacity(size);

for (i, index) in (start..end).enumerate() {
let value = Option::from_sql(column_data.at(index))?;
if value.is_none() {
nulls[i] = 1;
}
values.push(value);
}

Ok((nulls, values))
}

0 comments on commit 1bffb32

Please sign in to comment.