import ibis.expr.datatypes as dt
import ibis.expr.rules as rlz
import ibis.expr.datashape as ds
from ibis.expr.operations import Value
class JulianDay(Value):
arg: Value[dt.String, ds.Any]
= dt.float32
dtype = rlz.shape_like('arg') shape
Add an elementwise operation
This notebook will show you how to add a new elementwise operation to an existing backend.
We are going to add julianday
, a function supported by the SQLite database, to the SQLite Ibis backend.
The Julian day of a date, is the number of days since January 1st, 4713 BC. For more information check the Julian day Wikipedia page.
Step 1: Define the Operation
Let’s define the julianday
operation as a function that takes one string input argument and returns a float.
def julianday(date: str) -> float:
"""Return the Julian day from a date."""
We just defined a JulianDay
class that takes one argument of type string or binary, and returns a float.
Step 2: Define the API
Because we know the output type of the operation, to make an expression out of JulianDay
we can construct it and call its ibis.expr.types.Node.to_expr
method.
We still need to add a method to StringValue
(this needs to work on both scalars and columns).
When you add a method to any of the expression classes whose name matches *Value
both the scalar and column child classes will pick it up, making it easy to define operations for both scalars and columns in one place.
We can do this by defining a function and assigning it to the appropriate class of expressions.
from ibis.expr.types import StringValue
def julianday(string_value):
return JulianDay(string_value).to_expr()
= julianday StringValue.julianday
Interlude: Create some expressions with julianday
import ibis
= ibis.table(dict(string_col="string"), name="t")
t
t.string_col.julianday()
r0 := UnboundTable: t string_col string JulianDay(string_col): JulianDay(r0.string_col)
Step 3: Turn the Expression into SQL
import sqlalchemy as sa
@ibis.sqlite.add_operation(JulianDay)
def _julianday(translator, expr):
# pull out the arguments to the expression
= expr.args
(arg,)
# compile the argument
= translator.translate(arg)
compiled_arg
# return a SQLAlchemy expression that calls into the SQLite julianday function
return sa.func.julianday(compiled_arg)
Step 4: Putting it all Together
Download the geography database.
!curl -LsS -o geography.db 'https://storage.googleapis.com/ibis-tutorial-data/geography.db'
= ibis.sqlite.connect("geography.db") con
Create and execute a julianday
expression
= con.table("independence")
ind ind
DatabaseTable: independence country_code string independence_date date independence_from string
= ind.independence_date.cast("string")
day day
r0 := DatabaseTable: independence country_code string independence_date date independence_from string Cast(independence_date, string): Cast(r0.independence_date, to=string)
= day.julianday().name("jday")
jday_expr jday_expr
r0 := DatabaseTable: independence country_code string independence_date date independence_from string jday: JulianDay(Cast(r0.independence_date, to=string))
ibis.to_sql(jday_expr)
SELECT
CAST(t0.independence_date AS TEXT)) AS jday
JULIANDAY(FROM independence AS t0
Because we’ve defined our operation on StringValue
, and not just on StringColumn
we get operations on both string scalars and string columns for free.
= ibis.literal("2010-03-14").julianday()
jday con.execute(jday)
2455269.5