use of org.locationtech.geowave.core.store.query.gwql.ResultSet in project geowave by locationtech.
the class GeoWaveQueryLanguageIT method testAggregations.
@Test
public void testAggregations() {
final DataStore ds = dataStore.createDataStore();
final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
ds.addType(adapter, spatialIndex);
final Index latitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LATITUDE));
ds.addIndex(TYPE_NAME, latitudeIndex);
// Ingest data
ingestData(ds);
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT BBOX(%s) FROM %s", ALT, TYPE_NAME))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Envelope.class.isAssignableFrom(results.columnType(0)));
assertEquals(new Envelope(-63, 63, -63, 63), results.next().columnValue(0));
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT BBOX(%s) FROM %s WHERE %s > 0", ALT, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Envelope.class.isAssignableFrom(results.columnType(0)));
assertEquals(new Envelope(1, 63, -63, -1), results.next().columnValue(0));
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT COUNT(%s) FROM %s", ALT, TYPE_NAME))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Long.class.isAssignableFrom(results.columnType(0)));
assertEquals(TOTAL_FEATURES / 2, ((Long) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT COUNT(%s) FROM %s WHERE %s > 0", ALT, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Long.class.isAssignableFrom(results.columnType(0)));
assertEquals(TOTAL_FEATURES / 4, ((Long) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT SUM(%s) FROM %s", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
assertEquals(-64, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT SUM(%s) FROM %s WHERE %s > 0", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
int expected = 0;
for (int i = 1; i < TOTAL_FEATURES / 2; i++) {
expected += i;
}
assertEquals(expected, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT MIN(%s) FROM %s", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
assertEquals(-64, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT MIN(%s) FROM %s WHERE %s > 0", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
assertEquals(1, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT MAX(%s) FROM %s", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
assertEquals(63, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT MAX(%s) FROM %s WHERE %s < 0", INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertTrue(results.hasNext());
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(0)));
assertEquals(-1, ((BigDecimal) results.next().columnValue(0)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT BBOX(%s), MIN(%s), MAX(%s), SUM(%s) FROM %s", ALT, INTEGER, INTEGER, INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(4, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Envelope.class.isAssignableFrom(results.columnType(0)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(1)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(2)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(3)));
final Result next = results.next();
assertEquals(new Envelope(-63, 63, -63, 63), next.columnValue(0));
assertEquals(-64, ((BigDecimal) next.columnValue(1)).intValue());
assertEquals(63, ((BigDecimal) next.columnValue(2)).intValue());
assertEquals(-64, ((BigDecimal) next.columnValue(3)).intValue());
assertFalse(results.hasNext());
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT BBOX(%s), MIN(%s), MAX(%s), SUM(%s) FROM %s WHERE %s > 0", ALT, INTEGER, INTEGER, INTEGER, TYPE_NAME, LATITUDE))) {
assertEquals(4, results.columnCount());
assertTrue(results.hasNext());
assertTrue(Envelope.class.isAssignableFrom(results.columnType(0)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(1)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(2)));
assertTrue(BigDecimal.class.isAssignableFrom(results.columnType(3)));
final Result next = results.next();
assertEquals(new Envelope(1, 63, -63, -1), next.columnValue(0));
assertEquals(1, ((BigDecimal) next.columnValue(1)).intValue());
assertEquals(63, ((BigDecimal) next.columnValue(2)).intValue());
int expected = 0;
for (int i = 1; i < TOTAL_FEATURES / 2; i++) {
expected += i;
}
assertEquals(expected, ((BigDecimal) next.columnValue(3)).intValue());
assertFalse(results.hasNext());
}
}
use of org.locationtech.geowave.core.store.query.gwql.ResultSet in project geowave by locationtech.
the class GeoWaveQueryLanguageIT method testNumericExpressionQueries.
@Test
public void testNumericExpressionQueries() {
final DataStore ds = dataStore.createDataStore();
final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
ds.addType(adapter, spatialIndex);
final Index integerIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, INTEGER));
final Index latitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LATITUDE));
final Index longitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LONGITUDE));
ds.addIndex(TYPE_NAME, integerIndex, latitudeIndex, longitudeIndex);
// Ingest data
ingestData(ds);
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s > 0", INTEGER, TYPE_NAME, INTEGER))) {
assertEquals(1, results.columnCount());
assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s < 0", LATITUDE, TYPE_NAME, LATITUDE))) {
assertEquals(1, results.columnCount());
assertEquals(TOTAL_FEATURES / 2, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s >= 0", INTEGER, TYPE_NAME, INTEGER))) {
assertEquals(1, results.columnCount());
assertEquals(TOTAL_FEATURES / 2, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s <= 0", LONGITUDE, TYPE_NAME, LONGITUDE))) {
assertEquals(1, results.columnCount());
assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s = 12", INTEGER, TYPE_NAME, INTEGER))) {
assertEquals(1, results.columnCount());
assertEquals(1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s FROM %s WHERE %s <> 12 AND %s <> 8", INTEGER, TYPE_NAME, INTEGER, INTEGER))) {
assertEquals(1, results.columnCount());
assertEquals(TOTAL_FEATURES - 2, Iterators.size(results));
}
}
use of org.locationtech.geowave.core.store.query.gwql.ResultSet in project geowave by locationtech.
the class GWQLQuery method execute.
@Override
public void execute(final OperationParams params) {
if (debug) {
Configurator.setLevel(LogManager.getRootLogger().getName(), Level.DEBUG);
}
// Ensure we have all the required arguments
if (parameters.size() != 2) {
throw new ParameterException("Requires arguments: <store name> <query>");
}
final String storeName = parameters.get(0);
// Attempt to load store.
final DataStorePluginOptions inputStoreOptions = CLIUtils.loadStore(storeName, getGeoWaveConfigFile(params), params.getConsole());
final String query = parameters.get(1);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final ResultSet results = inputStoreOptions.createDataStore().query(query, authorizations.toArray(new String[authorizations.size()]));
stopWatch.stop();
output.output(results);
results.close();
if (debug) {
LOGGER.debug("Executed query in " + stopWatch.toString());
}
}
use of org.locationtech.geowave.core.store.query.gwql.ResultSet in project geowave by locationtech.
the class SelectStatement method execute.
@Override
public ResultSet execute(final String... authorizations) {
final String typeName = adapter.getTypeName();
if (isAggregation()) {
final AggregationQueryBuilder<PersistableList, List<Object>, T, ?> bldr = AggregationQueryBuilder.newBuilder();
bldr.setAuthorizations(authorizations);
if (filter != null) {
bldr.filter(filter);
}
if (limit != null) {
bldr.limit(limit);
}
final CompositeAggregation<T> composite = new CompositeAggregation<>();
final List<String> columnNames = Lists.newArrayListWithCapacity(selectors.size());
final List<Class<?>> columnTypes = Lists.newArrayListWithCapacity(selectors.size());
for (final Selector selector : selectors) {
final AggregationSelector aggregation = (AggregationSelector) selector;
final AggregationFunction<?> function = GWQLExtensionRegistry.instance().getAggregationFunction(aggregation.functionName());
if (function == null) {
throw new RuntimeException("No aggregation function called '" + aggregation.functionName() + "' was found.");
}
composite.add(function.getAggregation(adapter, aggregation.functionArgs()));
columnNames.add(selector.name());
columnTypes.add(function.getReturnType());
}
bldr.aggregate(typeName, composite);
return new SingletonResultSet(columnNames, columnTypes, dataStore.aggregate(bldr.build()));
} else {
final QueryBuilder<T, ?> bldr = QueryBuilder.newBuilder(adapter.getDataClass()).addTypeName(typeName);
bldr.setAuthorizations(authorizations);
if (filter != null) {
bldr.filter(filter);
}
if ((selectors != null) && !selectors.isEmpty()) {
final Set<String> usedAttributes = Sets.newHashSet();
selectors.forEach(s -> usedAttributes.add(((ColumnSelector) s).columnName()));
if (filter != null) {
filter.addReferencedFields(usedAttributes);
}
for (final String attribute : usedAttributes) {
if (adapter.getFieldDescriptor(attribute) == null) {
throw new RuntimeException("No column named " + attribute + " was found in " + typeName);
}
}
bldr.subsetFields(typeName, usedAttributes.toArray(new String[usedAttributes.size()]));
} else {
selectors = Lists.transform(Arrays.asList(adapter.getFieldDescriptors()), f -> new ColumnSelector(f.fieldName()));
}
if (limit != null) {
bldr.limit(limit);
}
return new AdapterEntryResultSet<>(selectors, adapter, dataStore.query(bldr.build()));
}
}
use of org.locationtech.geowave.core.store.query.gwql.ResultSet in project geowave by locationtech.
the class GeoWaveQueryLanguageIT method testSpatialExpressionQueries.
@Test
public void testSpatialExpressionQueries() {
final DataStore ds = dataStore.createDataStore();
final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
ds.addType(adapter, spatialIndex);
final Index altIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, ALT));
final Index polyIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, POLY));
ds.addIndex(TYPE_NAME, altIndex, polyIndex);
final String boxPoly = GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { new Coordinate(-20.5, -20.5), new Coordinate(-20.5, 20.5), new Coordinate(20.5, 20.5), new Coordinate(20.5, -20.5), new Coordinate(-20.5, -20.5) }).toText();
final String boxPoly2 = GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { new Coordinate(-20, -20), new Coordinate(-20, 20), new Coordinate(20, 20), new Coordinate(20, -20), new Coordinate(-20, -20) }).toText();
// Large diagonal line
final String line = GeometryUtils.GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(-20.5, -20.5), new Coordinate(20.5, 20.5) }).toText();
// Ingest data
ingestData(ds);
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE BBOX(%s, 0.5, 0.5, 64.5, 64.5)", GEOM, POLY, ALT, TYPE_NAME, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE BBOXLOOSE(%s, 0.5, 0.5, 64.5, 64.5)", GEOM, POLY, ALT, TYPE_NAME, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE INTERSECTS(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, ALT, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(20, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE INTERSECTSLOOSE(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, ALT, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(20, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE DISJOINT(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, GEOM, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(TOTAL_FEATURES - 41, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE DISJOINTLOOSE(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, GEOM, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(TOTAL_FEATURES - 41, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE CROSSES(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, POLY, line))) {
assertEquals(3, results.columnCount());
assertEquals(43, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE OVERLAPS(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, POLY, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(4, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE CONTAINS(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, GEOM, boxPoly))) {
assertEquals(3, results.columnCount());
assertFalse(results.hasNext());
}
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE CONTAINS('%s', %s)", GEOM, POLY, ALT, TYPE_NAME, boxPoly, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(41, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE TOUCHES(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, POLY, boxPoly2))) {
assertEquals(3, results.columnCount());
assertEquals(2, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE WITHIN(%s, '%s')", GEOM, POLY, ALT, TYPE_NAME, GEOM, boxPoly))) {
assertEquals(3, results.columnCount());
assertEquals(41, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE %s = 'POINT(1 1)'", GEOM, POLY, ALT, TYPE_NAME, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(1, Iterators.size(results));
}
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE 'POINT(1 1)'::geometry = %s", GEOM, POLY, ALT, TYPE_NAME, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT %s, %s, %s FROM %s WHERE %s <> 'POINT(1 1)'", GEOM, POLY, ALT, TYPE_NAME, GEOM))) {
assertEquals(3, results.columnCount());
assertEquals(TOTAL_FEATURES - 1, Iterators.size(results));
}
}
Aggregations