GitHub user DaveBirdsall opened a pull request:
https://github.com/apache/trafodion/pull/1414
[TRAFODION-2840] Make [first n] with ORDER BY views non-updatable
JIRA TRAFODION-2822 attempted to make views containing [first n] in the
select list non-updatable. It missed one case, the case where both [first n]
and ORDER BY are present in the view definition.
The reason it missed that case is because of a design oddity in how [first
n] is implemented. When there is no ORDER BY clause, the Binder pass introduces
a FirstN node into the query tree. But if an ORDER BY is present, it defers
doing this to the Generator. That means any FirstN-related semantic checks in
earlier passes will not be applied when ORDER BY is present.
This set of changes refactors the FirstN implementation a little bit so
that the FirstN can be introduced in the Binder when ORDER BY is present. Then
the check added in JIRA TRAFODION-2822 will apply, and such views will be
marked non-updatable as desired.
The refactored design copies a bound and transformed version of the ORDER
BY clause into the FirstN node at Normalize time. (Aside: I tried copying the
unbound ORDER BY tree into the FirstN node at Bind time initially, but this
fails if there are expressions in the select list referenced by the ORDER BY;
different ValueIds get assigned to parts of the expressions than in the
original resulting in expression coverage check failures at Optimize time.)
The refactored design has been implemented for all code paths that are
involved in view definition, which is all that is required to satisfy this
JIRA. However, there is another code path that hasn't been addressed: If a
query uses output rowsets, has [first n] + ORDER BY, then the FirstN is still
added in the Generator. I took a look at fixing this one too, but it proved
more tedious than I wanted to tackle just now. A follow-on JIRA,
TRAFODION-2924, has been written to track that case.
In addition, I noticed some code paths that use [first n] today that do not
work correctly. The parser supports [first n] syntax on UPDATE and DELETE
statements. But I discovered (even before my changes) that UPDATE ignores its
[first n] specification. And if [first n] is specified for DELETE, the query
fails with a compiler error 2235 (cannot produce plan). So those evidently are
incomplete features, or features that broke sometime in the past and no-one
noticed until now.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/DaveBirdsall/trafodion Trafodion2840
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/trafodion/pull/1414.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #1414
----
commit ebf7283de994729f898fa5a9f5e476fa03b40a4f
Author: Dave Birdsall <dbirdsall@...>
Date: 2018-01-25T00:16:08Z
[TRAFODION-2840] Make [first n] with ORDER BY views non-updatable
----
---