Search in sources :

Example 1 with AttributeIndexOptions

use of org.locationtech.geowave.core.store.index.AttributeIndexOptions in project geowave by locationtech.

the class ExpressionQueryIT method testIndexSelection.

@Test
public void testIndexSelection() {
    final DataStore ds = dataStore.createDataStore();
    final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
    final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
    final Index spatialTemporalIndex = SpatialTemporalDimensionalityTypeProvider.createIndexFromOptions(new SpatialTemporalOptions());
    final Index temporalIndex = TemporalDimensionalityTypeProvider.createIndexFromOptions(new TemporalOptions());
    ds.addType(adapter, spatialIndex, spatialTemporalIndex, temporalIndex);
    final Index altIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, ALTERNATE_GEOMETRY_FIELD));
    final Index integerIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, INTEGER_FIELD));
    final Index commentIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, COMMENT_FIELD));
    final Index idIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, ID_FIELD));
    final Index latitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LATITUDE_FIELD));
    final Index longitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LONGITUDE_FIELD));
    ds.addIndex(TYPE_NAME, altIndex, integerIndex, commentIndex, idIndex, latitudeIndex, longitudeIndex);
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final AdapterIndexMappingStore aimStore = dataStore.createAdapterIndexMappingStore();
    final IndexStore indexStore = dataStore.createIndexStore();
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final InternalDataAdapter<?> internalAdapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(TYPE_NAME));
    // Ingest data
    ingestData(ds);
    // ///////////////////////////////////////////////////
    // Basic BBOX on Alternate Geometry
    // ///////////////////////////////////////////////////
    Query<SimpleFeature> query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, 0.5, 0.5, 64.5)).build();
    QueryConstraints queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    ExplicitFilteredQuery constraints = (ExplicitFilteredQuery) queryConstraints;
    List<QueryFilter> filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    Filter filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof BBox);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 4, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Both bbox and comment are indexed, but comment
    // should result in fewer rows queried so that
    // should be the selected index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, -32.5, 32.5, 64.5).and(COMMENT.startsWith("b", true))).build();
    queryConstraints = assertBestIndex(internalAdapter, commentIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof CustomQueryConstraints);
    CustomQueryConstraints<?> customConstraints = (CustomQueryConstraints<?>) queryConstraints;
    filters = customConstraints.createFilters(commentIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Comment predicate was exact so only the bbox filter should need to be performed
    assertTrue(filter instanceof BBox);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        int count = 0;
        while (iterator.hasNext()) {
            final SimpleFeature feature = iterator.next();
            assertEquals("Bravo", feature.getAttribute(COMMENT_FIELD));
            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);
    }
    // ///////////////////////////////////////////////////
    // Both bbox and comment are indexed, but bbox should
    // result in fewer rows queried so that should be the
    // selected index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, 32.5, -32.5, 64.5).and(COMMENT.startsWith("b", true))).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // bbox is not exact, so it will still be part of the filter
    assertTrue(filter instanceof And);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 16, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Latitude is the most constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LATITUDE.isGreaterThan(5).and(LATITUDE.isLessThan(10), LONGITUDE.isGreaterThanOrEqualTo(7))).build();
    queryConstraints = assertBestIndex(internalAdapter, latitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(latitudeIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // since the latitude fields are exact, only the longitude needs to be filtered later
    assertTrue(filter instanceof ComparisonOperator);
    Set<String> referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(LONGITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(3, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Longitude is the most constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LONGITUDE.isGreaterThanOrEqualTo(5).and(LONGITUDE.isLessThan(10).or(LATITUDE.isLessThanOrEqualTo(15)))).build();
    queryConstraints = assertBestIndex(internalAdapter, longitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(longitudeIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // The second half of the expression cannot be predetermined, so both sides of the Or need to be
    // present
    assertTrue(filter instanceof Or);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(LATITUDE_FIELD));
    assertTrue(referencedFields.contains(LONGITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(11, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Longitude is an exact range
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LONGITUDE.isLessThan(-31.5).or(LONGITUDE.isGreaterThan(31.5))).build();
    queryConstraints = assertBestIndex(internalAdapter, longitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(longitudeIndex);
    // The constraints are exact, so there shouldn't be any additional filtering
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Default geom only should select spatial index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(0.5, 0.5, 10.5, 10.5)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // BBox isn't exact, so it should still be filtered
    assertTrue(filter instanceof BBox);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(10, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Default geom and time should select spatial-
    // temporal index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(0.5, 0.5, 30.5, 30.5).and(TIMESTAMP.isBefore(new Date((long) (66 * ONE_DAY_MILLIS + 1))))).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialTemporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialTemporalIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // BBox isn't exact, and neither is timestamp in a binned temporal index
    assertTrue(filter instanceof And);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    assertTrue(referencedFields.contains(TIMESTAMP_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Only timestamp should use temporal index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isBefore(new Date((long) (66 * ONE_DAY_MILLIS + 1)))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Timestamp is not exact on temporal index because there could be ranges
    assertTrue(filter instanceof Before);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(TIMESTAMP_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(67, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Integer is more constraining, half of the ID
    // values end with 0
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ID.endsWith("0").and(INTEGER.isBetween(10, 20))).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Integer is exact, so only the string predicate should remain
    assertTrue(filter instanceof EndsWith);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(ID_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(6, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // ID is more constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ID.endsWith("a0").and(INTEGER.isBetween(0, 40))).build();
    queryConstraints = assertBestIndex(internalAdapter, idIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof CustomQueryConstraints);
    customConstraints = (CustomQueryConstraints<?>) queryConstraints;
    filters = customConstraints.createFilters(commentIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // ID constraint is exact, so only the integer predicate should remain
    assertTrue(filter instanceof Between);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(INTEGER_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Alternate geometry is 50% null, so it is more
    // constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(-30.5, -30.5, 30.5, 30.5).and(ALT.bbox(-30.5, -30.5, 30.5, 30.5))).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Neither bbox is exact, so they will both be filtered
    assertTrue(filter instanceof And);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    assertTrue(referencedFields.contains(ALTERNATE_GEOMETRY_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(30, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Integer is more constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isLessThan(-60).and(LATITUDE.isLessThan(5))).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Neither bbox is exact, so they will both be filtered
    assertTrue(filter instanceof ComparisonOperator);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(LATITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(4, Iterators.size(iterator));
    }
}
Also used : Before(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.Before) ComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.ComparisonOperator) Or(org.locationtech.geowave.core.store.query.filter.expression.Or) Index(org.locationtech.geowave.core.store.api.Index) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) LineString(org.locationtech.jts.geom.LineString) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) DataStore(org.locationtech.geowave.core.store.api.DataStore) ExplicitFilteredQuery(org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) TemporalBetween(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalBetween) Between(org.locationtech.geowave.core.store.query.filter.expression.Between) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Point(org.locationtech.jts.geom.Point) Disjoint(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Disjoint) Date(java.util.Date) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) BBox(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.BBox) And(org.locationtech.geowave.core.store.query.filter.expression.And) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) TemporalOptions(org.locationtech.geowave.core.geotime.index.TemporalOptions) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) EndsWith(org.locationtech.geowave.core.store.query.filter.expression.text.EndsWith) Test(org.junit.Test)

Example 2 with AttributeIndexOptions

use of org.locationtech.geowave.core.store.index.AttributeIndexOptions in project geowave by locationtech.

the class ExpressionQueryIT 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, ALTERNATE_GEOMETRY_FIELD));
    final Index polyIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, POLYGON_FIELD));
    ds.addIndex(TYPE_NAME, altIndex, polyIndex);
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final AdapterIndexMappingStore aimStore = dataStore.createAdapterIndexMappingStore();
    final IndexStore indexStore = dataStore.createIndexStore();
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final InternalDataAdapter<?> internalAdapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(TYPE_NAME));
    final Polygon 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) });
    final Polygon 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) });
    // Large diagonal line
    final LineString line = GeometryUtils.GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(-20.5, -20.5), new Coordinate(20.5, 20.5) });
    // Ingest data
    ingestData(ds);
    // ///////////////////////////////////////////////////
    // Basic BBOX
    // ///////////////////////////////////////////////////
    Query<SimpleFeature> query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(0.5, 0.5, 64.5, 64.5)).build();
    QueryConstraints queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    ExplicitFilteredQuery constraints = (ExplicitFilteredQuery) queryConstraints;
    List<QueryFilter> filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    Filter filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof BBox);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Loose BBOX
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bboxLoose(0.5, 0.5, 64.5, 64.5)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Intersects
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.intersects(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Intersects);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(20, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Loose Intersects
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.intersectsLoose(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(20, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Disjoint
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.disjoint(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof FilteredEverythingQuery);
    FilteredEverythingQuery everything = (FilteredEverythingQuery) queryConstraints;
    filters = everything.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Disjoint);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES - 41, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Loose Disjoint
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.disjointLoose(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof FilteredEverythingQuery);
    everything = (FilteredEverythingQuery) queryConstraints;
    filters = everything.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Disjoint);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES - 41, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Crosses
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(POLY.crosses(line)).build();
    queryConstraints = assertBestIndex(internalAdapter, polyIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(polyIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Crosses);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(43, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Overlaps
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(POLY.overlaps(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, polyIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(polyIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Overlaps);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        // it overlaps 2 polygons in each corner
        assertEquals(4, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Contains
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.contains(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof SpatialContains);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertFalse(iterator.hasNext());
    }
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(SpatialLiteral.of(boxPoly).contains(GEOM)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof SpatialContains);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(41, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Touches
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(POLY.touches(boxPoly2)).build();
    queryConstraints = assertBestIndex(internalAdapter, polyIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(polyIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Touches);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Within
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.within(boxPoly)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Within);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(41, Iterators.size(iterator));
    }
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(SpatialLiteral.of(boxPoly).within(GEOM)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Within);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertFalse(iterator.hasNext());
    }
    // ///////////////////////////////////////////////////
    // EqualTo
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.isEqualTo(GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(1, 1)))).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof SpatialEqualTo);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // NotEqualTo
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.isNotEqualTo(GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(1, 1)))).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof FilteredEverythingQuery);
    everything = (FilteredEverythingQuery) queryConstraints;
    filters = everything.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof SpatialNotEqualTo);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES - 1, Iterators.size(iterator));
    }
}
Also used : TimeOverlaps(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TimeOverlaps) Overlaps(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Overlaps) Index(org.locationtech.geowave.core.store.api.Index) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) Crosses(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Crosses) DataStore(org.locationtech.geowave.core.store.api.DataStore) Intersects(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Intersects) Disjoint(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Disjoint) Touches(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Touches) FilteredEverythingQuery(org.locationtech.geowave.core.store.query.constraints.FilteredEverythingQuery) Polygon(org.locationtech.jts.geom.Polygon) ExplicitFilteredQuery(org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) SpatialNotEqualTo(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.SpatialNotEqualTo) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) SpatialEqualTo(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.SpatialEqualTo) SpatialContains(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.SpatialContains) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) BBox(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.BBox) Within(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Within) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) Test(org.junit.Test)

Example 3 with AttributeIndexOptions

use of org.locationtech.geowave.core.store.index.AttributeIndexOptions in project geowave by locationtech.

the class ExpressionQueryIT 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_FIELD));
    final Index latitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LATITUDE_FIELD));
    final Index longitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LONGITUDE_FIELD));
    ds.addIndex(TYPE_NAME, integerIndex, latitudeIndex, longitudeIndex);
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final AdapterIndexMappingStore aimStore = dataStore.createAdapterIndexMappingStore();
    final IndexStore indexStore = dataStore.createIndexStore();
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final InternalDataAdapter<?> internalAdapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(TYPE_NAME));
    // Ingest data
    ingestData(ds);
    // ///////////////////////////////////////////////////
    // Greater Than
    // ///////////////////////////////////////////////////
    Query<SimpleFeature> query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isGreaterThan(0)).build();
    QueryConstraints queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    ExplicitFilteredQuery constraints = (ExplicitFilteredQuery) queryConstraints;
    List<QueryFilter> filters = constraints.createFilters(integerIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Less Than
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LATITUDE.isLessThan(0)).build();
    queryConstraints = assertBestIndex(internalAdapter, latitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(latitudeIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Greater Than Or Equal To
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isGreaterThanOrEqualTo(0)).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Less Than
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LONGITUDE.isLessThanOrEqualTo(0)).build();
    queryConstraints = assertBestIndex(internalAdapter, longitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(longitudeIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Equal To
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isEqualTo(12)).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Not Equal To
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isNotEqualTo(12).and(INTEGER.isNotEqualTo(8))).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES - 2, Iterators.size(iterator));
    }
}
Also used : ExplicitFilteredQuery(org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) Index(org.locationtech.geowave.core.store.api.Index) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) DataStore(org.locationtech.geowave.core.store.api.DataStore) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) Test(org.junit.Test)

Example 4 with AttributeIndexOptions

use of org.locationtech.geowave.core.store.index.AttributeIndexOptions 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());
    }
}
Also used : AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) DataStore(org.locationtech.geowave.core.store.api.DataStore) ResultSet(org.locationtech.geowave.core.store.query.gwql.ResultSet) Index(org.locationtech.geowave.core.store.api.Index) Envelope(org.locationtech.jts.geom.Envelope) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) SimpleFeature(org.opengis.feature.simple.SimpleFeature) BigDecimal(java.math.BigDecimal) Point(org.locationtech.jts.geom.Point) Result(org.locationtech.geowave.core.store.query.gwql.Result) Test(org.junit.Test)

Example 5 with AttributeIndexOptions

use of org.locationtech.geowave.core.store.index.AttributeIndexOptions 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));
    }
}
Also used : AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) DataStore(org.locationtech.geowave.core.store.api.DataStore) ResultSet(org.locationtech.geowave.core.store.query.gwql.ResultSet) Index(org.locationtech.geowave.core.store.api.Index) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Aggregations

AttributeIndexOptions (org.locationtech.geowave.core.store.index.AttributeIndexOptions)16 DataStore (org.locationtech.geowave.core.store.api.DataStore)15 Index (org.locationtech.geowave.core.store.api.Index)15 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 Test (org.junit.Test)14 SpatialOptions (org.locationtech.geowave.core.geotime.index.SpatialOptions)14 AdapterIndexMappingStore (org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore)10 InternalAdapterStore (org.locationtech.geowave.core.store.adapter.InternalAdapterStore)10 Filter (org.locationtech.geowave.core.store.query.filter.expression.Filter)7 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)6 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)6 CustomQueryConstraints (org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints)6 QueryConstraints (org.locationtech.geowave.core.store.query.constraints.QueryConstraints)6 ExpressionQueryFilter (org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter)6 QueryFilter (org.locationtech.geowave.core.store.query.filter.QueryFilter)6 DataStatisticsStore (org.locationtech.geowave.core.store.statistics.DataStatisticsStore)6 ExplicitFilteredQuery (org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery)5 ResultSet (org.locationtech.geowave.core.store.query.gwql.ResultSet)4 Date (java.util.Date)3 AdapterToIndexMapping (org.locationtech.geowave.core.store.AdapterToIndexMapping)3