Opened 11 months ago

Last modified 10 months ago

#28805 new New feature

Add a database function for RegexpReplace

Reported by: Joey Wilhelm Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've created a database function in my own project to utilize REGEXP_REPLACE, and wanted to contribute it upstream. At a quick glance, it appears that this is only available on PostgreSQL and Oracle. So my main question would be, which route would be preferable for inclusion? Should this be added to the PostgreSQL-specific code and let Oracle languish, or would this require the addition of a new feature flag on database backends?

With the former implementation, I have code ready to go. The latter, I would definitely want some guidance.

This is of course all assuming that this feature is desired.

Here is an example usage:

MyModel.objects.annotate(no_letters=RegexpReplace(F('name'), r'[A-Za-z]+', ''))

Change History (4)

comment:1 Changed 11 months ago by Matthew Schinckel

Personally, I'd create it in the django.contrib.postgres section: there are already other functions in there that you should be able to look at how they are written.

The other alternative is to put it in django.db.models.functions.text, but I'm not sure how to flag that it only works on specific backends there.

You might want to bring this up on the django-developers list, as that sometimes gets a bit more notice.

comment:2 Changed 11 months ago by Matthew Schinckel

Oh, it is worth pointing out that this is something that should be easy to package up in a reusable manner, since it (probably) won't require any changes, just the addition of a new class.

That class could then be imported from anywhere.

comment:3 Changed 11 months ago by Joey Wilhelm

Yeah, I was debating the thought of creating some sort of django-postgres-regex package, for this and related functions. But if I could contribute it to core, why not, ya know?

The implementation relatively easy; I based it off, I believe, Substr.

from django.db.models import Func, Value


class RegexpReplace(Func):
    function = 'REGEXP_REPLACE'

    def __init__(self, expression, pattern, replacement, **extra):
        if not hasattr(pattern, 'resolve_expression'):
            if not isinstance(pattern, str):
                raise TypeError("'pattern' must be a string")
            pattern = Value(pattern)
        if not hasattr(replacement, 'resolve_expression'):
            if not isinstance(replacement, str):
                raise TypeError("'replacement' must be a string")
            replacement = Value(replacement)
        expressions = [expression, pattern, replacement]
        super().__init__(*expressions, **extra)

comment:4 Changed 10 months ago by Tim Graham

Summary: Provide a new database function for RegexpReplaceAdd a database function for RegexpReplace
Triage Stage: UnreviewedAccepted

For a mergable patch, I think we would want both Oracle and PostgreSQL support.

Note: See TracTickets for help on using tickets.
Back to Top