| 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.aggregate; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.expression.ExpressionColumn; |
| 13 |
import org.h2.index.Index; |
| 14 |
import org.h2.mvstore.db.MVSpatialIndex; |
| 15 |
import org.h2.table.Column; |
| 16 |
import org.h2.table.TableFilter; |
| 17 |
import org.h2.util.geometry.GeometryUtils; |
| 18 |
import org.h2.value.Value; |
| 19 |
import org.h2.value.ValueGeometry; |
| 20 |
import org.h2.value.ValueNull; |
| 21 |
|
| 22 |
/** |
| 23 |
* Data stored while calculating an aggregate. |
| 24 |
*/ |
| 25 |
final class AggregateDataEnvelope extends AggregateData { |
| 26 |
|
| 27 |
private double[] envelope; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get the index (if any) for the column specified in the geometry |
| 31 |
* aggregate. |
| 32 |
* |
| 33 |
* @param on |
| 34 |
* the expression (usually a column expression) |
| 35 |
* @return the index, or null |
| 36 |
*/ |
| 37 |
static Index getGeometryColumnIndex(Expression on) { |
| 38 |
if (on instanceof ExpressionColumn) { |
| 39 |
ExpressionColumn col = (ExpressionColumn) on; |
| 40 |
Column column = col.getColumn(); |
| 41 |
if (column.getType().getValueType() == Value.GEOMETRY) { |
| 42 |
TableFilter filter = col.getTableFilter(); |
| 43 |
if (filter != null) { |
| 44 |
ArrayList<Index> indexes = filter.getTable().getIndexes(); |
| 45 |
if (indexes != null) { |
| 46 |
for (int i = 1, size = indexes.size(); i < size; i++) { |
| 47 |
Index index = indexes.get(i); |
| 48 |
if (index instanceof MVSpatialIndex && index.isFirstColumn(column)) { |
| 49 |
return index; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
void add(SessionLocal session, Value v) { |
| 61 |
if (v == ValueNull.INSTANCE) { |
| 62 |
return; |
| 63 |
} |
| 64 |
envelope = GeometryUtils.union(envelope, v.convertToGeometry(null).getEnvelopeNoCopy()); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
Value getValue(SessionLocal session) { |
| 69 |
return ValueGeometry.fromEnvelope(envelope); |
| 70 |
} |
| 71 |
|
| 72 |
} |