Skip to content

Commit 113b099

Browse files
committed
chore(cubestore): Implement now(), unix_timestamp()
1 parent 78ec2a5 commit 113b099

File tree

1 file changed

+47
-11
lines changed
  • rust/cubestore/cubestore/src/queryplanner

1 file changed

+47
-11
lines changed

rust/cubestore/cubestore/src/queryplanner/udfs.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use serde_derive::{Deserialize, Serialize};
1818
use smallvec::smallvec;
1919
use smallvec::SmallVec;
2020
use std::sync::Arc;
21-
21+
use std::time::SystemTime;
2222
use super::udf_xirr::XirrAccumulator;
2323

2424
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
@@ -159,19 +159,37 @@ impl CubeScalarUDF for Now {
159159
}
160160

161161
fn descriptor(&self) -> ScalarUDF {
162-
return ScalarUDF {
162+
ScalarUDF {
163163
name: self.name().to_string(),
164164
signature: Self::signature(),
165165
return_type: Arc::new(|inputs| {
166166
assert!(inputs.is_empty());
167167
Ok(Arc::new(DataType::Timestamp(TimeUnit::Nanosecond, None)))
168168
}),
169169
fun: Arc::new(|_| {
170-
Err(DataFusionError::Internal(
171-
"NOW() was not optimized away".to_string(),
172-
))
170+
let t = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
171+
Ok(t) => t,
172+
Err(e) => {
173+
return Err(DataFusionError::Internal(format!(
174+
"Failed to get current timestamp: {}",
175+
e
176+
)))
177+
}
178+
};
179+
180+
let nanos = match i64::try_from(t.as_nanos()) {
181+
Ok(t) => t,
182+
Err(e) => {
183+
return Err(DataFusionError::Internal(format!(
184+
"Failed to convert timestamp to i64: {}",
185+
e
186+
)))
187+
}
188+
};
189+
190+
Ok(ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(nanos))))
173191
}),
174-
};
192+
}
175193
}
176194
}
177195

@@ -191,19 +209,37 @@ impl CubeScalarUDF for UnixTimestamp {
191209
}
192210

193211
fn descriptor(&self) -> ScalarUDF {
194-
return ScalarUDF {
212+
ScalarUDF {
195213
name: self.name().to_string(),
196214
signature: Self::signature(),
197215
return_type: Arc::new(|inputs| {
198216
assert!(inputs.is_empty());
199217
Ok(Arc::new(DataType::Int64))
200218
}),
201219
fun: Arc::new(|_| {
202-
Err(DataFusionError::Internal(
203-
"UNIX_TIMESTAMP() was not optimized away".to_string(),
204-
))
220+
let t = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
221+
Ok(t) => t,
222+
Err(e) => {
223+
return Err(DataFusionError::Internal(format!(
224+
"Failed to get current timestamp: {}",
225+
e
226+
)))
227+
}
228+
};
229+
230+
let seconds = match i64::try_from(t.as_secs()) {
231+
Ok(t) => t,
232+
Err(e) => {
233+
return Err(DataFusionError::Internal(format!(
234+
"Failed to convert timestamp to i64: {}",
235+
e
236+
)))
237+
}
238+
};
239+
240+
Ok(ColumnarValue::Scalar(ScalarValue::Int64(Some(seconds))))
205241
}),
206-
};
242+
}
207243
}
208244
}
209245

0 commit comments

Comments
 (0)