pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Exclude constraint on ranges : commutative containment : allow only complete containment
8+ messages / 3 participants
[nested] [flat]

* Exclude constraint on ranges : commutative containment : allow only complete containment
@ 2020-01-29 16:12 Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Achilleas Mantzios @ 2020-01-29 16:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello Dear Postgresql ppl,
I have a table with date ranges and need to express the following constraint : allow overlaps only if there is complete containment, e.g.
allow values in rows like :
[2020-01-01,2020-01-31)
[2020-01-02,2020-01-10)
[2020-01-10,2020-01-20)

but disallow rows like

[2020-01-02,2020-01-10)
[2020-01-08,2020-01-11)

I think that writing a new commutative range operator e.g. |<@@> which would return true if the left operand is either contained by or contains the right operand and false otherwise would solve this, 
I am just wondering if there is a more elegant and economical way to express this. (besides writing a trigger which is always an option).|

-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
@ 2020-01-29 18:32 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-29 18:43   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Michael Lewis <mlewis@entrata.com>
  2020-01-30 11:46   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Adrian Klaver @ 2020-01-29 18:32 UTC (permalink / raw)
  To: Achilleas Mantzios <achill@matrix.gatewaynet.com>; pgsql-general@lists.postgresql.org

On 1/29/20 8:12 AM, Achilleas Mantzios wrote:
> Hello Dear Postgresql ppl,
> I have a table with date ranges and need to express the following 
> constraint : allow overlaps only if there is complete containment, e.g.
> allow values in rows like :
> [2020-01-01,2020-01-31)
> [2020-01-02,2020-01-10)
> [2020-01-10,2020-01-20)
> 
> but disallow rows like
> 
> [2020-01-02,2020-01-10)
> [2020-01-08,2020-01-11)

I'm missing something. Can you provide a more complete example?

> 
> I think that writing a new commutative range operator e.g. |<@@> which 
> would return true if the left operand is either contained by or contains 
> the right operand and false otherwise would solve this, I am just 
> wondering if there is a more elegant and economical way to express this. 
> (besides writing a trigger which is always an option).|
> 
> -- 
> Achilleas Mantzios
> IT DEV Lead
> IT DEPT
> Dynacom Tankers Mgmt
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
@ 2020-01-29 18:43   ` Michael Lewis <mlewis@entrata.com>
  2020-01-30 07:33     ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Michael Lewis @ 2020-01-29 18:43 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Achilleas Mantzios <achill@matrix.gatewaynet.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>

If the new value does not overlap with any existing, allow. If it does
overlap, then it must be fully contained by the existing element, or the
existing element must be fully contained by it. Else, reject. Is that right?

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-29 18:43   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Michael Lewis <mlewis@entrata.com>
@ 2020-01-30 07:33     ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Achilleas Mantzios @ 2020-01-30 07:33 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 29/1/20 8:43 μ.μ., Michael Lewis wrote:

> If the new value does not overlap with any existing, allow. If it does 
> overlap, then it must be fully contained by the existing element, or 
> the existing element must be fully contained by it. Else, reject. Is 
> that right?


Exactly.






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
@ 2020-01-30 11:46   ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-30 16:49     ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Achilleas Mantzios @ 2020-01-30 11:46 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 29/1/20 8:32 μ.μ., Adrian Klaver wrote:
> On 1/29/20 8:12 AM, Achilleas Mantzios wrote:
>> Hello Dear Postgresql ppl,
>> I have a table with date ranges and need to express the following constraint : allow overlaps only if there is complete containment, e.g.
>> allow values in rows like :
>> [2020-01-01,2020-01-31)
>> [2020-01-02,2020-01-10)
>> [2020-01-10,2020-01-20)
>>
>> but disallow rows like
>>
>> [2020-01-02,2020-01-10)
>> [2020-01-08,2020-01-11)
>
> I'm missing something. Can you provide a more complete example?
Dear Adrian,
I can give an example, lets say that we want to model the concept of budget, and we allow basic complete budgets covering a specific time period (daterange) which will have predictions and also 
matched actual transactions (Debits, Credits) , but also want "super" budgets of larger time periods which include a number of basic budgets (sub-budgets) and for which only predictions are allowed, 
not actual transactions. We could make the design strict and explicit by using referential constraints (basic budget pointing to a super budget) but If we chose to not make it strict , and conversely 
allow a more dynamic and liberal way that the system detects one form or the other , we could say :
each sub-budget (of the lowest level of the hierarchy - although for the time being we have only two levels) cannot overlap with any other sub-budget
each super-budget can only fully contain its sub-budgets , no partial overlap allowed.

This could be solved easily if there was a commutative containment operator like :
CREATE OR REPLACE FUNCTION range_containment(anyrange, anyrange)
  RETURNS boolean
  LANGUAGE sql
  IMMUTABLE PARALLEL SAFE STRICT
AS $function$
select $1 <@ $2 OR $1 @> $2;
$function$;

so range_containment returns true if the first operand is contained in the second or contains the second.

create operator <@@> (PROCEDURE=range_containment, LEFTARG=anyrange, RIGHTARG=anyrange, COMMUTATOR = <@@> );

But unfortunately :

alter table bdynacom.acc_budget ADD CONSTRAINT acc_budget_start_end EXCLUDE USING gist (daterange(period_start, period_end, '[]'::text) WITH <@@>);
ERROR:  operator <@@>(anyrange,anyrange) is not a member of operator family "range_ops"
DETAIL:  The exclusion operator must be related to the index operator class for the constraint.

 From a small research I did this might mean recompiling the source to make <@@> member of range_ops .

>
>>
>> I think that writing a new commutative range operator e.g. |<@@> which would return true if the left operand is either contained by or contains the right operand and false otherwise would solve 
>> this, I am just wondering if there is a more elegant and economical way to express this. (besides writing a trigger which is always an option).|
>>
>> -- 
>> Achilleas Mantzios
>> IT DEV Lead
>> IT DEPT
>> Dynacom Tankers Mgmt
>>
>
>


-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-30 11:46   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
@ 2020-01-30 16:49     ` Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-30 17:03       ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Adrian Klaver @ 2020-01-30 16:49 UTC (permalink / raw)
  To: Achilleas Mantzios <achill@matrix.gatewaynet.com>; pgsql-general@lists.postgresql.org

On 1/30/20 3:46 AM, Achilleas Mantzios wrote:
> On 29/1/20 8:32 μ.μ., Adrian Klaver wrote:
>> On 1/29/20 8:12 AM, Achilleas Mantzios wrote:
>>> Hello Dear Postgresql ppl,
>>> I have a table with date ranges and need to express the following 
>>> constraint : allow overlaps only if there is complete containment, e.g.
>>> allow values in rows like :
>>> [2020-01-01,2020-01-31)
>>> [2020-01-02,2020-01-10)
>>> [2020-01-10,2020-01-20)
>>>
>>> but disallow rows like
>>>
>>> [2020-01-02,2020-01-10)
>>> [2020-01-08,2020-01-11)
>>
>> I'm missing something. Can you provide a more complete example?
> Dear Adrian,
> I can give an example, lets say that we want to model the concept of 
> budget, and we allow basic complete budgets covering a specific time 
> period (daterange) which will have predictions and also matched actual 
> transactions (Debits, Credits) , but also want "super" budgets of larger 
> time periods which include a number of basic budgets (sub-budgets) and 
> for which only predictions are allowed, not actual transactions. We 
> could make the design strict and explicit by using referential 
> constraints (basic budget pointing to a super budget) but If we chose to 
> not make it strict , and conversely allow a more dynamic and liberal way 
> that the system detects one form or the other , we could say :
> each sub-budget (of the lowest level of the hierarchy - although for the 
> time being we have only two levels) cannot overlap with any other 
> sub-budget
> each super-budget can only fully contain its sub-budgets , no partial 
> overlap allowed.

Um, that makes my head hurt:) Questions:

1) Are the basic complete budgets and the sub-budgets and super budgets 
existing in the same table?

2) Depending on answer to 1, to prevent overlap could you not use a form 
of the example here?:

https://www.postgresql.org/docs/12/rangetypes.html#RANGETYPES-CONSTRAINT

CREATE TABLE reservation (
     during tsrange,
     EXCLUDE USING GIST (during WITH &&)
);



> 
> This could be solved easily if there was a commutative containment 
> operator like :
> CREATE OR REPLACE FUNCTION range_containment(anyrange, anyrange)
>   RETURNS boolean
>   LANGUAGE sql
>   IMMUTABLE PARALLEL SAFE STRICT
> AS $function$
> select $1 <@ $2 OR $1 @> $2;
> $function$;
> 
> so range_containment returns true if the first operand is contained in 
> the second or contains the second.
> 
> create operator <@@> (PROCEDURE=range_containment, LEFTARG=anyrange, 
> RIGHTARG=anyrange, COMMUTATOR = <@@> );
> 
> But unfortunately :
> 
> alter table bdynacom.acc_budget ADD CONSTRAINT acc_budget_start_end 
> EXCLUDE USING gist (daterange(period_start, period_end, '[]'::text) WITH 
> <@@>);
> ERROR:  operator <@@>(anyrange,anyrange) is not a member of operator 
> family "range_ops"
> DETAIL:  The exclusion operator must be related to the index operator 
> class for the constraint.
> 
>  From a small research I did this might mean recompiling the source to 
> make <@@> member of range_ops .
> 
>>
>>>
>>> I think that writing a new commutative range operator e.g. |<@@> 
>>> which would return true if the left operand is either contained by or 
>>> contains the right operand and false otherwise would solve this, I am 
>>> just wondering if there is a more elegant and economical way to 
>>> express this. (besides writing a trigger which is always an option).|
>>>
>>> -- 
>>> Achilleas Mantzios
>>> IT DEV Lead
>>> IT DEPT
>>> Dynacom Tankers Mgmt
>>>
>>
>>
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-30 11:46   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-30 16:49     ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
@ 2020-01-30 17:03       ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-30 22:16         ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Achilleas Mantzios @ 2020-01-30 17:03 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 30/1/20 6:49 μ.μ., Adrian Klaver wrote:
> On 1/30/20 3:46 AM, Achilleas Mantzios wrote:
>> On 29/1/20 8:32 μ.μ., Adrian Klaver wrote:
>>> On 1/29/20 8:12 AM, Achilleas Mantzios wrote:
>>>> Hello Dear Postgresql ppl,
>>>> I have a table with date ranges and need to express the following constraint : allow overlaps only if there is complete containment, e.g.
>>>> allow values in rows like :
>>>> [2020-01-01,2020-01-31)
>>>> [2020-01-02,2020-01-10)
>>>> [2020-01-10,2020-01-20)
>>>>
>>>> but disallow rows like
>>>>
>>>> [2020-01-02,2020-01-10)
>>>> [2020-01-08,2020-01-11)
>>>
>>> I'm missing something. Can you provide a more complete example?
>> Dear Adrian,
>> I can give an example, lets say that we want to model the concept of budget, and we allow basic complete budgets covering a specific time period (daterange) which will have predictions and also 
>> matched actual transactions (Debits, Credits) , but also want "super" budgets of larger time periods which include a number of basic budgets (sub-budgets) and for which only predictions are 
>> allowed, not actual transactions. We could make the design strict and explicit by using referential constraints (basic budget pointing to a super budget) but If we chose to not make it strict , and 
>> conversely allow a more dynamic and liberal way that the system detects one form or the other , we could say :
>> each sub-budget (of the lowest level of the hierarchy - although for the time being we have only two levels) cannot overlap with any other sub-budget
>> each super-budget can only fully contain its sub-budgets , no partial overlap allowed.
>
> Um, that makes my head hurt:) Questions:
>
> 1) Are the basic complete budgets and the sub-budgets and super budgets existing in the same table?
>
> 2) Depending on answer to 1, to prevent overlap could you not use a form of the example here?:
>
> https://www.postgresql.org/docs/12/rangetypes.html#RANGETYPES-CONSTRAINT
>
> CREATE TABLE reservation (
>     during tsrange,
>     EXCLUDE USING GIST (during WITH &&)
> );
>
same table.
overlap is allowed but only when it is complete containment, which is the whole point of this thread.

>
>
>>
>> This could be solved easily if there was a commutative containment operator like :
>> CREATE OR REPLACE FUNCTION range_containment(anyrange, anyrange)
>>   RETURNS boolean
>>   LANGUAGE sql
>>   IMMUTABLE PARALLEL SAFE STRICT
>> AS $function$
>> select $1 <@ $2 OR $1 @> $2;
>> $function$;
>>
>> so range_containment returns true if the first operand is contained in the second or contains the second.
>>
>> create operator <@@> (PROCEDURE=range_containment, LEFTARG=anyrange, RIGHTARG=anyrange, COMMUTATOR = <@@> );
>>
>> But unfortunately :
>>
>> alter table bdynacom.acc_budget ADD CONSTRAINT acc_budget_start_end EXCLUDE USING gist (daterange(period_start, period_end, '[]'::text) WITH <@@>);
>> ERROR:  operator <@@>(anyrange,anyrange) is not a member of operator family "range_ops"
>> DETAIL:  The exclusion operator must be related to the index operator class for the constraint.
>>
>>  From a small research I did this might mean recompiling the source to make <@@> member of range_ops .
>>
>>>
>>>>
>>>> I think that writing a new commutative range operator e.g. |<@@> which would return true if the left operand is either contained by or contains the right operand and false otherwise would solve 
>>>> this, I am just wondering if there is a more elegant and economical way to express this. (besides writing a trigger which is always an option).|
>>>>
>>>> -- 
>>>> Achilleas Mantzios
>>>> IT DEV Lead
>>>> IT DEPT
>>>> Dynacom Tankers Mgmt
>>>>
>>>
>>>
>>
>>
>
>


-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Exclude constraint on ranges : commutative containment : allow only complete containment
  2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-29 18:32 ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-30 11:46   ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
  2020-01-30 16:49     ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-30 17:03       ` Re: Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
@ 2020-01-30 22:16         ` Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Adrian Klaver @ 2020-01-30 22:16 UTC (permalink / raw)
  To: Achilleas Mantzios <achill@matrix.gatewaynet.com>; pgsql-general@lists.postgresql.org

On 1/30/20 9:03 AM, Achilleas Mantzios wrote:

>>
>> Um, that makes my head hurt:) Questions:
>>
>> 1) Are the basic complete budgets and the sub-budgets and super 
>> budgets existing in the same table?
>>
>> 2) Depending on answer to 1, to prevent overlap could you not use a 
>> form of the example here?:
>>
>> https://www.postgresql.org/docs/12/rangetypes.html#RANGETYPES-CONSTRAINT
>>
>> CREATE TABLE reservation (
>>     during tsrange,
>>     EXCLUDE USING GIST (during WITH &&)
>> );
>>
> same table.
> overlap is allowed but only when it is complete containment, which is 
> the whole point of this thread.
> 

I finally got it. Unfortunately that does not mean I have solution.


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2020-01-30 22:16 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-01-29 16:12 Exclude constraint on ranges : commutative containment : allow only complete containment Achilleas Mantzios <achill@matrix.gatewaynet.com>
2020-01-29 18:32 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-29 18:43   ` Michael Lewis <mlewis@entrata.com>
2020-01-30 07:33     ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2020-01-30 11:46   ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2020-01-30 16:49     ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-30 17:03       ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2020-01-30 22:16         ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox