SQL Clone
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
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24993 Visits: 13362
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
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4970 Visits: 3031
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
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4125 Visits: 2935
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 Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 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
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24993 Visits: 13362
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)
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 68
Excellent article. Thanks for sharing!
Gianluca Sartori
Gianluca Sartori
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24993 Visits: 13362
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
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24993 Visits: 13362
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
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24993 Visits: 13362
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
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