[Django] #28805: Provide a new database function for RegexpReplace

40 views
Skip to first unread message

Django

unread,
Nov 16, 2017, 1:37:07 PM11/16/17
to django-...@googlegroups.com
#28805: Provide a new database function for RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey | Owner: nobody
Wilhelm |
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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:


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

--
Ticket URL: <https://code.djangoproject.com/ticket/28805>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2017, 6:19:09 PM11/16/17
to django-...@googlegroups.com
#28805: Provide a new database function for RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:1>

Django

unread,
Nov 16, 2017, 6:20:07 PM11/16/17
to django-...@googlegroups.com
#28805: Provide a new database function for RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:2>

Django

unread,
Nov 16, 2017, 6:31:39 PM11/16/17
to django-...@googlegroups.com
#28805: Provide a new database function for RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (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.

{{{#!python
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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:3>

Django

unread,
Dec 27, 2017, 1:16:57 PM12/27/17
to django-...@googlegroups.com
#28805: Add a database function for RegexpReplace
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:4>

Django

unread,
Feb 7, 2019, 11:48:40 PM2/7/19
to django-...@googlegroups.com
#28805: Add a database function for RegexpReplace
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sardorbek Imomaliev):

* cc: Sardorbek Imomaliev (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:5>

Django

unread,
Jun 7, 2019, 6:12:24 AM6/7/19
to django-...@googlegroups.com
#28805: Add a database function for RegexpReplace
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Oskar Persson):

* cc: Oskar Persson (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:6>

Django

unread,
Feb 9, 2020, 9:21:53 AM2/9/20
to django-...@googlegroups.com
#28805: Add a database function for RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* status: new => assigned
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* owner: nobody => Nick Pope
* needs_docs: 0 => 1
* has_patch: 0 => 1


Comment:

I have a WIP [https://github.com/django/django/pull/12438 PR].

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:7>

Django

unread,
Jul 1, 2020, 5:12:00 PM7/1/20
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace

-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* needs_docs: 1 => 0


Comment:

I've updated the [https://github.com/django/django/pull/12438 PR] to add
support for `RegexpStrIndex`, `RegexpReplace`, and `RegexpSubstr`.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:8>

Django

unread,
Sep 17, 2020, 3:48:05 AM9/17/20
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:9>

Django

unread,
Jul 17, 2021, 7:21:52 AM7/17/21
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:10>

Django

unread,
Mar 18, 2022, 3:26:08 AM3/18/22
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:11>

Django

unread,
Jun 11, 2023, 4:01:16 PM6/11/23
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:12>

Django

unread,
Aug 7, 2023, 6:49:31 AM8/7/23
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* needs_better_patch: 0 => 1


Comment:

Moving back to PNI. Having a rethink on a few aspects of this patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:13>

Django

unread,
Aug 7, 2023, 7:20:05 AM8/7/23
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

You can also remove code for MySQL <= 8.0.4.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:14>

Django

unread,
Aug 7, 2023, 7:55:32 AM8/7/23
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

Replying to [comment:14 Mariusz Felisiak]:


> You can also remove code for MySQL <= 8.0.4.

Yup. Already done in what I'm working on.

--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:15>

Django

unread,
Aug 7, 2023, 8:47:24 AM8/7/23
to django-...@googlegroups.com
#28805: Add database functions for regular expressions, e.g. RegexpReplace
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Oskar Persson):

* cc: Oskar Persson (removed)


--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:16>

Reply all
Reply to author
Forward
0 new messages