| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.condition; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
|
| 10 |
import org.h2.command.query.Query; |
| 11 |
import org.h2.engine.NullsDistinct; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.expression.Expression; |
| 14 |
import org.h2.expression.ValueExpression; |
| 15 |
import org.h2.result.LocalResult; |
| 16 |
import org.h2.result.ResultTarget; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueBoolean; |
| 19 |
import org.h2.value.ValueNull; |
| 20 |
|
| 21 |
/** |
| 22 |
* Unique predicate as in UNIQUE(SELECT ...) |
| 23 |
*/ |
| 24 |
public class UniquePredicate extends PredicateWithSubquery { |
| 25 |
|
| 26 |
private static final class Target implements ResultTarget { |
| 27 |
|
| 28 |
private final int columnCount; |
| 29 |
|
| 30 |
private final NullsDistinct nullsDistinct; |
| 31 |
|
| 32 |
private final LocalResult result; |
| 33 |
|
| 34 |
boolean hasDuplicates; |
| 35 |
|
| 36 |
Target(int columnCount, NullsDistinct nullsDistinct, LocalResult result) { |
| 37 |
this.columnCount = columnCount; |
| 38 |
this.nullsDistinct = nullsDistinct; |
| 39 |
this.result = result; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void limitsWereApplied() { |
| 44 |
// Nothing to do |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public long getRowCount() { |
| 49 |
// Not required |
| 50 |
return 0L; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void addRow(Value... values) { |
| 55 |
if (hasDuplicates) { |
| 56 |
return; |
| 57 |
} |
| 58 |
check: switch (nullsDistinct) { |
| 59 |
case DISTINCT: |
| 60 |
for (int i = 0; i < columnCount; i++) { |
| 61 |
if (values[i] == ValueNull.INSTANCE) { |
| 62 |
return; |
| 63 |
} |
| 64 |
} |
| 65 |
break; |
| 66 |
case ALL_DISTINCT: |
| 67 |
for (int i = 0; i < columnCount; i++) { |
| 68 |
if (values[i] != ValueNull.INSTANCE) { |
| 69 |
break check; |
| 70 |
} |
| 71 |
} |
| 72 |
return; |
| 73 |
default: |
| 74 |
} |
| 75 |
if (values.length != columnCount) { |
| 76 |
values = Arrays.copyOf(values, columnCount); |
| 77 |
} |
| 78 |
long expected = result.getRowCount() + 1; |
| 79 |
result.addRow(values); |
| 80 |
if (expected != result.getRowCount()) { |
| 81 |
hasDuplicates = true; |
| 82 |
result.close(); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private final NullsDistinct nullsDistinct; |
| 88 |
|
| 89 |
public UniquePredicate(Query query, NullsDistinct nullsDistinct) { |
| 90 |
super(query); |
| 91 |
this.nullsDistinct = nullsDistinct; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public Expression optimize(SessionLocal session) { |
| 96 |
super.optimize(session); |
| 97 |
if (query.isStandardDistinct()) { |
| 98 |
return ValueExpression.TRUE; |
| 99 |
} |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public Value getValue(SessionLocal session) { |
| 105 |
query.setSession(session); |
| 106 |
int columnCount = query.getColumnCount(); |
| 107 |
LocalResult result = new LocalResult(session, |
| 108 |
query.getExpressions().toArray(new Expression[0]), columnCount, columnCount); |
| 109 |
result.setDistinct(); |
| 110 |
Target target = new Target(columnCount, nullsDistinct, result); |
| 111 |
query.query(Integer.MAX_VALUE, target); |
| 112 |
result.close(); |
| 113 |
return ValueBoolean.get(!target.hasDuplicates); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 118 |
builder.append("UNIQUE"); |
| 119 |
if (nullsDistinct != NullsDistinct.DISTINCT) { |
| 120 |
nullsDistinct.getSQL(builder.append(' '), 0); |
| 121 |
} |
| 122 |
return super.getUnenclosedSQL(builder, sqlFlags); |
| 123 |
} |
| 124 |
|
| 125 |
} |