You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "doki23 (via GitHub)" <gi...@apache.org> on 2023/03/09 04:26:52 UTC

[GitHub] [arrow-rs] doki23 commented on a diff in pull request #3801: Faster timestamp parsing (~70-90% faster)

doki23 commented on code in PR #3801:
URL: https://github.com/apache/arrow-rs/pull/3801#discussion_r1130411077


##########
arrow-cast/src/parse.rs:
##########
@@ -15,11 +15,117 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use arrow_array::timezone::Tz;
 use arrow_array::types::*;
 use arrow_array::ArrowPrimitiveType;
 use arrow_schema::ArrowError;
 use chrono::prelude::*;
 
+/// Helper for parsing timestamps
+struct TimestampParser {
+    digits: [u8; 32],
+    mask: u32,
+}
+
+impl TimestampParser {
+    fn new(bytes: &[u8]) -> Self {
+        let mut digits = [0; 32];
+        let mut mask = 0;
+
+        for (idx, (o, i)) in digits.iter_mut().zip(bytes).enumerate() {
+            *o = i.wrapping_sub(b'0');
+            mask |= ((*o < 10) as u32) << idx
+        }
+
+        Self { digits, mask }
+    }
+
+    /// Returns true if the byte at `idx` equals `b`
+    fn test(&self, idx: usize, b: u8) -> bool {
+        self.digits[idx] == b.wrapping_sub(b'0')
+    }
+
+    /// Parses a date of the form `1997-01-31`
+    fn date(&self) -> Option<NaiveDate> {
+        if self.mask & 0b1111111111 != 0b1101101111
+            || !self.test(4, b'-')
+            || !self.test(7, b'-')
+        {
+            return None;
+        }
+
+        let year = self.digits[0] as u16 * 1000
+            + self.digits[1] as u16 * 100
+            + self.digits[2] as u16 * 10
+            + self.digits[3] as u16;
+
+        let month = self.digits[5] * 10 + self.digits[6];
+        let day = self.digits[8] * 10 + self.digits[9];
+
+        NaiveDate::from_ymd_opt(year as _, month as _, day as _)
+    }
+
+    /// Parses a time of any of forms
+    /// - `09:26:56`
+    /// - `09:26:56.123`
+    /// - `09:26:56.123456`
+    /// - `09:26:56.123456789`
+    /// - `092656`
+    ///
+    /// Returning the end byte offset
+    fn time(&self) -> Option<(NaiveTime, usize)> {
+        match (self.mask >> 11) & 0b11111111 {
+            // 09:26:56
+            0b11011011 if self.test(13, b':') && self.test(16, b':') => {
+                let hour = self.digits[11] * 10 + self.digits[12];
+                let minute = self.digits[14] * 10 + self.digits[15];
+                let second = self.digits[17] * 10 + self.digits[18];
+                let time = NaiveTime::from_hms_opt(hour as _, minute as _, second as _)?;
+
+                let millis = || {
+                    self.digits[20] as u32 * 100_000_000
+                        + self.digits[21] as u32 * 10_000_000
+                        + self.digits[22] as u32 * 1_000_000
+                };
+
+                let micros = || {
+                    self.digits[23] as u32 * 100_000
+                        + self.digits[24] as u32 * 10_000
+                        + self.digits[25] as u32 * 1_000
+                };
+
+                let nanos = || {
+                    self.digits[26] as u32 * 100
+                        + self.digits[27] as u32 * 10
+                        + self.digits[28] as u32
+                };
+
+                match self.test(19, b'.') {
+                    true => match (self.mask >> 20).trailing_ones() {
+                        3 => Some((time.with_nanosecond(millis())?, 23)),
+                        6 => Some((time.with_nanosecond(millis() + micros())?, 26)),
+                        9 => Some((
+                            time.with_nanosecond(millis() + micros() + nanos())?,
+                            29,
+                        )),
+                        _ => None,
+                    },
+                    false => Some((time, 19)),
+                }
+            }
+            // 09:26:56

Review Comment:
   a typo -- should be `// 092656`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org