How to extend Backends with non-scalar functions #8774
-
In previous versions, the docs described a way to add functions to a Backend. For example, here a reminder on how it was done in Ibis 4. Luckily, the approach still works and I only had to update some of the class attribute's names and definitions after upgrading to Ibis 8 🎉 import ibis.expr.datatypes as dt
import ibis.expr.rules as rlz
from ibis.backends.bigquery.compiler import BigQueryExprTranslator
compiles = BigQueryExprTranslator.compiles
class JSONExtract(Value):
value: Value[dt.String]
json_path: Value[dt.String]
shape = rlz.shape_like("value")
dtype = dt.string
def json_extract(value, json_path):
return JSONExtract(value, json_path).to_expr()
StringValue.json_extract = json_extract
@compiles(JSONExtract)
def _json_extract(t, expr):
value, json_path = expr.op().args
t_value = t.translate(value)
t_json_path = t.translate(json_path)
return f"JSON_QUERY({t_value}, {t_json_path})" Now I would like to add the same functionality for postgres but the I'd also be happy to hear whether I could use sqlglot to maybe add a backend agnostic solution (or a solution that works where ever its available). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@saschahofmann Thanks for raising this. We removed that section from the docs due to the instability of the internal operations APIs. Still, I'm glad to hear that it wasn't too bad to update your custom operations. In your particular case, we do support that operation on JSON typed columns, which you should be able to access from string columns by casting:
Regarding We're considering ways to support custom operations in the newly refactored sqlglot-ized version of Ibis, so stay tuned! |
Beta Was this translation helpful? Give feedback.
-
I got another question: This time I am trying to find a generic way to implement bigquery's As before, for BigQuery I am using this to compile bigquery code class AnyValue(Reduction):
arg: Value
dtype = rlz.dtype_like("arg")
def any_value(arg):
return AnyValue(arg).to_expr()
Column.any_value = any_value
@compiles(AnyValue)
def _any_value(t, expr):
(arg,) = expr.op().args
return f"ANY_VALUE({t.translate(arg)})" I am using this to create a distinct functionality across several columns. You group by the columns you want to be distinct and select any value from the others. Another solution would be to have the |
Beta Was this translation helpful? Give feedback.
@saschahofmann Thanks for raising this.
We removed that section from the docs due to the instability of the internal operations APIs.
Still, I'm glad to hear that it wasn't too bad to update your custom operations.
In your particular case, we do support that operation on JSON typed columns, which you should be able to access from string columns by casting: