|
42 | 42 |
|
43 | 43 | if TYPE_CHECKING: |
44 | 44 | from datafusion.context import SessionContext |
45 | | - |
46 | 45 | __all__ = [ |
47 | 46 | "abs", |
48 | 47 | "acos", |
|
268 | 267 | "sum", |
269 | 268 | "tan", |
270 | 269 | "tanh", |
| 270 | + "to_char", |
| 271 | + "to_date", |
271 | 272 | "to_hex", |
| 273 | + "to_local_time", |
| 274 | + "to_time", |
272 | 275 | "to_timestamp", |
273 | 276 | "to_timestamp_micros", |
274 | 277 | "to_timestamp_millis", |
275 | 278 | "to_timestamp_nanos", |
276 | 279 | "to_timestamp_seconds", |
277 | 280 | "to_unixtime", |
| 281 | + "today", |
278 | 282 | "translate", |
279 | 283 | "trim", |
280 | 284 | "trunc", |
@@ -1010,67 +1014,111 @@ def now() -> Expr: |
1010 | 1014 | return Expr(f.now()) |
1011 | 1015 |
|
1012 | 1016 |
|
| 1017 | +def to_char(arg: Expr, formatter: Expr) -> Expr: |
| 1018 | + """Returns a string representation of a date, time, timestamp or duration. |
| 1019 | +
|
| 1020 | + For usage of ``formatter`` see the rust chrono package ``strftime`` package. |
| 1021 | +
|
| 1022 | + [Documentation here.](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) |
| 1023 | + """ |
| 1024 | + return Expr(f.to_char(arg.expr, formatter.expr)) |
| 1025 | + |
| 1026 | + |
| 1027 | +def _unwrap_exprs(args: tuple[Expr, ...]) -> list: |
| 1028 | + return [arg.expr for arg in args] |
| 1029 | + |
| 1030 | + |
| 1031 | +def to_date(arg: Expr, *formatters: Expr) -> Expr: |
| 1032 | + """Converts a value to a date (YYYY-MM-DD). |
| 1033 | +
|
| 1034 | + Supports strings, numeric and timestamp types as input. |
| 1035 | + Integers and doubles are interpreted as days since the unix epoch. |
| 1036 | + Strings are parsed as YYYY-MM-DD (e.g. '2023-07-20') |
| 1037 | + if ``formatters`` are not provided. |
| 1038 | +
|
| 1039 | + For usage of ``formatters`` see the rust chrono package ``strftime`` package. |
| 1040 | +
|
| 1041 | + [Documentation here.](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) |
| 1042 | + """ |
| 1043 | + return Expr(f.to_date(arg.expr, *_unwrap_exprs(formatters))) |
| 1044 | + |
| 1045 | + |
| 1046 | +def to_local_time(*args: Expr) -> Expr: |
| 1047 | + """Converts a timestamp with a timezone to a timestamp without a timezone. |
| 1048 | +
|
| 1049 | + This function handles daylight saving time changes. |
| 1050 | + """ |
| 1051 | + return Expr(f.to_local_time(*_unwrap_exprs(args))) |
| 1052 | + |
| 1053 | + |
| 1054 | +def to_time(arg: Expr, *formatters: Expr) -> Expr: |
| 1055 | + """Converts a value to a time. Supports strings and timestamps as input. |
| 1056 | +
|
| 1057 | + If ``formatters`` is not provided strings are parsed as HH:MM:SS, HH:MM or |
| 1058 | + HH:MM:SS.nnnnnnnnn; |
| 1059 | +
|
| 1060 | + For usage of ``formatters`` see the rust chrono package ``strftime`` package. |
| 1061 | +
|
| 1062 | + [Documentation here.](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) |
| 1063 | + """ |
| 1064 | + return Expr(f.to_time(arg.expr, *_unwrap_exprs(formatters))) |
| 1065 | + |
| 1066 | + |
1013 | 1067 | def to_timestamp(arg: Expr, *formatters: Expr) -> Expr: |
1014 | 1068 | """Converts a string and optional formats to a ``Timestamp`` in nanoseconds. |
1015 | 1069 |
|
1016 | 1070 | For usage of ``formatters`` see the rust chrono package ``strftime`` package. |
1017 | 1071 |
|
1018 | 1072 | [Documentation here.](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) |
1019 | 1073 | """ |
1020 | | - if formatters is None: |
1021 | | - return f.to_timestamp(arg.expr) |
1022 | | - |
1023 | | - formatters = [f.expr for f in formatters] |
1024 | | - return Expr(f.to_timestamp(arg.expr, *formatters)) |
| 1074 | + return Expr(f.to_timestamp(arg.expr, *_unwrap_exprs(formatters))) |
1025 | 1075 |
|
1026 | 1076 |
|
1027 | 1077 | def to_timestamp_millis(arg: Expr, *formatters: Expr) -> Expr: |
1028 | 1078 | """Converts a string and optional formats to a ``Timestamp`` in milliseconds. |
1029 | 1079 |
|
1030 | 1080 | See :py:func:`to_timestamp` for a description on how to use formatters. |
1031 | 1081 | """ |
1032 | | - formatters = [f.expr for f in formatters] |
1033 | | - return Expr(f.to_timestamp_millis(arg.expr, *formatters)) |
| 1082 | + return Expr(f.to_timestamp_millis(arg.expr, *_unwrap_exprs(formatters))) |
1034 | 1083 |
|
1035 | 1084 |
|
1036 | 1085 | def to_timestamp_micros(arg: Expr, *formatters: Expr) -> Expr: |
1037 | 1086 | """Converts a string and optional formats to a ``Timestamp`` in microseconds. |
1038 | 1087 |
|
1039 | 1088 | See :py:func:`to_timestamp` for a description on how to use formatters. |
1040 | 1089 | """ |
1041 | | - formatters = [f.expr for f in formatters] |
1042 | | - return Expr(f.to_timestamp_micros(arg.expr, *formatters)) |
| 1090 | + return Expr(f.to_timestamp_micros(arg.expr, *_unwrap_exprs(formatters))) |
1043 | 1091 |
|
1044 | 1092 |
|
1045 | 1093 | def to_timestamp_nanos(arg: Expr, *formatters: Expr) -> Expr: |
1046 | 1094 | """Converts a string and optional formats to a ``Timestamp`` in nanoseconds. |
1047 | 1095 |
|
1048 | 1096 | See :py:func:`to_timestamp` for a description on how to use formatters. |
1049 | 1097 | """ |
1050 | | - formatters = [f.expr for f in formatters] |
1051 | | - return Expr(f.to_timestamp_nanos(arg.expr, *formatters)) |
| 1098 | + return Expr(f.to_timestamp_nanos(arg.expr, *_unwrap_exprs(formatters))) |
1052 | 1099 |
|
1053 | 1100 |
|
1054 | 1101 | def to_timestamp_seconds(arg: Expr, *formatters: Expr) -> Expr: |
1055 | 1102 | """Converts a string and optional formats to a ``Timestamp`` in seconds. |
1056 | 1103 |
|
1057 | 1104 | See :py:func:`to_timestamp` for a description on how to use formatters. |
1058 | 1105 | """ |
1059 | | - formatters = [f.expr for f in formatters] |
1060 | | - return Expr(f.to_timestamp_seconds(arg.expr, *formatters)) |
| 1106 | + return Expr(f.to_timestamp_seconds(arg.expr, *_unwrap_exprs(formatters))) |
1061 | 1107 |
|
1062 | 1108 |
|
1063 | 1109 | def to_unixtime(string: Expr, *format_arguments: Expr) -> Expr: |
1064 | 1110 | """Converts a string and optional formats to a Unixtime.""" |
1065 | | - args = [f.expr for f in format_arguments] |
1066 | | - return Expr(f.to_unixtime(string.expr, *args)) |
| 1111 | + return Expr(f.to_unixtime(string.expr, *_unwrap_exprs(format_arguments))) |
1067 | 1112 |
|
1068 | 1113 |
|
1069 | 1114 | def current_date() -> Expr: |
1070 | 1115 | """Returns current UTC date as a Date32 value.""" |
1071 | 1116 | return Expr(f.current_date()) |
1072 | 1117 |
|
1073 | 1118 |
|
| 1119 | +today = current_date |
| 1120 | + |
| 1121 | + |
1074 | 1122 | def current_time() -> Expr: |
1075 | 1123 | """Returns current UTC time as a Time64 value.""" |
1076 | 1124 | return Expr(f.current_time()) |
|
0 commit comments