Suggestions for implementing IS DISTINCT FROM?
Suggestions for implementing IS DISTINCT FROM?
От:
Thomas Lockhart <lockhart@fourpalms.org>
Дата:
I'm looking at implementing IS DISTINCT FROM, among other things. It has
the unusual behavior that it compares elements for a tuple by
considering two NULLs to be equal (hence non-distinct) rather than
"unknown". So the rules for comparison seem to be:
a) if the rows compared have different lengths, they are distinct
b) if the rows are both zero-length, they are not distinct
c) otherwise, each element in the row (or a single value on each side of
the comparison) are compared pairwise, with1) if both elements are NULL, they are not distinct2) if one element of the pair is NULL, they are distinct3) if both elements are NOT NULL and are equal, they are not distinct4) if no pair of elements is distinct, the rows are not distinct5) otherwise, the rows are distinct
I was thinking to implement this by simply expanding these rules within
gram.y to be a tree of comparison tests. But I think that this does not
generalize properly into allowing tuples or rows to be supplied by
subqueries or other non-literal tuples. So, I'm looking for suggestions
on how to go about implementing this. Should I define a new comparison
node like the AND expression which can directly handle the NULL
behaviors correctly? That would require at least minor changes in the
optimizer and executor. Does another approach come to mind (esp. one
which works ;)?
TIA
- Thomas
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Thomas Lockhart writes: > I'm looking at implementing IS DISTINCT FROM, among other things. > ... > I was thinking to implement this by simply expanding these rules within > gram.y to be a tree of comparison tests. Please, please, do not do that. Make a new expression node tree type, instead. We've made this mistake before (eg for BETWEEN) and I don't want to do it again. Aside from the points you make, a direct expansion approach cannot reverse-list properly in rules/views, and it will force multiple evaluations of arguments that should not be multiply evaluated. Adding a new expression node tree type is not too difficult these days; see for example Joe Conway's recent NullTest and BooleanTest additions. I believe the existing expansions of row comparison operators (makeRowExpr) should be replaced by specialized nodes, too. That would give us a shot at implementing row '<', '>' comparisons in a spec-compliant fashion... regards, tom lane
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Thomas Lockhart <lockhart@fourpalms.org>
Дата:
> I've actually already done almost all the work for converting BETWEEN to a
> node but I have a couple of questions:
> Should I use a boolean in the node to indicate whether it is SYMMETRIC or
> ASYMMETRIC, or should I use some sort of integer to indicate whether it is
> SYMMETRIC, ASYMMETRIC or DEFAULT (ASYMMETRIC). That way the reverse in
> rules and views could leave out the ASYMMETRIC if it wasn't specified
> originally, rather than always adding it in. Which is better?
Great!
I would use a boolean (or integer) to indicate two possibilities, not
three.
The language specifies what the default should be, and dump programs
could choose to omit the ASYMMETRIC if they choose. imho it is best to
resolve defaults earlier, rather than pushing the resolution deep into
the parser or even farther.
- Thomas
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Thomas Lockhart <lockhart@fourpalms.org>
Дата:
> > I'm looking at implementing IS DISTINCT FROM, among other things.
> > ...
> > I was thinking to implement this by simply expanding these rules within
> > gram.y to be a tree of comparison tests.
> Please, please, do not do that. Make a new expression node tree type,
> instead. We've made this mistake before (eg for BETWEEN) and I don't
> want to do it again.
Uh, sure. If you don't quote out of context I think it is pretty clear
that I was looking for a helpful suggestion to do just that. Thanks,
I'll proceed with the assurance that you won't object to *that* too ;)
- Thomas
Re: Suggestions for implementing IS DISTINCT FROM?
От:
"Christopher Kings-Lynne" <chriskl@familyhealth.com.au>
Дата:
> Please, please, do not do that. Make a new expression node tree type, > instead. We've made this mistake before (eg for BETWEEN) and I don't > want to do it again. I've actually already done almost all the work for converting BETWEEN to a node but I have a couple of questions: Should I use a boolean in the node to indicate whether it is SYMMETRIC or ASYMMETRIC, or should I use some sort of integer to indicate whether it is SYMMETRIC, ASYMMETRIC or DEFAULT (ASYMMETRIC). That way the reverse in rules and views could leave out the ASYMMETRIC if it wasn't specified originally, rather than always adding it in. Which is better? Chris
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Christopher Kings-Lynne" writes: > Should I use a boolean in the node to indicate whether it is SYMMETRIC or > ASYMMETRIC, or should I use some sort of integer to indicate whether it is > SYMMETRIC, ASYMMETRIC or DEFAULT (ASYMMETRIC). That way the reverse in > rules and views could leave out the ASYMMETRIC if it wasn't specified > originally, rather than always adding it in. Which is better? My intention is to reverse-list as either "BETWEEN" or "BETWEEN SYMMETRIC". While I believe in reproducing the source text during reverse listing, I don't take it to extremes ;-) So a boolean is sufficient. regards, tom lane
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Thomas Lockhart <lockhart@fourpalms.org>
Дата:
...
> Adding a new expression node tree type is not too difficult these days;
> see for example Joe Conway's recent NullTest and BooleanTest additions.
> I believe the existing expansions of row comparison operators
> (makeRowExpr) should be replaced by specialized nodes, too. That would
> give us a shot at implementing row '<', '>' comparisons in a
> spec-compliant fashion...
OK, now that we are pushing new nodes into the executor with abandon :)
...
Let's talk about the preferred technique for doing so, especially with
row-style argument lists. I want to implement IS NULL for rows also
(actually, already did so using a transformation in gram.y -- no need to
jump on that one Tom ;), and found a comment from Joe on the NullTest
code saying that he wanted to do just that "someday". Should we honk
around the existing NullTest node to handle both lists of arguments and
single arguments, or should we have a completely different set of nodes
for handling row arguments? I'll guess the latter, but we should talk
whether that scales properly, about the preferred style for this new
kind of node, and about how to minimize performance hits which we might
see if we have a large number of new node types being handled by the
executor.
If we extend existing nodes to handle lists, then I might be able to
package the DISTINCT test into an A_Expr node, though I haven't pushed
that all the way through to see for sure.
Using the SubLink node does not seem quite right because IS DISTINCT
FROM does not seem to make sense with an embedded select as one of the
arguments, but maybe it does??
Comments? I'm itchy to code but don't want to waste more time than
necessary heading the wrong direction...
- Thomas
Re: Suggestions for implementing IS DISTINCT FROM?
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Thomas Lockhart writes:
> Let's talk about the preferred technique for doing so, especially with
> row-style argument lists. I want to implement IS NULL for rows also
> (actually, already did so using a transformation in gram.y -- no need to
> jump on that one Tom ;), and found a comment from Joe on the NullTest
> code saying that he wanted to do just that "someday". Should we honk
> around the existing NullTest node to handle both lists of arguments and
> single arguments, or should we have a completely different set of nodes
> for handling row arguments? I'll guess the latter, but we should talk
> whether that scales properly, about the preferred style for this new
> kind of node, and about how to minimize performance hits which we might
> see if we have a large number of new node types being handled by the
> executor.
I have a mild preference for minimizing the number of expression node
types. If we can represent two similar kinds of expressions with one
node type instead of two, we roughly halve the amount of overhead code
needed.
My inclination would be to divvy things up on the basis of the kinds
of things to be compared, so we'd have these classes of nodes:
1. Test a single ; this would cover
::= IS [ NOT ] NULL
2. Compare two ; this would cover
::=
::= IS DISTINCT FROM
(OVERLAPS is a special case that should remain separate, IMHO)
3. Compare three :
::= [ NOT ] BETWEEN [ ASYMMETRIC | SYMMETRIC ] AND
4. Compare a to a list of :
::= [ NOT ] IN
::= { }...
5. Compare a to the outputs of a :
::= [ NOT ] IN