![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Expressions in a query are matched with any available computed columns so that the cost of the computation can be avoided. Further, this matching allows an index to be used to satisfy a search condition where otherwise a sequential scan might be needed. Since the original implementation of computed columns, a few restrictions have been put in place to avoid unexpected results. Computed columns forbid the use of non-deterministic functions such as rand() or user functions explicitly marked as non-deterministic because matching these expressions would expose the types of anomaly that is described below. The current behavior matches the origianl design of computed columns, but the result is indeed surprising and likely not what was intended by the table creator. A future version of SQL Anywhere will likely either a) forbid any expression in a compute clause where matching would give anomalies as described below, or, b) allow such expressions but forbid matching the computed expression. Also, further documentation of how common sub-expression matching is performed will be included in a future version of the software. Even with those restrictions on allowed expressions or matching, there remain temporal anomalies that can occur with computed columns. For example, if a computed column uses a UDF and the UDF is later modified so that it returns a different result, the computed column will continue to reflect the old result. Any computed column that relies on server state can also exhibit these temporal anomalies. One possibility we have discussed in the past is to disallow computed columns except for those expressions that are explicitly marked as DETERMINISTIC. Such an approach would avoid anomalies at the expense of preventing useful implementations. An alternative to disallowing expressions that might exhibit anomalies would be to give a warning at the time the computed column is created that the column might give rise to anomalies in queries, even queries that don't directly reference the column. These options are under discussion for future versions of SQL Anywhere. In any case, given the current behavior of computed columns, you should use caution when creating a computed column that contains expressions such as CURRENT XXX, now(), XXX_property(), or so on. The work-around that Bruce described can be made more robust using a UDF that is not used in any other query (as shown in the attached example): create function F_dontmatch_ts( x timestamp ) returns timestamp begin return x end; create table ttt ( pk int default autoincrement, val text, change datetime compute( F_dontmatch_ts(current timestamp) ), primary key(pk) ); insert into ttt(val) values ('t1'); insert into ttt(val) values ('t2'); insert into ttt(val) values ('t3'); select * from ttt; select current timestamp, * from ttt; |
![]() |
| Thread Tools | |
| Display Modes | |
| |