Skip to content

Commit

Permalink
[7.17] Support for Byte and Short as vector tiles value tag (#97619) (#…
Browse files Browse the repository at this point in the history
…97673)

* Port lucene fix github-12352 to Elasticsearch 7.17

* Update docs/changelog/96721.yaml

* Support for Byte and Short as vector tiles value tag (#97619)

Byte and short data types are not supported by the vector tiles specification. If a user tries to add one of those, it
 actually gets ignored. This add s support to those values by casting them as integers. In addition it will throw an
exception if the value added is a type it cannot understand so it is not silently ignored.
# Conflicts:
#	x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/vector-tile/10_basic.yml
  • Loading branch information
iverase committed Jul 14, 2023
1 parent d4069ef commit a5433e3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog/96721.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 96721
summary: Port lucene tessellator fix github-12352 to Elasticsearch 7.17
area: Geo
type: bug
issues: []
6 changes: 6 additions & 0 deletions docs/changelog/97619.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 97619
summary: Support for Byte and Short as vector tiles features
area: Geo
type: bug
issues:
- 97612
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ public static void addPropertyToFeature(VectorTile.Tile.Feature.Builder feature,
// guard for null values
return;
}
if (value instanceof Byte || value instanceof Short) {
// mvt does not support byte and short data types
value = ((Number) value).intValue();
}
feature.addTags(layerProps.addKey(key));
feature.addTags(layerProps.addValue(value));
int valIndex = layerProps.addValue(value);
if (valIndex < 0) {
throw new IllegalArgumentException("Unsupported vector tile type for field [" + key + "] : " + value.getClass().getName());
}
feature.addTags(valIndex);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.vectortile.rest;

import com.wdtinc.mapbox_vector_tile.VectorTile;
import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps;

import org.elasticsearch.test.ESTestCase;

import java.util.stream.StreamSupport;

import static org.hamcrest.Matchers.containsString;

public class VectorTileUtilTests extends ESTestCase {

public void testAddPropertyToFeature() {
final MvtLayerProps layerProps = new MvtLayerProps();
final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder();
// boolean
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "bool", true);
assertPropertyToFeature(layerProps, featureBuilder, 1);
// byte
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "byte", (byte) 1);
assertPropertyToFeature(layerProps, featureBuilder, 2);
// short
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "short", (short) 2);
assertPropertyToFeature(layerProps, featureBuilder, 3);
// integer
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "integer", 3);
assertPropertyToFeature(layerProps, featureBuilder, 4);
// long
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "long", 4L);
assertPropertyToFeature(layerProps, featureBuilder, 5);
// float
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "float", 5f);
assertPropertyToFeature(layerProps, featureBuilder, 6);
// double
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "double", 6d);
assertPropertyToFeature(layerProps, featureBuilder, 7);
// string
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "string", "7");
assertPropertyToFeature(layerProps, featureBuilder, 8);
// invalid
IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "invalid", new Object())
);
assertThat(ex.getMessage(), containsString("Unsupported vector tile type for field [invalid]"));
}

private void assertPropertyToFeature(MvtLayerProps layerProps, VectorTile.Tile.Feature.Builder featureBuilder, int numProps) {
assertEquals(numProps, StreamSupport.stream(layerProps.getKeys().spliterator(), false).count());
assertEquals(numProps, StreamSupport.stream(layerProps.getVals().spliterator(), false).count());
assertEquals(2 * numProps, featureBuilder.getTagsCount());
}
}

0 comments on commit a5433e3

Please sign in to comment.