use of org.locationtech.geowave.core.store.query.gwql.Result 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.Result in project geowave by locationtech.
the class CSVQueryOutputFormat method output.
@Override
public void output(final ResultSet results) {
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile), StringUtils.getGeoWaveCharset())) {
try (CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
final String[] header = new String[results.columnCount()];
for (int i = 0; i < results.columnCount(); i++) {
header[i] = results.columnName(i);
}
printer.printRecord((Object[]) header);
while (results.hasNext()) {
final Result result = results.next();
final Object[] values = new Object[results.columnCount()];
for (int i = 0; i < results.columnCount(); i++) {
values[i] = result.columnValue(i);
}
printer.printRecord(values);
}
}
} catch (IOException e) {
throw new RuntimeException("Error writing CSV: " + e.getMessage(), e);
}
}
use of org.locationtech.geowave.core.store.query.gwql.Result in project geowave by locationtech.
the class ConsoleQueryOutputFormat method getRows.
// Convert to the more generic Object matrix structure for console printing
private List<List<Object>> getRows(final ResultSet results, final int columns) {
List<List<Object>> rows = new ArrayList<List<Object>>();
while (results.hasNext() && rows.size() < RESULTS_PER_PAGE) {
Result result = results.next();
List<Object> values = new ArrayList<Object>(columns);
for (int i = 0; i < columns; i++) {
values.add(result.columnValue(i));
}
rows.add(values);
}
return rows;
}
use of org.locationtech.geowave.core.store.query.gwql.Result in project geowave by locationtech.
the class GeoWaveQueryLanguageIT method testSelectionQueries.
@Test
public void testSelectionQueries() {
final DataStore ds = dataStore.createDataStore();
final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
final Index spatialTemporalIndex = SpatialTemporalDimensionalityTypeProvider.createIndexFromOptions(new SpatialTemporalOptions());
ds.addType(adapter, spatialIndex, spatialTemporalIndex);
// Ingest data
ingestData(ds);
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE BBOX(%s, -64.5, 0.5, 0.5, 64.5)", TYPE_NAME, ALT))) {
assertEquals(9, results.columnCount());
assertEquals(TOTAL_FEATURES / 4, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE BBOX(%s, -64.5, -32.5, 32.5, 64.5) AND strStartsWith(%s, 'b', true)", TYPE_NAME, ALT, COMMENT))) {
int count = 0;
final int commentColumn = results.columnIndex(COMMENT);
while (results.hasNext()) {
final Result result = results.next();
assertEquals("Bravo", result.columnValue(commentColumn));
count++;
}
// 1/4 of entries match the comment predicate, but only 3/4 of those match the bounding box
assertEquals(Math.round(TOTAL_FEATURES / 8 * 1.5), count);
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE %s > 5 AND %s < 10 AND %s >= 7", TYPE_NAME, LATITUDE, LATITUDE, LONGITUDE))) {
assertEquals(9, results.columnCount());
assertEquals(3, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE %s < -31.5 OR %s > 31.5", TYPE_NAME, LONGITUDE, LONGITUDE))) {
assertEquals(9, results.columnCount());
assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE BBOX(%s, 0.5, 0.5, 10.5, 10.5)", TYPE_NAME, GEOM))) {
assertEquals(9, results.columnCount());
assertEquals(10, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE BBOX(%s, 0.5, 0.5, 30.5, 30.5) AND %s BEFORE %d", TYPE_NAME, GEOM, TIMESTAMP, 66 * ONE_DAY_MILLIS + 1))) {
assertEquals(9, results.columnCount());
assertEquals(2, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE %s BEFORE %d", TYPE_NAME, TIMESTAMP, 66 * ONE_DAY_MILLIS + 1))) {
assertEquals(9, results.columnCount());
assertEquals(67, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE strEndsWith(%s, '0') AND %s BETWEEN 10 AND 20", TYPE_NAME, ID, INTEGER))) {
assertEquals(9, results.columnCount());
assertEquals(6, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE strEndsWith(%s, 'a0') AND %s BETWEEN 0 AND 40", TYPE_NAME, ID, INTEGER))) {
assertEquals(9, results.columnCount());
assertEquals(2, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE BBOX(%s, -30.5, -30.5, 30.5, 30.5) AND BBOX(%s, -30.5, -30.5, 30.5, 30.5)", TYPE_NAME, GEOM, ALT))) {
assertEquals(9, results.columnCount());
assertEquals(30, Iterators.size(results));
}
// ///////////////////////////////////////////////////
try (final ResultSet results = ds.query(String.format("SELECT * FROM %s WHERE %s < -60 AND %s < 5", TYPE_NAME, INTEGER, LATITUDE))) {
assertEquals(9, results.columnCount());
assertEquals(4, Iterators.size(results));
}
}
Aggregations