Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STARTS_WITH and ENDS_WITH #2790

Closed
saschahofmann opened this issue May 24, 2021 · 7 comments · Fixed by #2791
Closed

Add STARTS_WITH and ENDS_WITH #2790

saschahofmann opened this issue May 24, 2021 · 7 comments · Fixed by #2791
Assignees
Labels
expressions Issues or PRs related to the expression API feature Features or general enhancements
Milestone

Comments

@saschahofmann
Copy link
Contributor

saschahofmann commented May 24, 2021

In ibis-project/ibis-bigquery#61 , I suggested to add the big query functions STARTS_WITH and ENDS_WITH to the BigQuery repo. @tswast, rightfully, pointed out that this could go into the main repo.

Should I go ahead and make a PR? Any thing I should look out for?

I understand the operation definitions belong in operations.py. Would a PR also require an implementation for each backend?

My current implementation looks for BigQuery looks like this:

import ibis.expr.rules as rlz
from ibis.expr.operations import Arg, ValueOp
from ibis.expr.types import StringValue
from ibis_bigquery import BigQueryExprTranslator

compiles = BigQueryExprTranslator.compiles


class StartsWith(ValueOp):
    value = Arg(rlz.string)
    start_string = Arg(rlz.string)
    output_type = rlz.shape_like("value", "bool")


def startswith(value, start_string):
    return StartsWith(value, start_string).to_expr()


class EndsWith(ValueOp):
    value = Arg(rlz.string)
    end_string = Arg(rlz.string)
    output_type = rlz.shape_like("value", "bool")


def endswith(value, start_string):
    return EndsWith(value, start_string).to_expr()


StringValue.startswith = startswith
StringValue.endswith = endswith


@compiles(StartsWith)
def _startswith(t, expr):
    # pull out the arguments to the expression
    value, start_string = expr.op().args
    # compile the argument
    t_value = t.translate(value)
    t_start = t.translate(start_string)
    # return a SQL expression that calls the BigQuery STARTS_WITH function
    return f"STARTS_WITH({t_value}, {t_start})"


@compiles(EndsWith)
def _endswith(t, expr):
    # pull out the arguments to the expression
    value, start_string = expr.op().args
    # compile the argument
    t_value = t.translate(value)
    t_start = t.translate(start_string)
    # return a SQL expression that calls the BigQuery STARTS_WITH function
    return f"ENDS_WITH({t_value}, {t_start})"
@datapythonista
Copy link
Contributor

I don't know any database that has specific functions for those. In SQL this is implemented as LIKE 'value%'. Could be implemented in Ibis, but not sure if any other backend will benefit from it. @jreback thoughts?

@datapythonista datapythonista added discussion feature Features or general enhancements labels May 24, 2021
@saschahofmann
Copy link
Contributor Author

saschahofmann commented May 24, 2021

Ah yeah, I thought I checked whether Postgres does it last week but now I can't find it so apparently I didn't 😅 .

SQLAlchemy implements it as well. He does the LIKE 'value%' approach.

@saschahofmann
Copy link
Contributor Author

And maybe there could be an argument made for pandas having it as well?

@saschahofmann
Copy link
Contributor Author

Anyway, happy to implement it for bigquery specifically if that's the consens.

@datapythonista
Copy link
Contributor

If SQLAlchemy implements it, happy to take a PR for it. Should be easy to add to all backends.

@saschahofmann
Copy link
Contributor Author

Is there a way to add the SQLAlchemy implementation? Or is each backend handling it individually?

@datapythonista
Copy link
Contributor

We have a base SQL backend in backends/base/sql and a base SQLAlchemy backend in backends/base/sql/alchemy. Adding the translation functions to each of them should make them work in all backends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expressions Issues or PRs related to the expression API feature Features or general enhancements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants