[ 
https://issues.apache.org/jira/browse/CALCITE-4486?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17382410#comment-17382410
 ] 

duan xiong commented on CALCITE-4486:
-------------------------------------

According to jooq's 
[unique-predicate|https://www.jooq.org/doc/3.0/manual/sql-building/conditional-expressions/unique-predicate/],
 we can implement the UNIQUE predicate by a convert to EXISTS in 
SubQueryRemoveRule rewriteUnique method.

For example:
UNIQUE (SELECT PUBLISHED_IN FROM BOOK            WHERE AUTHOR_ID = 3)

{code:java}
NOT EXISTS (
  SELECT 1 FROM (
    SELECT PUBLISHED_IN
    FROM BOOK
    WHERE AUTHOR_ID = 3
  ) T
  WHERE (T.PUBLISHED_IN) IS NOT NULL
  GROUP BY T.PUBLISHED_IN
  HAVING COUNT(*) > 1
){code}
But When have more than 1 field in the subquery, How to handle it?

Because we can group by like this: 
{code:java}
GROUP BY $1,$2{code}
But how to add the filter condition(connect each field condition by AND 
operator?) like  this:
{code:java}
WHERE ($1) IS NOT NULL and ($2) IS NOT NULL{code}
I need some advice about this or confirm this design is right.

> UNIQUE predicate
> ----------------
>
>                 Key: CALCITE-4486
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4486
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Julian Hyde
>            Assignee: duan xiong
>            Priority: Major
>
> Implement the UNIQUE predicate, as specified by the SQL standard.
> Example:
> {code:java}
> UNIQUE (SELECT publishedIn FROM Book
>             WHERE authorId = 3)
> NOT UNIQUE (SELECT publishedIn FROM BOOK
>             WHERE authorId = 3)
> {code}
> {{UNIQUE}} returns {{FALSE}} if the query returns two or more rows that do 
> not include NULL values and are equal. Like {{EXISTS}}, it never returns 
> {{UNKNOWN}}.
> More details at 
> [JOOQ|https://www.jooq.org/doc/3.0/manual/sql-building/conditional-expressions/unique-predicate/].



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to