Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Understanding T-SQL Expression Short-Circuiting


Understanding T-SQL Expression Short-Circuiting

Author
Message
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8838 Visits: 13345
Comments posted to this topic are about the item Understanding T-SQL Expression Short-Circuiting

I would like to acknowledge and thank Gus Gwynne, Jeff Moden and Paul White for their review and the constructive feedback that they gave for this article.
For some reason my thanks did not make it to the article page. My humble apologies to all of you.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Phil Factor
Phil Factor
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1511 Visits: 2968
This is a great bit of research, with some timely warnings from Gianluca for anyone who makes assumptions about TSQL based on experience with a procedural language.


Best wishes,

Phil Factor
Simple Talk
sknox
sknox
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2365 Visits: 2794
Any boolean expression is capable of being short-circuited, in the right circumstances.


So under what circumstances can you short-circuit an XOR? (i.e, if either A or B but not both then C)?
Daniel Ruehle
Daniel Ruehle
SSC-Enthusiastic
SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)

Group: General Forum Members
Points: 191 Visits: 86
One easy way to make sure short circuiting works the way you want it is using case statements:

select
*
from
Person
where
1 = 1
and CreateDateTime > getdate() - 30
and case
when Age > 90 then 1
when Age < 5 then 0
when Gender = 'Male' then 1
when LastName like 'SAM%' then 1
else 0
end = 1



This gets records for all people over the age of 90, males of age 5 or more and anyone with a last name that starts with the letters SAM. Notice that the integer checks are done first as they are the easiest to evaluate and the expensive like expression is last. The documentation for the case statement explicity says:

Evaluates, in the order specified, Boolean_expression for each WHEN clause.

so this is like an explicit short circuit if you would like.
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8838 Visits: 13345
sknox (12/30/2010)
Any boolean expression is capable of being short-circuited, in the right circumstances.


So under what circumstances can you short-circuit an XOR? (i.e, if either A or B but not both then C)?


T-SQL lacks a XOR logical operator, but it can be implemented from its definition:
A XOR B = (A AND NOT B) OR (NOT A AND B)



Sorry for the stupid example, I can't think of a better one right now: to find all users with NULL first_name (expression A) or NULL middle_name (expression B) but not both you could write:

-- This is how you would do it if T-SQL had a XOR operator.
SELECT *
FROM user
WHERE (first_name IS NULL) XOR (middle_name IS NULL)

-- This is how you have to code it with AND, OR and NOT operators
SELECT *
FROM user
WHERE (first_name IS NULL AND middle_name IS NOT NULL)
OR (first_name IS NOT NULL AND middle_name IS NULL)



Any boolean operator can be rewritten using AND, OR and NOT.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Jason E Bacani...
Jason E Bacani (JEBacaniSQLDude)
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 68
Excellent article. Thanks for sharing!
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8838 Visits: 13345
Phil Factor (12/30/2010)
This is a great bit of research, with some timely warnings from Gianluca for anyone who makes assumptions about TSQL based on experience with a procedural language.

Thanks, Phil.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8838 Visits: 13345
CELKO (12/30/2010)
Another short-circuit (or McCarthy) evaluation problem from Algol was functions with side effects:

IF a=b OR Boolean_function_with side_effect (x) THEN ..

If the function is skipped, then there was no side effect; if it was executed, then a or b might be changed. Thius is why functional programming disallows aside effects.


Unfortunately, many programming languages don't disallow side-effects inside functions. It's up to the programmer to produce reliable code and avoid "dirty tricks".
As a side note, a CLR function can update data. ;-)

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Gianluca Sartori
Gianluca Sartori
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8838 Visits: 13345
Daniel Ruehle (12/30/2010)
One easy way to make sure short circuiting works the way you want it is using case statements:

select
*
from
Person
where
1 = 1
and CreateDateTime > getdate() - 30
and case
when Age > 90 then 1
when Age < 5 then 0
when Gender = 'Male' then 1
when LastName like 'SAM%' then 1
else 0
end = 1



This gets records for all people over the age of 90, males of age 5 or more and anyone with a last name that starts with the letters SAM. Notice that the integer checks are done first as they are the easiest to evaluate and the expensive like expression is last. The documentation for the case statement explicity says:

Evaluates, in the order specified, Boolean_expression for each WHEN clause.

so this is like an explicit short circuit if you would like.


You're right, Daniel. CASE is guranteed to evaluate expressions in the exact order they appear.
What is questionable is the time you save by pushing "expensive tests" down. Unless you're working with billion row tables, you wouldn't even notice the difference. It's the query plan that decides how fast the query will run, not the number of expressions to evaluate.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
magarity kerns
magarity kerns
SSC-Addicted
SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)

Group: General Forum Members
Points: 454 Visits: 397
Excellent article - For more fun, check other DBMSes. I checked on Oracle and "select 'A' from dual where 1=0 or 1/0 = 1;" gives a division by zero error. (although it may need to be in a procedure on Oracle to do it with the IF statement). Anyone have DB/2 or Teradata handy?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search