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

Inconsistency between code and comment #19743

Closed
YichiZhang0613 opened this issue Nov 12, 2024 · 2 comments · Fixed by #19810
Closed

Inconsistency between code and comment #19743

YichiZhang0613 opened this issue Nov 12, 2024 · 2 comments · Fixed by #19810

Comments

@YichiZhang0613
Copy link
Contributor

In polars/crates/polars-core/src/chunked_array/object/mod.rs, the comment is "panics iff values.len() != self.len()" while the code check "panics iff validity.len() != self.len()"

/// Sets the validity of this array.
/// # Panics
/// This function panics iff `values.len() != self.len()`.
#[inline]
pub fn set_validity(&mut self, validity: Option<Bitmap>) {
    if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
        panic!("validity must be equal to the array's length")
    }
    self.null_bitmap = validity;
}

In polars/crates/polars-arrow/src/array/primitive/mutable.rs, the comment is "Panics iff the values' length is not equal to the existing validity's len" while the code check "Panics iff the values' length is not equal to the existing value's len"

/// Sets values.
    /// # Panic
    /// Panics iff the values' length is not equal to the existing validity's len.
    pub fn set_values(&mut self, values: Vec<T>) {
        assert_eq!(values.len(), self.values.len());
        self.values = values;
    }
}

In polars/crates/polars-parquet/src/parquet/encoding/uleb128.rs, the code wouldn't panic when container.len() < 10.

/// # Panic
/// This function may panic if `container.len() < 10` and `value` requires more bytes.
pub fn encode(mut value: u64, container: &mut [u8]) -> usize {
    let mut consumed = 0;
    let mut iter = container.iter_mut();
    loop {
        let mut byte = (value as u8) & !128;
        value >>= 7;
        if value != 0 {
            byte |= 128;
        }
        *iter.next().unwrap() = byte;
        consumed += 1;
        if value == 0 {
            break;
        }
    }
    consumed
}

if I changed test below

#[test]
    fn round_trip() {
        let original = 123124234u64;
        let mut container = [0u8; 10];
        let encoded_len = encode(original, &mut container);
        let (value, len) = decode(&container).unwrap();
        assert_eq!(value, original);
        assert_eq!(len, encoded_len);
    }

to

#[test]
   fn round_trip() {
        let original = 123124234u64;
        let mut container = [0u8; 9];
        let encoded_len = encode(original, &mut container);
        let (value, len) = decode(&container).unwrap();
        assert_eq!(value, original);
        assert_eq!(len, encoded_len);
    }

and run, it still works instead of panic.
In polars/crates/polars-parquet/src/parquet/encoding/mod.rs/get_length, the code wouldn't panic when values.len() < 4.

/// # Panics
/// This function panics iff `values.len() < 4`.
#[inline]
pub fn get_length(values: &[u8]) -> Option<usize> {
    values
        .get(0..4)
        .map(|x| u32::from_le_bytes(x.try_into().unwrap()) as usize)
}

As the same, If I add a test

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test(){
        let values = &[1, 2, 3];
        let res = get_length(values);
        println!("res: {:#?}",res);
    }
}

below polars/crates/polars-parquet/src/parquet/encoding/mod.rs/get_length, it will not panic as well,
which are inconsistent with comment.

@ritchie46
Copy link
Member

Could you maybe just open a PR and fix the comments. This is private code and isn't a priority for us. Having many of these super small issues clutters our issue board.

@YichiZhang0613
Copy link
Contributor Author

Could you maybe just open a PR and fix the comments. This is private code and isn't a priority for us. Having many of these super small issues clutters our issue board.

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants