-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3817 from katzyn/any_value
Add ANY_VALUE() aggregate function
- Loading branch information
Showing
9 changed files
with
239 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
h2/src/main/org/h2/expression/aggregate/AggregateDataAnyValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, | ||
* and the EPL 1.0 (https://h2database.com/html/license.html). | ||
* Initial Developer: H2 Group | ||
*/ | ||
package org.h2.expression.aggregate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
import org.h2.engine.SessionLocal; | ||
import org.h2.value.Value; | ||
import org.h2.value.ValueNull; | ||
|
||
/** | ||
* Data stored while calculating an ANY_VALUE aggregate. | ||
*/ | ||
final class AggregateDataAnyValue extends AggregateData { | ||
|
||
private static final int MAX_VALUES = 256; | ||
|
||
ArrayList<Value> values = new ArrayList<>(); | ||
|
||
private long filter = -1L; | ||
|
||
/** | ||
* Creates new instance of data for ANY_VALUE. | ||
*/ | ||
AggregateDataAnyValue() { | ||
} | ||
|
||
@Override | ||
void add(SessionLocal session, Value v) { | ||
if (v == ValueNull.INSTANCE) { | ||
return; | ||
} | ||
long filter = this.filter; | ||
if (filter == Long.MIN_VALUE || (session.getRandom().nextLong() | filter) == filter) { | ||
values.add(v); | ||
if (values.size() == MAX_VALUES) { | ||
compact(session); | ||
} | ||
} | ||
} | ||
|
||
private void compact(SessionLocal session) { | ||
filter <<= 1; | ||
Random random = session.getRandom(); | ||
for (int s = 0, t = 0; t < MAX_VALUES / 2; s += 2, t++) { | ||
int idx = s; | ||
if (random.nextBoolean()) { | ||
idx++; | ||
} | ||
values.set(t, values.get(idx)); | ||
} | ||
values.subList(MAX_VALUES / 2, MAX_VALUES).clear(); | ||
} | ||
|
||
@Override | ||
Value getValue(SessionLocal session) { | ||
int count = values.size(); | ||
if (count == 0) { | ||
return ValueNull.INSTANCE; | ||
} | ||
return values.get(session.getRandom().nextInt(count)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.