Skip to content

Commit

Permalink
[FEAT]: SQL temporal functions (#2858)
Browse files Browse the repository at this point in the history
  • Loading branch information
universalmind303 authored Sep 20, 2024
1 parent 48a123a commit c5b7062
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 654 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions daft/daft/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,6 @@ class PyExpr:
def __repr__(self) -> str: ...
def __hash__(self) -> int: ...
def __reduce__(self) -> tuple: ...
def dt_date(self) -> PyExpr: ...
def dt_day(self) -> PyExpr: ...
def dt_hour(self) -> PyExpr: ...
def dt_minute(self) -> PyExpr: ...
def dt_second(self) -> PyExpr: ...
def dt_time(self) -> PyExpr: ...
def dt_month(self) -> PyExpr: ...
def dt_year(self) -> PyExpr: ...
def dt_day_of_week(self) -> PyExpr: ...
def dt_truncate(self, interval: str, relative_to: PyExpr) -> PyExpr: ...
def utf8_endswith(self, pattern: PyExpr) -> PyExpr: ...
def utf8_startswith(self, pattern: PyExpr) -> PyExpr: ...
def utf8_contains(self, pattern: PyExpr) -> PyExpr: ...
Expand Down Expand Up @@ -1251,6 +1241,20 @@ def fill_nan(expr: PyExpr, fill_value: PyExpr) -> PyExpr: ...
# ---
def json_query(expr: PyExpr, query: str) -> PyExpr: ...

# ---
# expr.dt namespace
# ---
def dt_date(expr: PyExpr) -> PyExpr: ...
def dt_day(expr: PyExpr) -> PyExpr: ...
def dt_hour(expr: PyExpr) -> PyExpr: ...
def dt_minute(expr: PyExpr) -> PyExpr: ...
def dt_second(expr: PyExpr) -> PyExpr: ...
def dt_time(expr: PyExpr) -> PyExpr: ...
def dt_month(expr: PyExpr) -> PyExpr: ...
def dt_year(expr: PyExpr) -> PyExpr: ...
def dt_day_of_week(expr: PyExpr) -> PyExpr: ...
def dt_truncate(expr: PyExpr, interval: str, relative_to: PyExpr) -> PyExpr: ...

# ---
# expr.list namespace
# ---
Expand Down
20 changes: 10 additions & 10 deletions daft/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ def date(self) -> Expression:
Returns:
Expression: a Date expression
"""
return Expression._from_pyexpr(self._expr.dt_date())
return Expression._from_pyexpr(native.dt_date(self._expr))

def day(self) -> Expression:
"""Retrieves the day for a datetime column
Expand Down Expand Up @@ -1526,7 +1526,7 @@ def day(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the day extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_day())
return Expression._from_pyexpr(native.dt_day(self._expr))

def hour(self) -> Expression:
"""Retrieves the day for a datetime column
Expand Down Expand Up @@ -1561,7 +1561,7 @@ def hour(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the day extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_hour())
return Expression._from_pyexpr(native.dt_hour(self._expr))

def minute(self) -> Expression:
"""Retrieves the minute for a datetime column
Expand Down Expand Up @@ -1596,7 +1596,7 @@ def minute(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the minute extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_minute())
return Expression._from_pyexpr(native.dt_minute(self._expr))

def second(self) -> Expression:
"""Retrieves the second for a datetime column
Expand Down Expand Up @@ -1631,7 +1631,7 @@ def second(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the second extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_second())
return Expression._from_pyexpr(native.dt_second(self._expr))

def time(self) -> Expression:
"""Retrieves the time for a datetime column
Expand Down Expand Up @@ -1666,7 +1666,7 @@ def time(self) -> Expression:
Returns:
Expression: a Time expression
"""
return Expression._from_pyexpr(self._expr.dt_time())
return Expression._from_pyexpr(native.dt_time(self._expr))

def month(self) -> Expression:
"""Retrieves the month for a datetime column
Expand Down Expand Up @@ -1699,7 +1699,7 @@ def month(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the month extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_month())
return Expression._from_pyexpr(native.dt_month(self._expr))

def year(self) -> Expression:
"""Retrieves the year for a datetime column
Expand Down Expand Up @@ -1733,7 +1733,7 @@ def year(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the year extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_year())
return Expression._from_pyexpr(native.dt_year(self._expr))

def day_of_week(self) -> Expression:
"""Retrieves the day of the week for a datetime column, starting at 0 for Monday and ending at 6 for Sunday
Expand Down Expand Up @@ -1766,7 +1766,7 @@ def day_of_week(self) -> Expression:
Returns:
Expression: a UInt32 expression with just the day_of_week extracted from a datetime column
"""
return Expression._from_pyexpr(self._expr.dt_day_of_week())
return Expression._from_pyexpr(native.dt_day_of_week(self._expr))

def truncate(self, interval: str, relative_to: Expression | None = None) -> Expression:
"""Truncates the datetime column to the specified interval
Expand Down Expand Up @@ -1804,7 +1804,7 @@ def truncate(self, interval: str, relative_to: Expression | None = None) -> Expr
Expression: a DateTime expression truncated to the specified interval
"""
relative_to = Expression._to_expression(relative_to)
return Expression._from_pyexpr(self._expr.dt_truncate(interval, relative_to._expr))
return Expression._from_pyexpr(native.dt_truncate(self._expr, interval, relative_to._expr))


class ExpressionStringNamespace(ExpressionNamespace):
Expand Down
5 changes: 1 addition & 4 deletions src/daft-dsl/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod partitioning;
pub mod scalar;
pub mod sketch;
pub mod struct_;
pub mod temporal;
pub mod utf8;

use std::{
Expand All @@ -19,7 +18,7 @@ use serde::{Deserialize, Serialize};

use self::{
map::MapExpr, numeric::NumericExpr, partitioning::PartitioningExpr, sketch::SketchExpr,
struct_::StructExpr, temporal::TemporalExpr, utf8::Utf8Expr,
struct_::StructExpr, utf8::Utf8Expr,
};
use crate::{Expr, ExprRef, Operator};

Expand All @@ -30,7 +29,6 @@ use python::PythonUDF;
pub enum FunctionExpr {
Numeric(NumericExpr),
Utf8(Utf8Expr),
Temporal(TemporalExpr),
Map(MapExpr),
Sketch(SketchExpr),
Struct(StructExpr),
Expand All @@ -56,7 +54,6 @@ impl FunctionExpr {
match self {
Numeric(expr) => expr.get_evaluator(),
Utf8(expr) => expr.get_evaluator(),
Temporal(expr) => expr.get_evaluator(),
Map(expr) => expr.get_evaluator(),
Sketch(expr) => expr.get_evaluator(),
Struct(expr) => expr.get_evaluator(),
Expand Down
42 changes: 0 additions & 42 deletions src/daft-dsl/src/functions/temporal/date.rs

This file was deleted.

42 changes: 0 additions & 42 deletions src/daft-dsl/src/functions/temporal/day.rs

This file was deleted.

42 changes: 0 additions & 42 deletions src/daft-dsl/src/functions/temporal/day_of_week.rs

This file was deleted.

42 changes: 0 additions & 42 deletions src/daft-dsl/src/functions/temporal/hour.rs

This file was deleted.

42 changes: 0 additions & 42 deletions src/daft-dsl/src/functions/temporal/minute.rs

This file was deleted.

Loading

0 comments on commit c5b7062

Please sign in to comment.