Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Pytest configuration for doctest namespace injection."""

import datafusion as dfn
import numpy as np
import pytest


@pytest.fixture(autouse=True)
def _doctest_namespace(doctest_namespace: dict) -> None:
"""Add common imports to the doctest namespace."""
doctest_namespace["dfn"] = dfn
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This auto imports dfn and np to save 2 lines in each example.

doctest_namespace["np"] = np
190 changes: 171 additions & 19 deletions python/datafusion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,28 @@ def abs(arg: Expr) -> Expr:
def acos(arg: Expr) -> Expr:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be helpful to extract shared example setup, or align the example style with existing docstrings.

Most of the new examples duplicate the same SessionContext / from_pydict / select / collect_column flow with only the function name and literal changing. That is fine for a few functions, but the repetition will get expensive as more scalar helpers gain examples.
It would be worth standardizing on a small doctest helper namespace or a more consistent public-facing import style so future additions do not copy-paste boilerplate across dozens of docstrings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is value in the examples being copy pastable/fully stand alone if it's only a few lines of boilerplate. For reference numpy https://numpy.org/doc/2.4/reference/generated/numpy.inner.html#numpy.inner.

Expensive computationally or for maintenance? This is inline with the style from the existing doc examples as far as I can tell https://github.com/rerun-io/datafusion-python/blob/231ed2b1d375fefe9aa01cdc8ae41c620c772f76/python/datafusion/dataframe.py#L324

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point and it is defensible that each example is self-contained.

Expensive computationally or for maintenance?

maintenance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is definitely a bit of maintenance trade off between the documentation value and pain if there is a large refactor that requires updating the docs examples.

I think there is value to the standalone but am open to opinions on this since I'm not a primary maintainer. My hope is that after the first large landing of these examples this is kind of just a small additional test surface so it's not more cumbersome than updating tests when api changes happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think given the rise of LLMs the maintenance is likely easier now

"""Returns the arc cosine or inverse cosine of a number.

Returns:
--------
Expr
A new expression representing the arc cosine of the input expression.
Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [1.0]})
>>> result = df.select(dfn.functions.acos(dfn.col("a")).alias("acos"))
>>> result.collect_column("acos")[0].as_py()
0.0
"""
return Expr(f.acos(arg.expr))


def acosh(arg: Expr) -> Expr:
"""Returns inverse hyperbolic cosine."""
"""Returns inverse hyperbolic cosine.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [1.0]})
>>> result = df.select(dfn.functions.acosh(dfn.col("a")).alias("acosh"))
>>> result.collect_column("acosh")[0].as_py()
0.0
"""
return Expr(f.acosh(arg.expr))


Expand All @@ -510,27 +522,73 @@ def ascii(arg: Expr) -> Expr:


def asin(arg: Expr) -> Expr:
"""Returns the arc sine or inverse sine of a number."""
"""Returns the arc sine or inverse sine of a number.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.asin(dfn.col("a")).alias("asin"))
>>> result.collect_column("asin")[0].as_py()
0.0
"""
return Expr(f.asin(arg.expr))


def asinh(arg: Expr) -> Expr:
"""Returns inverse hyperbolic sine."""
"""Returns inverse hyperbolic sine.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.asinh(dfn.col("a")).alias("asinh"))
>>> result.collect_column("asinh")[0].as_py()
0.0
"""
return Expr(f.asinh(arg.expr))


def atan(arg: Expr) -> Expr:
"""Returns inverse tangent of a number."""
"""Returns inverse tangent of a number.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.atan(dfn.col("a")).alias("atan"))
>>> result.collect_column("atan")[0].as_py()
0.0
"""
return Expr(f.atan(arg.expr))


def atanh(arg: Expr) -> Expr:
"""Returns inverse hyperbolic tangent."""
"""Returns inverse hyperbolic tangent.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.atanh(dfn.col("a")).alias("atanh"))
>>> result.collect_column("atanh")[0].as_py()
0.0
"""
return Expr(f.atanh(arg.expr))


def atan2(y: Expr, x: Expr) -> Expr:
"""Returns inverse tangent of a division given in the argument."""
"""Returns inverse tangent of a division given in the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"y": [0.0], "x": [1.0]})
>>> result = df.select(
... dfn.functions.atan2(dfn.col("y"), dfn.col("x")).alias("atan2"))
>>> result.collect_column("atan2")[0].as_py()
0.0
"""
return Expr(f.atan2(y.expr, x.expr))


Expand Down Expand Up @@ -581,22 +639,65 @@ def coalesce(*args: Expr) -> Expr:


def cos(arg: Expr) -> Expr:
"""Returns the cosine of the argument."""
"""Returns the cosine of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0,-1,1]})
>>> cos_df = df.select(dfn.functions.cos(dfn.col("a")).alias("cos"))
>>> cos_df.collect_column("cos")[0].as_py()
1.0
"""
return Expr(f.cos(arg.expr))


def cosh(arg: Expr) -> Expr:
"""Returns the hyperbolic cosine of the argument."""
"""Returns the hyperbolic cosine of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0,-1,1]})
>>> cosh_df = df.select(dfn.functions.cosh(dfn.col("a")).alias("cosh"))
>>> cosh_df.collect_column("cosh")[0].as_py()
1.0
"""
return Expr(f.cosh(arg.expr))


def cot(arg: Expr) -> Expr:
"""Returns the cotangent of the argument."""
"""Returns the cotangent of the argument.

Examples:
---------
>>> from math import pi
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [pi / 4]})
>>> import builtins
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't like builtins then doctest has an ellipses notation for the expected returned values as well.

>>> result = df.select(
... dfn.functions.cot(dfn.col("a")).alias("cot")
... )
>>> builtins.round(
... result.collect_column("cot")[0].as_py(), 1
... )
1.0
"""
return Expr(f.cot(arg.expr))


def degrees(arg: Expr) -> Expr:
"""Converts the argument from radians to degrees."""
"""Converts the argument from radians to degrees.

Examples:
---------
>>> from math import pi
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0,pi,2*pi]})
>>> deg_df = df.select(dfn.functions.degrees(dfn.col("a")).alias("deg"))
>>> deg_df.collect_column("deg")[2].as_py()
360.0
"""
return Expr(f.degrees(arg.expr))


Expand Down Expand Up @@ -774,7 +875,22 @@ def pow(base: Expr, exponent: Expr) -> Expr:


def radians(arg: Expr) -> Expr:
"""Converts the argument from degrees to radians."""
"""Converts the argument from degrees to radians.

Examples:
---------
>>> from math import pi
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [180.0]})
>>> import builtins
>>> result = df.select(
... dfn.functions.radians(dfn.col("a")).alias("rad")
... )
>>> builtins.round(
... result.collect_column("rad")[0].as_py(), 6
... )
3.141593
"""
return Expr(f.radians(arg.expr))


Expand Down Expand Up @@ -935,12 +1051,30 @@ def signum(arg: Expr) -> Expr:


def sin(arg: Expr) -> Expr:
"""Returns the sine of the argument."""
"""Returns the sine of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.sin(dfn.col("a")).alias("sin"))
>>> result.collect_column("sin")[0].as_py()
0.0
"""
return Expr(f.sin(arg.expr))


def sinh(arg: Expr) -> Expr:
"""Returns the hyperbolic sine of the argument."""
"""Returns the hyperbolic sine of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.sinh(dfn.col("a")).alias("sinh"))
>>> result.collect_column("sinh")[0].as_py()
0.0
"""
return Expr(f.sinh(arg.expr))


Expand Down Expand Up @@ -988,12 +1122,30 @@ def substring(string: Expr, position: Expr, length: Expr) -> Expr:


def tan(arg: Expr) -> Expr:
"""Returns the tangent of the argument."""
"""Returns the tangent of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.tan(dfn.col("a")).alias("tan"))
>>> result.collect_column("tan")[0].as_py()
0.0
"""
return Expr(f.tan(arg.expr))


def tanh(arg: Expr) -> Expr:
"""Returns the hyperbolic tangent of the argument."""
"""Returns the hyperbolic tangent of the argument.

Examples:
---------
>>> ctx = dfn.SessionContext()
>>> df = ctx.from_pydict({"a": [0.0]})
>>> result = df.select(dfn.functions.tanh(dfn.col("a")).alias("tanh"))
>>> result.collect_column("tanh")[0].as_py()
0.0
"""
return Expr(f.tanh(arg.expr))


Expand Down
Loading