|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 4,804,
Visits: 8,067
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 5:02 AM
Points: 533,
Visits: 2,284
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:22 AM
Points: 1,037,
Visits: 1,354
|
|
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)?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, March 13, 2013 9:21 AM
Points: 158,
Visits: 83
|
|
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.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 4,804,
Visits: 8,067
|
|
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.
Get your two-cent-answer quickly The Spaghetti DBA
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, June 08, 2012 1:09 PM
Points: 1,
Visits: 58
|
|
| Excellent article. Thanks for sharing!
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 4,804,
Visits: 8,067
|
|
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.
Get your two-cent-answer quickly The Spaghetti DBA
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 4,804,
Visits: 8,067
|
|
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.
Get your two-cent-answer quickly The Spaghetti DBA
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 4,804,
Visits: 8,067
|
|
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.
Get your two-cent-answer quickly The Spaghetti DBA
|
|
|
|