Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Understanding T-SQL Expression Short-Circuiting Expand / Collapse
Author
Message
Posted Wednesday, December 29, 2010 8:26 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 4,651, Visits: 11,108
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
Post #1040686
Posted Thursday, December 30, 2010 2:43 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, December 12, 2014 11:01 AM
Points: 594, Visits: 2,580
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
Post #1040780
Posted Thursday, December 30, 2010 7:26 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, December 12, 2014 8:24 PM
Points: 1,945, Visits: 3,173
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.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1040893
Posted Thursday, December 30, 2010 7:58 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:36 PM
Points: 1,380, Visits: 1,750
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)?
Post #1040910
Posted Thursday, December 30, 2010 8:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:48 AM
Points: 175, 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.
Post #1040928
Posted Thursday, December 30, 2010 8:33 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 4,651, Visits: 11,108
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
Post #1040939
Posted Thursday, December 30, 2010 8:33 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 8:07 AM
Points: 1, Visits: 66
Excellent article. Thanks for sharing!
Post #1040941
Posted Thursday, December 30, 2010 8:55 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 4,651, Visits: 11,108
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
Post #1040960
Posted Thursday, December 30, 2010 9:01 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 4,651, Visits: 11,108
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
Post #1040963
Posted Thursday, December 30, 2010 9:17 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 4,651, Visits: 11,108
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
Post #1040974
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse