all groups > sql server programming > april 2006 >
You're in the

sql server programming

group:

Weird..


Weird.. Omnibuzz
4/16/2006 9:55:01 PM
sql server programming:
SQL Server assumes all functions to be deterministic.. is it true??

check this out.

create view vtest
as
select rand() as today

drop function func1
create function func1 (@b varchar(10))
returns float
as
begin
declare @a float
select @a = today from vtest
return(@a)
end

select col1, dbo.func1(col1) as col2 from ta group by col1

select distinct col1, dbo.func1(col1) as col2 from ta

RE: Weird.. KH
4/16/2006 10:09:02 PM
Not true... SELECT OBJECTPROPERTY(OBJECT_ID('func1'), 'IsDeterministic')


[quoted text, click to view]
Re: Weird.. John Bell
4/17/2006 12:00:00 AM
Hi

See "Deterministic and Nondeterministic Functions" in Books Online.

John

[quoted text, click to view]

RE: Weird.. Omnibuzz
4/17/2006 1:55:01 AM
Hi KH and John,
Thanks for your replies. I can understand that it says the function is
non-deterministic. But is it using the information in generating the query
execution plan.
And I know what deterministic and non-deterministic functions are. what I am
trying to analyze here is the behaviour of the query engine.

Looking at the query I posted,

select col1, dbo.func1(col1) as col2 from ta group by col1

If SQL Server already knew the function was non-deterministic, then it
should have thrown an error saying "not all columns are in the group
by...blah..blah.."..

But it runs without any problem and what more gives an erronous result.. or
so I believe.

check out the query execution plan of both the queries. where the
sort/distinct operation is done. I find THAT wierd :) I hope I made sense and
RE: Weird.. Omnibuzz
4/17/2006 2:01:01 AM
A small change in the question.. I shouldn't have generalized and I apologise.
SQL server assumed the function I had written to be deterministic.

[quoted text, click to view]
Re: Weird.. Omnibuzz
4/17/2006 10:24:42 AM
Hi John,
I think you need to do better than that :). The create view failed
because the view definition was wrong.
try this

CREATE View Utest

AS select CURRENT_USER as usr

Moreover, rand() function is non-deterministic whether you give a seed or
not. check out the least significant digits in the result and it will change
:)

Now that we have resolved that issue. It puts us to square one.. why this
Re: Weird.. John Bell
4/17/2006 2:19:48 PM
Hi

It does seem to be an anomaly RAND() is supposed to only be deterministic
when you use a seed value, therefore you would not expect to be able to
create your vtest view as it should be have like:
CREATE View Utest

AS select CURRENT_USER

Msg 4511, Level 16, State 1, Procedure Utest, Line 2
Create View or Function failed because no column name was specified for
column 1.

Therefore it seems to have assumed that it will be deterministic and given
the subsequent behaviour.

John

[quoted text, click to view]

Re: Weird.. John Bell
4/17/2006 7:39:45 PM

[quoted text, click to view]
Guess I should have paid more attention to the message!

[quoted text, click to view]
This is not what is documented in Book Online. Your function and view would
also need to be SCHEMA_BINDING to make it deterministic but even adding them
does not change that.

[quoted text, click to view]

I am not sure why this is a problem, it is unforseen behaviour why you may
want to log a support call on.

John

Re: Weird.. John Bell
4/17/2006 8:42:32 PM
Looking at SHOWPLAN_TEXT

If RAND is used through a function
|--Compute
Scalar(DEFINE:([Expr1004]=[testdb].[dbo].[func1](CONVERT_IMPLICIT(varchar(10),[testdb].[dbo].[ta].[col1],0))))

|--Constant Scan(VALUES:((rand())))

If RAND is used directly
|--Compute Scalar(DEFINE:([ConstExpr1005]=rand()))

Therefore it seems that the criteria is a scalar function rather than a
deterministic one, but using RAND directly returns the same value for each
row.

John

[quoted text, click to view]

Re: Weird.. Erland Sommarskog
4/17/2006 10:15:50 PM
Omnibuzz (Omnibuzz@discussions.microsoft.com) writes:
[quoted text, click to view]

I'm not sure that I see the problem. Try to use the function in an
indexed view or an indexed computed column, and see whether SQL Server
things its deterministic.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button