-
Notifications
You must be signed in to change notification settings - Fork 18
Register pivot #58
Comments
I had another look and it seems to me like I would need to overwrite the |
The ideal flow is that we add the operator to https://github.com/ibis-project/ibis/blob/master/ibis/expr/operations.py (plus add the pivot method to the table class(es). Then we register the implementation here: ibis-bigquery/ibis_bigquery/compiler.py Lines 481 to 484 in abe966b
It's certainly possible to monkeypatch the base ibis classes here to have BigQuery-specific functionality, but I'd like to make sure we stay consistent with the rest of the ibis community. |
Hm I think in this case it's very bigquery specific. Most databases don't have a pivot implementation? |
I tried to register this for our project. This is what I have so far (probably not quite correct but not the issue right now). import ibis.expr.rules as rlz
from ibis.expr.operations import Arg, TableNode
from ibis.expr.types import TableExpr
from ibis_bigquery import BigQueryExprTranslator, BigQueryTable
from ibis.expr.schema import HasSchema
compiles = BigQueryExprTranslator.compiles
class Pivot(TableNode, HasSchema):
table = Arg(TableExpr)
aggregation = Arg(rlz.scalar(rlz.string))
input_column = Arg(rlz.noop)
pivot_columns = Arg(rlz.array_of(rlz.string))
def pivot(table, aggregation, input_column, pivot_columns):
return Pivot(table, aggregation, input_column, pivot_columns).to_expr()
TableExpr.pivot = pivot
@compiles(Pivot)
def _pivot(t, expr):
table, aggregation, input_column, pivot_columns = expr.op().args
return (f"SELECT * FROM ({t.translate(table)})"
f"PIVOT({t.translate(aggregation)} FOR {t.translate(input_column)} IN {t.translate(pivot_columns)})") Unfortunately, when I try to call
I can't figure out what I am doing wrong. It looks to me like it's not registering the compile function . I can call |
Could someone point me to where I could see an example for registering a new BigQuery operator to ibis?
More specifically, I would like to add the new pivot operator.
I imagine it to be called like this
And I would expect it to produce something like
I had a quick look at compiler.py but it isn't clear to me how to achieve this.
The text was updated successfully, but these errors were encountered: