|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.sql.analysis.index; |
| 7 | + |
| 8 | +import org.elasticsearch.test.ESTestCase; |
| 9 | +import org.elasticsearch.xpack.sql.type.EsField; |
| 10 | +import org.elasticsearch.xpack.sql.type.TypesTests; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class IndexResolverTests extends ESTestCase { |
| 16 | + |
| 17 | + public void testMergeSameMapping() throws Exception { |
| 18 | + Map<String, EsField> oneMapping = TypesTests.loadMapping("mapping-basic.json", true); |
| 19 | + Map<String, EsField> sameMapping = TypesTests.loadMapping("mapping-basic.json", true); |
| 20 | + assertNotSame(oneMapping, sameMapping); |
| 21 | + assertEquals(oneMapping, sameMapping); |
| 22 | + |
| 23 | + String wildcard = "*"; |
| 24 | + IndexResolution resolution = IndexResolver.merge( |
| 25 | + Arrays.asList(IndexResolution.valid(new EsIndex("a", oneMapping)), IndexResolution.valid(new EsIndex("b", sameMapping))), |
| 26 | + wildcard); |
| 27 | + |
| 28 | + assertTrue(resolution.isValid()); |
| 29 | + |
| 30 | + EsIndex esIndex = resolution.get(); |
| 31 | + |
| 32 | + assertEquals(wildcard, esIndex.name()); |
| 33 | + assertEquals(sameMapping, esIndex.mapping()); |
| 34 | + } |
| 35 | + |
| 36 | + public void testMergeDifferentMapping() throws Exception { |
| 37 | + Map<String, EsField> oneMapping = TypesTests.loadMapping("mapping-basic.json", true); |
| 38 | + Map<String, EsField> sameMapping = TypesTests.loadMapping("mapping-basic.json", true); |
| 39 | + Map<String, EsField> differentMapping = TypesTests.loadMapping("mapping-numeric.json", true); |
| 40 | + |
| 41 | + assertNotSame(oneMapping, sameMapping); |
| 42 | + assertEquals(oneMapping, sameMapping); |
| 43 | + assertNotEquals(oneMapping, differentMapping); |
| 44 | + |
| 45 | + String wildcard = "*"; |
| 46 | + IndexResolution resolution = IndexResolver.merge( |
| 47 | + Arrays.asList(IndexResolution.valid(new EsIndex("a", oneMapping)), |
| 48 | + IndexResolution.valid(new EsIndex("b", sameMapping)), |
| 49 | + IndexResolution.valid(new EsIndex("diff", differentMapping))), |
| 50 | + wildcard); |
| 51 | + |
| 52 | + assertFalse(resolution.isValid()); |
| 53 | + |
| 54 | + MappingException ex = expectThrows(MappingException.class, () -> resolution.get()); |
| 55 | + assertEquals( |
| 56 | + "[*] points to indices [a] and [diff] which have different mappings. " |
| 57 | + + "When using multiple indices, the mappings must be identical.", |
| 58 | + ex.getMessage()); |
| 59 | + } |
| 60 | +} |
0 commit comments