diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolver.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolver.java index 1800c170b7cff..10586c991b1ac 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolver.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolver.java @@ -253,7 +253,7 @@ static IndexResolution merge(List resolutions, String indexWild // need the same mapping across all resolutions if (!merged.get().mapping().equals(resolution.get().mapping())) { return IndexResolution.invalid( - "[" + indexWildcard + "] points to indices [" + resolution.get().name() + "] " + "[" + indexWildcard + "] points to indices [" + merged.get().name() + "] " + "and [" + resolution.get().name() + "] which have different mappings. " + "When using multiple indices, the mappings must be identical."); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java new file mode 100644 index 0000000000000..639356b2997f9 --- /dev/null +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.sql.analysis.index; + +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.sql.type.EsField; +import org.elasticsearch.xpack.sql.type.TypesTests; + +import java.util.Arrays; +import java.util.Map; + +public class IndexResolverTests extends ESTestCase { + + public void testMergeSameMapping() throws Exception { + Map oneMapping = TypesTests.loadMapping("mapping-basic.json", true); + Map sameMapping = TypesTests.loadMapping("mapping-basic.json", true); + assertNotSame(oneMapping, sameMapping); + assertEquals(oneMapping, sameMapping); + + String wildcard = "*"; + IndexResolution resolution = IndexResolver.merge( + Arrays.asList(IndexResolution.valid(new EsIndex("a", oneMapping)), IndexResolution.valid(new EsIndex("b", sameMapping))), + wildcard); + + assertTrue(resolution.isValid()); + + EsIndex esIndex = resolution.get(); + + assertEquals(wildcard, esIndex.name()); + assertEquals(sameMapping, esIndex.mapping()); + } + + public void testMergeDifferentMapping() throws Exception { + Map oneMapping = TypesTests.loadMapping("mapping-basic.json", true); + Map sameMapping = TypesTests.loadMapping("mapping-basic.json", true); + Map differentMapping = TypesTests.loadMapping("mapping-numeric.json", true); + + assertNotSame(oneMapping, sameMapping); + assertEquals(oneMapping, sameMapping); + assertNotEquals(oneMapping, differentMapping); + + String wildcard = "*"; + IndexResolution resolution = IndexResolver.merge( + Arrays.asList(IndexResolution.valid(new EsIndex("a", oneMapping)), + IndexResolution.valid(new EsIndex("b", sameMapping)), + IndexResolution.valid(new EsIndex("diff", differentMapping))), + wildcard); + + assertFalse(resolution.isValid()); + + MappingException ex = expectThrows(MappingException.class, () -> resolution.get()); + assertEquals( + "[*] points to indices [a] and [diff] which have different mappings. " + + "When using multiple indices, the mappings must be identical.", + ex.getMessage()); + } +} diff --git a/x-pack/plugin/sql/src/test/resources/mapping-numeric.json b/x-pack/plugin/sql/src/test/resources/mapping-numeric.json new file mode 100644 index 0000000000000..a95ecfb3aa706 --- /dev/null +++ b/x-pack/plugin/sql/src/test/resources/mapping-numeric.json @@ -0,0 +1,16 @@ +{ + "properties" : { + "byte" : { + "type" : "byte" + }, + "short" : { + "type" : "short" + }, + "integer" : { + "type" : "integer" + }, + "long" : { + "type" : "long" + } + } +}