Skip to content

Commit

Permalink
Search default window functions if no session context was provided (#963
Browse files Browse the repository at this point in the history
)

* Search default window functions if no session context was provided

* Check if value is None because [] don't trigger the intended behavior
  • Loading branch information
timsaucer authored and andygrove committed Dec 4, 2024
1 parent 09b2f49 commit d8199d3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions python/datafusion/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ def join(
left_on = join_keys[0]
right_on = join_keys[1]

if on:
if left_on or right_on:
if on is not None:
if left_on is not None or right_on is not None:
raise ValueError(
"`left_on` or `right_on` should not provided with `on`"
)
left_on = on
right_on = on
elif left_on or right_on:
elif left_on is not None or right_on is not None:
if left_on is None or right_on is None:
raise ValueError("`left_on` and `right_on` should both be provided.")
else:
Expand Down
1 change: 1 addition & 0 deletions python/datafusion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def window(
partition_by = expr_list_to_raw_expr_list(partition_by)
order_by_raw = sort_list_to_raw_sort_list(order_by)
window_frame = window_frame.window_frame if window_frame is not None else None
ctx = ctx.ctx if ctx is not None else None
return Expr(f.window(name, args, partition_by, order_by_raw, window_frame, ctx))


Expand Down
11 changes: 11 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use datafusion::functions_aggregate::all_default_aggregate_functions;
use datafusion::functions_window::all_default_window_functions;
use datafusion::logical_expr::ExprFunctionExt;
use datafusion::logical_expr::WindowFrame;
use pyo3::{prelude::*, wrap_pyfunction};
Expand Down Expand Up @@ -282,6 +283,16 @@ fn find_window_fn(name: &str, ctx: Option<PySessionContext>) -> PyResult<WindowF
return Ok(agg_fn);
}

// search default window functions
let window_fn = all_default_window_functions()
.iter()
.find(|v| v.name() == name || v.aliases().contains(&name.to_string()))
.map(|f| WindowFunctionDefinition::WindowUDF(f.clone()));

if let Some(window_fn) = window_fn {
return Ok(window_fn);
}

Err(DataFusionError::Common(format!("window function `{name}` not found")).into())
}

Expand Down

0 comments on commit d8199d3

Please sign in to comment.