Re: Arithmetic operators for macaddr type

Lists: pgsql-hackers
From: Brendan Jurd <direvus(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Arithmetic operators for macaddr type
Date: 2011-12-12 04:57:48
Message-ID: CADxJZo22bcxcqUEF__TANNtCjrzke2r9eHtJAf6QCHhbeU=FFQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello folks,

I just bumped into a situation where I wanted to do a little macaddr
arithmetic in postgres. I note that the inet type has support for
bitwise AND, OR and NOT, as well as subtraction, but macaddr has none
of the above.

These operations are easy to perform in C, but relatively a pain to do
in SQL, especially as there doesn't seem to be a direct way to get a
macaddr into a plain numeric form.

I can't see any reason why postgres shouldn't support these operations
on macaddr. I'd like to add them as fully realised operators in core.
Would that be acceptable?

Cheers,
BJ


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Arithmetic operators for macaddr type
Date: 2011-12-12 04:59:40
Message-ID: CAFj8pRBCghxmvDkJHZN-=h5i5dAXRWQyrkr4-ag=6DisvoDHiw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/12/12 Brendan Jurd <direvus(at)gmail(dot)com>:
> Hello folks,
>
> I just bumped into a situation where I wanted to do a little macaddr
> arithmetic in postgres.  I note that the inet type has support for
> bitwise AND, OR and NOT, as well as subtraction, but macaddr has none
> of the above.
>
> These operations are easy to perform in C, but relatively a pain to do
> in SQL, especially as there doesn't seem to be a direct way to get a
> macaddr into a plain numeric form.
>
> I can't see any reason why postgres shouldn't support these operations
> on macaddr.  I'd like to add them as fully realised operators in core.
>  Would that be acceptable?

+1

Pavel

>
> Cheers,
> BJ
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers


From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Arithmetic operators for macaddr type
Date: 2011-12-13 05:16:14
Message-ID: CADxJZo3ZoGFAMph6DV3eehqh6r0aarHYaLo64vGpnGDxvgcJFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 12 December 2011 15:59, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> 2011/12/12 Brendan Jurd <direvus(at)gmail(dot)com>:
>> I just bumped into a situation where I wanted to do a little macaddr
>> arithmetic in postgres.  I note that the inet type has support for
>> bitwise AND, OR and NOT, as well as subtraction, but macaddr has none
>> of the above.
>
> +1
>

Here is a patch for $SUBJECT. I merely added support for ~, & and |
operators for the macaddr type. The patch itself is rather trivial,
and includes regression tests and a doc update.

For the documentation, I did think about adding a new table for the
macaddr operators, but in the end decided it would probably be an
overkill. If others think a table would be better, I'm happy to
revise it.

I also considered adding a function which would return the numeric
value of the MAC as a bigint, but figured I might save that for a
separate patch.

Cheers,
BJ

Attachment Content-Type Size
macaddr-bitwise-ops.diff text/x-patch 6.7 KB

From: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Arithmetic operators for macaddr type
Date: 2012-01-17 05:38:04
Message-ID: CAHGQGwFoppCsM6zOs9gHcWiFvLUvbqKGm7ek9PX_Nd-i13E86g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Dec 13, 2011 at 2:16 PM, Brendan Jurd <direvus(at)gmail(dot)com> wrote:
> On 12 December 2011 15:59, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>> 2011/12/12 Brendan Jurd <direvus(at)gmail(dot)com>:
>>> I just bumped into a situation where I wanted to do a little macaddr
>>> arithmetic in postgres.  I note that the inet type has support for
>>> bitwise AND, OR and NOT, as well as subtraction, but macaddr has none
>>> of the above.
>>
>> +1
>>
>
> Here is a patch for $SUBJECT.  I merely added support for ~, & and |
> operators for the macaddr type.  The patch itself is rather trivial,
> and includes regression tests and a doc update.

The patch looks fine except that it uses the OIDs already used in pg_proc.h.
Attached is the updated version of the patch, which fixes the above problem.

> For the documentation, I did think about adding a new table for the
> macaddr operators, but in the end decided it would probably be an
> overkill.

Agreed.

Regards,

--
Fujii Masao
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center

Attachment Content-Type Size
macaddr-bitwise-ops_v2.diff text/x-diff 6.7 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Cc: Brendan Jurd <direvus(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Arithmetic operators for macaddr type
Date: 2012-01-19 20:26:08
Message-ID: CA+Tgmoaa+bAZuH-o3K6_HRE=xZf7JCWR+ccR5_fF_xWkg+Jqfg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jan 17, 2012 at 12:38 AM, Fujii Masao <masao(dot)fujii(at)gmail(dot)com> wrote:
>> Here is a patch for $SUBJECT.  I merely added support for ~, & and |
>> operators for the macaddr type.  The patch itself is rather trivial,
>> and includes regression tests and a doc update.
>
> The patch looks fine except that it uses the OIDs already used in pg_proc.h.
> Attached is the updated version of the patch, which fixes the above problem.

That same problem came back into existence, so I fixed it again, added
a catversion bump, and committed this.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company