This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch feat/featureset-subset-abstract in repository https://gitbox.apache.org/repos/asf/sis.git
commit a34b9e86f27f254277f389785c4b9914bc3e1e3d Author: jsorel <johann.so...@geomatys.com> AuthorDate: Mon Oct 19 14:04:23 2020 +0200 FeatureSet : support simple query using a property unknowned by the feature type --- .../java/org/apache/sis/filter/PropertyValue.java | 8 +++++- .../org/apache/sis/storage/FeatureQueryTest.java | 33 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/PropertyValue.java b/core/sis-feature/src/main/java/org/apache/sis/filter/PropertyValue.java index 89714ff..1638c07 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/filter/PropertyValue.java +++ b/core/sis-feature/src/main/java/org/apache/sis/filter/PropertyValue.java @@ -303,7 +303,13 @@ abstract class PropertyValue<V> extends LeafExpression<Feature,V> implements Val */ @Override public PropertyTypeBuilder expectedType(final FeatureType valueType, final FeatureTypeBuilder addTo) { - PropertyType type = valueType.getProperty(name); // May throw IllegalArgumentException. + PropertyType type; + try { + type = valueType.getProperty(name); + } catch (IllegalArgumentException ex) { + // the property does not exist but may be defined on a yet unknown child type. + return addTo.addAttribute(Object.class).setName(name).setMinimumOccurs(0); + } while (type instanceof Operation) { final IdentifiedType result = ((Operation) type).getResult(); if (result != type && result instanceof PropertyType) { diff --git a/storage/sis-storage/src/test/java/org/apache/sis/storage/FeatureQueryTest.java b/storage/sis-storage/src/test/java/org/apache/sis/storage/FeatureQueryTest.java index 7f6af73..3c0516c 100644 --- a/storage/sis-storage/src/test/java/org/apache/sis/storage/FeatureQueryTest.java +++ b/storage/sis-storage/src/test/java/org/apache/sis/storage/FeatureQueryTest.java @@ -233,4 +233,37 @@ public final strictfp class FeatureQueryTest extends TestCase { assertEquals("Unnamed #2", properties.next().getName().toString()); assertFalse(properties.hasNext()); } + + /** + * Tests {@link FeatureQuery#setProjection(FeatureQuery.NamedExpression...)} on an abstract feature type. + * We expect the column to be defined even if the property name is undefined on the feature type. + * This case happens when the {@link FeatureSet} contains features with inherited types. + * + * @throws DataStoreException if an error occurred while executing the query. + */ + @Test + public void testColumnsAbstractType() throws DataStoreException { + final FilterFactory<Feature,?,?> ff = DefaultFilterFactory.forFeatures(); + query.setProjection(new FeatureQuery.NamedExpression(ff.property("value1"), (String) null), + new FeatureQuery.NamedExpression(ff.property("unknown"), "unexpected")); + query.setLimit(1); + + final FeatureSet fs = query.execute(featureSet); + final Feature result = TestUtilities.getSingleton(fs.features(false).collect(Collectors.toList())); + + // Check result type. + final FeatureType resultType = result.getType(); + assertEquals("Test", resultType.getName().toString()); + assertEquals(2, resultType.getProperties(true).size()); + final PropertyType pt1 = resultType.getProperty("value1"); + final PropertyType pt2 = resultType.getProperty("unexpected"); + assertTrue(pt1 instanceof AttributeType); + assertTrue(pt2 instanceof AttributeType); + assertEquals(Integer.class, ((AttributeType) pt1).getValueClass()); + assertEquals(Object.class, ((AttributeType) pt2).getValueClass()); + + // Check feature property values. + assertEquals(3, result.getPropertyValue("value1")); + assertEquals(null, result.getPropertyValue("unexpected")); + } }