Skip to content

Commit

Permalink
feat: remove init and validate rows in new()
Browse files Browse the repository at this point in the history
  • Loading branch information
evenyag committed Aug 12, 2023
1 parent 28e7333 commit 1e9f5bf
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/mito2/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ pub struct WriteRequest {
}

impl WriteRequest {
/// Returns a new request.
/// Creates a new request.
///
/// Returns `Err` if `rows` are invalid.
pub fn new(region_id: RegionId, op_type: OpType, rows: Rows) -> Result<WriteRequest> {
let mut name_to_index = HashMap::with_capacity(rows.schema.len());
for (index, column) in rows.schema.iter().enumerate() {
Expand All @@ -120,55 +122,44 @@ impl WriteRequest {
);
}

let has_null = vec![false; rows.schema.len()];
let mut request = WriteRequest {
region_id,
op_type,
rows,
name_to_index,
has_null,
};
request.init()?;

Ok(request)
}

/// Initializes and validates the request.
///
/// Ensures rows match the schema.
fn init(&mut self) -> Result<()> {
for row in &self.rows.rows {
let mut has_null = vec![false; rows.schema.len()];
for row in &rows.rows {
ensure!(
row.values.len() == self.rows.schema.len(),
row.values.len() == rows.schema.len(),
InvalidRequestSnafu {
region_id: self.region_id,
region_id: region_id,
reason: format!(
"row has {} columns but schema has {}",
row.values.len(),
self.rows.schema.len()
rows.schema.len()
),
}
);

for (i, (value, column_schema)) in row.values.iter().zip(&self.rows.schema).enumerate()
{
validate_proto_value(self.region_id, value, column_schema)?;
for (i, (value, column_schema)) in row.values.iter().zip(&rows.schema).enumerate() {
validate_proto_value(region_id, value, column_schema)?;

if value.value.is_none() {
self.has_null[i] = true;
has_null[i] = true;
}
}
}

Ok(())
Ok(WriteRequest {
region_id,
op_type,
rows,
name_to_index,
has_null,
})
}

/// Get column index by name.
pub(crate) fn column_index_by_name(&self, name: &str) -> Option<usize> {
self.name_to_index.get(name).copied()
}

/// Checks schema of rows.
/// Checks schema of rows is compatible with schema of the region.
///
/// If column with default value is missing, it returns a special [FillDefault](crate::error::Error::FillDefault)
/// error.
Expand Down

0 comments on commit 1e9f5bf

Please sign in to comment.