Thread: syntax sugar for conditional check

syntax sugar for conditional check

From
Alexander Ostrow
Date:
Hello, <div class=""><div class=""><br class="" /></div><div class="">I thought it would be cool to have conditional
checksyntax, which gets converted to simple check constraint syntax. </div><div class=""><br class="" /></div><div
class="">Here’sa gist:</div><div class=""><br class="" /></div><div class=""><a class=""
href="https://gist.github.com/aj0strow/5a07f2ddcad324c4dac2c4095c821999">https://gist.github.com/aj0strow/5a07f2ddcad324c4dac2c4095c821999</a></div><div
class=""><brclass="" /></div><div class="">It’s just sugar, but i think it would make check constraints easier to read,
andeasier to write without backwards boolean logic. </div><div class=""><br class="" /></div><div class="">Thank
you,</div><divclass=""><br class="" /></div></div><div class="">AJ</div><div class=""><br class="" /></div> 

Re: syntax sugar for conditional check

From
"David G. Johnston"
Date:
On Thu, Mar 31, 2016 at 10:19 AM, Alexander Ostrow <aj@epcylon.com> wrote:
Hello,

I thought it would be cool to have conditional check syntax, which gets converted to simple check constraint syntax. 

Here’s a gist:


It’s just sugar, but i think it would make check constraints easier to read, and easier to write without backwards boolean logic. 

For future reference please make every effort to make emails to the list self-contained - which has the added benefit of avoid link expiration in the future.

As to the recommendation at hand - I don't see significant value in implementing non-SQL Standard syntax in this area.

David J.
​​

Re: syntax sugar for conditional check

From
Jim Nasby
Date:
On 3/31/16 12:19 PM, Alexander Ostrow wrote:
> Hello,
>
> I thought it would be cool to have conditional check syntax, which gets
> converted to simple check constraint syntax.
>
> Here’s a gist:
>
> https://gist.github.com/aj0strow/5a07f2ddcad324c4dac2c4095c821999
>
> It’s just sugar, but i think it would make check constraints easier to
> read, and easier to write without backwards boolean logic.

Rather than this, I think an exclusive-or operator would be a lot more 
useful. The only difficulty I run into with CHECK constaints is when I 
want to ensure that only ONE condition is true.
-- 
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com



Re: syntax sugar for conditional check

From
Pavel Stehule
Date:


2016-04-01 18:57 GMT+02:00 David G. Johnston <david.g.johnston@gmail.com>:
On Thu, Mar 31, 2016 at 10:19 AM, Alexander Ostrow <aj@epcylon.com> wrote:
Hello,

I thought it would be cool to have conditional check syntax, which gets converted to simple check constraint syntax. 

Here’s a gist:


It’s just sugar, but i think it would make check constraints easier to read, and easier to write without backwards boolean logic. 

For future reference please make every effort to make emails to the list self-contained - which has the added benefit of avoid link expiration in the future.

As to the recommendation at hand - I don't see significant value in implementing non-SQL Standard syntax in this area.

+1

This formula should be known to all developers

Regards

Pavel


David J.
​​


Re: syntax sugar for conditional check

From
Tom Lane
Date:
Jim Nasby <Jim.Nasby@BlueTreble.com> writes:
> Rather than this, I think an exclusive-or operator would be a lot more 
> useful. The only difficulty I run into with CHECK constaints is when I 
> want to ensure that only ONE condition is true.

"bool != bool" works as XOR.  If you need "exactly one of N" you could
do something like "(cond1::int + cond2::int + ...) = 1".  We could
wrap some syntactic sugar around either of these, but it's not clear
to me that it'd be any more useful than a custom SQL function.
        regards, tom lane



Re: syntax sugar for conditional check

From
Jim Nasby
Date:
On 4/1/16 1:08 PM, Tom Lane wrote:
> Jim Nasby <Jim.Nasby@BlueTreble.com> writes:
>> Rather than this, I think an exclusive-or operator would be a lot more
>> useful. The only difficulty I run into with CHECK constaints is when I
>> want to ensure that only ONE condition is true.
>
> "bool != bool" works as XOR.  If you need "exactly one of N" you could
> do something like "(cond1::int + cond2::int + ...) = 1".  We could
> wrap some syntactic sugar around either of these, but it's not clear
> to me that it'd be any more useful than a custom SQL function.

It would prevent having to re-create that function every time... :)
-- 
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com



Re: syntax sugar for conditional check

From
"David G. Johnston"
Date:
On Fri, Apr 1, 2016 at 3:22 PM, Jim Nasby <Jim.Nasby@bluetreble.com> wrote:
On 4/1/16 1:08 PM, Tom Lane wrote:
Jim Nasby <Jim.Nasby@BlueTreble.com> writes:
Rather than this, I think an exclusive-or operator would be a lot more
useful. The only difficulty I run into with CHECK constaints is when I
want to ensure that only ONE condition is true.

"bool != bool" works as XOR.  If you need "exactly one of N" you could
do something like "(cond1::int + cond2::int + ...) = 1".  We could
wrap some syntactic sugar around either of these, but it's not clear
to me that it'd be any more useful than a custom SQL function.

It would prevent having to re-create that function every time... :)

​And it would nicely complement our recent addition of "
num_nonnulls
​(variadic "any")​"

David J.