alamb commented on code in PR #18390:
URL: https://github.com/apache/datafusion/pull/18390#discussion_r2479375217


##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -47,6 +47,71 @@ use chrono::{
     DateTime, Datelike, Duration, LocalResult, NaiveDateTime, Offset, 
TimeDelta, Timelike,
 };
 
+/// Represents the granularity for date truncation operations
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum DateTruncGranularity {
+    Microsecond,
+    Millisecond,
+    Second,
+    Minute,
+    Hour,
+    Day,
+    Week,
+    Month,
+    Quarter,
+    Year,
+}
+
+impl DateTruncGranularity {
+    /// Mapping of string representations to enum variants
+    const GRANULARITY_MAP: &[(&str, Self)] = &[
+        ("microsecond", Self::Microsecond),
+        ("millisecond", Self::Millisecond),
+        ("second", Self::Second),
+        ("minute", Self::Minute),
+        ("hour", Self::Hour),
+        ("day", Self::Day),
+        ("week", Self::Week),
+        ("month", Self::Month),
+        ("quarter", Self::Quarter),
+        ("year", Self::Year),
+    ];
+
+    /// Parse a granularity string into a DateTruncGranularity enum
+    fn from_str(s: &str) -> Result<Self> {
+        let s_lower = s.to_lowercase();
+        Self::GRANULARITY_MAP
+            .iter()
+            .find(|(key, _)| *key == s_lower.as_str())

Review Comment:
   Perhaps we can make this faster via 
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.binary_search?
   
   Or maybe just hard coding the strings so the compiler can make a jump table



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -47,6 +47,71 @@ use chrono::{
     DateTime, Datelike, Duration, LocalResult, NaiveDateTime, Offset, 
TimeDelta, Timelike,
 };
 
+/// Represents the granularity for date truncation operations
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum DateTruncGranularity {
+    Microsecond,
+    Millisecond,
+    Second,
+    Minute,
+    Hour,
+    Day,
+    Week,
+    Month,
+    Quarter,
+    Year,
+}
+
+impl DateTruncGranularity {
+    /// Mapping of string representations to enum variants
+    const GRANULARITY_MAP: &[(&str, Self)] = &[
+        ("microsecond", Self::Microsecond),
+        ("millisecond", Self::Millisecond),
+        ("second", Self::Second),
+        ("minute", Self::Minute),
+        ("hour", Self::Hour),
+        ("day", Self::Day),
+        ("week", Self::Week),
+        ("month", Self::Month),
+        ("quarter", Self::Quarter),
+        ("year", Self::Year),
+    ];
+
+    /// Parse a granularity string into a DateTruncGranularity enum
+    fn from_str(s: &str) -> Result<Self> {
+        let s_lower = s.to_lowercase();
+        Self::GRANULARITY_MAP
+            .iter()
+            .find(|(key, _)| *key == s_lower.as_str())
+            .map(|(_, value)| *value)
+            .ok_or_else(|| {
+                let supported = Self::GRANULARITY_MAP
+                    .iter()
+                    .map(|(key, _)| *key)
+                    .collect::<Vec<_>>()
+                    .join(", ");
+                exec_datafusion_err!(

Review Comment:
   Can you please add a `slt` test for this error?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to