ovr commented on a change in pull request #9373:
URL: https://github.com/apache/arrow/pull/9373#discussion_r568170411



##########
File path: rust/datafusion/src/sql/planner.rs
##########
@@ -894,6 +908,131 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             ))),
         }
     }
+
+    fn sql_interval_to_literal(
+        &self,
+        value: &String,
+        leading_field: &Option<DateTimeField>,
+        leading_precision: &Option<u64>,
+        last_field: &Option<DateTimeField>,
+        fractional_seconds_precision: &Option<u64>,
+    ) -> Result<Expr> {
+        if leading_field.is_some() {
+            return Err(DataFusionError::NotImplemented(format!(
+                "Unsupported Interval Expression with leading_field {:?}",
+                leading_field
+            )));
+        }
+
+        if leading_precision.is_some() {
+            return Err(DataFusionError::NotImplemented(format!(
+                "Unsupported Interval Expression with leading_precision {:?}",
+                leading_precision
+            )));
+        }
+
+        if last_field.is_some() {
+            return Err(DataFusionError::NotImplemented(format!(
+                "Unsupported Interval Expression with last_field {:?}",
+                last_field
+            )));
+        }
+
+        if fractional_seconds_precision.is_some() {
+            return Err(DataFusionError::NotImplemented(format!(
+                "Unsupported Interval Expression with 
fractional_seconds_precision {:?}",
+                fractional_seconds_precision
+            )));
+        }
+
+        // Should we resort parts on overflow?
+        let resort_parts = |days: i32, seconds: f32| -> (i32, f32) { (days, 
seconds) };
+
+        let calculate_from_part =
+            |interval_period_str: &str, interval_type: &str| -> Result<(i32, 
f32)> {
+                let interval_period = match interval_period_str.parse::<f32>() 
{
+                    Ok(n) => n,
+                    Err(_) => {
+                        return Err(DataFusionError::SQL(ParserError(format!(
+                            "Unsupported Interval Expression with value {:?}",
+                            value
+                        ))))
+                    }
+                };
+
+                if interval_period > (i32::MAX as f32) {
+                    return Err(DataFusionError::NotImplemented(format!(
+                        "Interval field value out of range: {:?}",
+                        value
+                    )));
+                }
+
+                const SECONDS_PER_HOUR: f32 = 3_600_f32;
+                const MILLIS_PER_SECOND: f32 = 1_000_f32;
+
+                match interval_type.to_lowercase().as_str() {
+                    "year" => Ok(((interval_period * 365_f32) as i32, 0.0)),
+                    "month" => Ok(((interval_period * 30_f32) as i32, 0.0)),
+                    "day" | "days" => Ok((interval_period as i32, 0.0)),
+                    "hour" | "hours" => Ok(resort_parts(
+                        0,
+                        interval_period * SECONDS_PER_HOUR * MILLIS_PER_SECOND,
+                    )),
+                    "minutes" | "minute" => Ok(resort_parts(
+                        0,
+                        interval_period * 60_f32 * MILLIS_PER_SECOND,
+                    )),
+                    "seconds" | "second" => {
+                        Ok(resort_parts(0, interval_period * 
MILLIS_PER_SECOND))
+                    }
+                    "milliseconds" | "millisecond" => Ok((0, interval_period)),
+                    _ => {
+                        return Err(DataFusionError::NotImplemented(format!(
+                            "Invalid input syntax for type interval: {:?}",
+                            value
+                        )))
+                    }
+                }
+            };
+
+        let mut parts = value.split_whitespace();
+        let mut result_days: i64 = 0;
+        let mut result_millis: i64 = 0;
+
+        loop {

Review comment:
       Awesome, thank you! I will try.




----------------------------------------------------------------
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.

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


Reply via email to