From 15a670d6624eaf8c0f997f27d345bcad1b57e2e0 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Tue, 17 Mar 2020 16:42:24 +0200 Subject: [PATCH] Add support for index aliases for SYS COLUMNS command (cherry picked from commit f65e4d6ff7b2e00eb6f9c985fbe7cb24de00f045) --- .../sql/qa/single_node/SysColumnsIT.java | 13 + .../xpack/sql/qa/jdbc/SysColumnsTestCase.java | 358 ++++++++++++++++++ .../single-node-only/command-sys.csv-spec | 88 +++-- .../sql/analysis/index/IndexResolver.java | 235 +++++++++++- .../xpack/sql/type/InvalidMappedField.java | 6 + .../xpack/sql/util/CollectionUtils.java | 7 + .../analysis/index/IndexResolverTests.java | 4 +- 7 files changed, 658 insertions(+), 53 deletions(-) create mode 100644 x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/SysColumnsIT.java create mode 100644 x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/SysColumnsTestCase.java diff --git a/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/SysColumnsIT.java b/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/SysColumnsIT.java new file mode 100644 index 0000000000000..b2568d970bca1 --- /dev/null +++ b/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/SysColumnsIT.java @@ -0,0 +1,13 @@ +/* + * 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.qa.single_node; + +import org.elasticsearch.xpack.sql.qa.jdbc.SysColumnsTestCase; + +public class SysColumnsIT extends SysColumnsTestCase { + +} diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/SysColumnsTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/SysColumnsTestCase.java new file mode 100644 index 0000000000000..d69c564c94902 --- /dev/null +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/SysColumnsTestCase.java @@ -0,0 +1,358 @@ +/* + * 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.qa.jdbc; + +import org.elasticsearch.client.Request; +import org.elasticsearch.common.CheckedConsumer; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.json.JsonXContent; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.elasticsearch.xpack.sql.qa.jdbc.ResultSetTestCase.updateMapping; + +public class SysColumnsTestCase extends JdbcIntegrationTestCase { + + public void testAliasWithIncompatibleTypes() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("value").field("type", "double").endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "text").endObject(); + builder.startObject("value").field("type", "double").endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"KEYWORD"}, + {"test1" ,"value" ,"DOUBLE"}, + {"test2" ,"id" ,"TEXT"}, + {"test2" ,"value" ,"DOUBLE"}, + {"test_alias" ,"value" ,"DOUBLE"} + }); + } + + public void testAliasWithIncompatibleSearchableProperty() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("value").field("type", "boolean").endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "keyword").field("index", false).endObject(); + builder.startObject("value").field("type", "boolean").endObject(); + }); + + createIndexWithMapping("test3", builder -> { + builder.startObject("id").field("type", "keyword").field("index", false).endObject(); + builder.startObject("value").field("type", "boolean").endObject(); + }); + + createIndexWithMapping("test4", builder -> { + builder.startObject("id").field("type", "keyword").field("index", false).endObject(); + builder.startObject("value").field("type", "boolean").endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test3").field("alias", "test_alias2").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test4").field("alias", "test_alias2").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"KEYWORD"}, + {"test1" ,"value" ,"BOOLEAN"}, + {"test2" ,"id" ,"KEYWORD"}, + {"test2" ,"value" ,"BOOLEAN"}, + {"test3" ,"id" ,"KEYWORD"}, + {"test3" ,"value" ,"BOOLEAN"}, + {"test4" ,"id" ,"KEYWORD"}, + {"test4" ,"value" ,"BOOLEAN"}, + {"test_alias" ,"value" ,"BOOLEAN"}, + {"test_alias2" ,"id" ,"KEYWORD"}, + {"test_alias2" ,"value" ,"BOOLEAN"} + }); + } + + public void testAliasWithIncompatibleAggregatableProperty() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "text").field("fielddata", true).endObject(); + builder.startObject("value").field("type", "date").endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "text").endObject(); + builder.startObject("value").field("type", "date").endObject(); + }); + + createIndexWithMapping("test3", builder -> { + builder.startObject("id").field("type", "text").field("fielddata", true).endObject(); + builder.startObject("value").field("type", "date").endObject(); + }); + + createIndexWithMapping("test4", builder -> { + builder.startObject("id").field("type", "text").field("fielddata", true).endObject(); + builder.startObject("value").field("type", "date").endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test3").field("alias", "test_alias2").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test4").field("alias", "test_alias2").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"TEXT"}, + {"test1" ,"value" ,"DATETIME"}, + {"test2" ,"id" ,"TEXT"}, + {"test2" ,"value" ,"DATETIME"}, + {"test3" ,"id" ,"TEXT"}, + {"test3" ,"value" ,"DATETIME"}, + {"test4" ,"id" ,"TEXT"}, + {"test4" ,"value" ,"DATETIME"}, + {"test_alias" ,"value" ,"DATETIME"}, + {"test_alias2","id" ,"TEXT"}, + {"test_alias2","value" ,"DATETIME"}, + }); + } + + public void testAliasWithIncompatibleTypesInSubfield() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "keyword") + .endObject().endObject().endObject(); + builder.startObject("value").field("type", "date").startObject("fields").startObject("raw").field("type", "long") + .endObject().endObject().endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "integer") + .endObject().endObject().endObject(); + builder.startObject("value").field("type", "date").startObject("fields").startObject("raw").field("type", "long") + .endObject().endObject().endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"TEXT"}, + {"test1" ,"id.raw" ,"KEYWORD"}, + {"test1" ,"value" ,"DATETIME"}, + {"test1" ,"value.raw","LONG"}, + {"test2" ,"id" ,"TEXT"}, + {"test2" ,"id.raw" ,"INTEGER"}, + {"test2" ,"value" ,"DATETIME"}, + {"test2" ,"value.raw","LONG"}, + {"test_alias" ,"id" ,"TEXT"}, + {"test_alias" ,"value" ,"DATETIME"}, + {"test_alias" ,"value.raw","LONG"}, + }); + } + + public void testAliasWithIncompatibleSearchablePropertyInSubfield() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "integer") + .endObject().endObject().endObject(); + builder.startObject("value").field("type", "date").startObject("fields").startObject("raw").field("type", "long") + .endObject().endObject().endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "integer") + .field("index", false).endObject().endObject().endObject(); + builder.startObject("value").field("type", "date").startObject("fields").startObject("raw").field("type", "long") + .endObject().endObject().endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"TEXT"}, + {"test1" ,"id.raw" ,"INTEGER"}, + {"test1" ,"value" ,"DATETIME"}, + {"test1" ,"value.raw","LONG"}, + {"test2" ,"id" ,"TEXT"}, + {"test2" ,"id.raw" ,"INTEGER"}, + {"test2" ,"value" ,"DATETIME"}, + {"test2" ,"value.raw","LONG"}, + {"test_alias" ,"id" ,"TEXT"}, + {"test_alias" ,"value" ,"DATETIME"}, + {"test_alias" ,"value.raw","LONG"}, + }); + } + + public void testAliasWithIncompatibleAggregatablePropertyInSubfield() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "integer") + .field("doc_values", false).endObject().endObject().endObject(); + builder.startObject("value").field("type", "ip").startObject("fields").startObject("raw").field("type", "text") + .endObject().endObject().endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "text").startObject("fields").startObject("raw").field("type", "integer") + .endObject().endObject().endObject(); + builder.startObject("value").field("type", "ip").startObject("fields").startObject("raw").field("type", "text") + .endObject().endObject().endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"TEXT"}, + {"test1" ,"id.raw" ,"INTEGER"}, + {"test1" ,"value" ,"IP"}, + {"test1" ,"value.raw","TEXT"}, + {"test2" ,"id" ,"TEXT"}, + {"test2" ,"id.raw" ,"INTEGER"}, + {"test2" ,"value" ,"IP"}, + {"test2" ,"value.raw","TEXT"}, + {"test_alias" ,"id" ,"TEXT"}, + {"test_alias" ,"value" ,"IP"}, + {"test_alias" ,"value.raw","TEXT"}, + }); + } + + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/53445") + public void testAliasWithSubfieldsAndDifferentRootFields() throws Exception { + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "text").startObject("fields").startObject("raw").field("type", "keyword") + .endObject().endObject().endObject(); + }); + + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "keyword").field("index", false).startObject("fields").startObject("raw") + .field("type", "keyword").endObject().endObject().endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "test_alias").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "test_alias").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"test1" ,"id" ,"KEYWORD"}, + {"test1" ,"name" ,"TEXT"}, + {"test1" ,"name.raw","KEYWORD"}, + {"test2" ,"id" ,"KEYWORD"}, + {"test2" ,"name" ,"KEYWORD"}, + {"test2" ,"name.raw","KEYWORD"}, + {"test_alias" ,"id" ,"KEYWORD"} + }); + } + + public void testMultiIndicesMultiAlias() throws Exception { + createIndexWithMapping("test2", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "text").endObject(); + }); + createIndexWithMapping("test4", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "text").field("index", false).endObject(); + }); + createIndexWithMapping("test1", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "keyword").endObject(); + builder.startObject("number").field("type", "long").endObject(); + }); + createIndexWithMapping("test3", builder -> { + builder.startObject("id").field("type", "keyword").endObject(); + builder.startObject("name").field("type", "keyword").endObject(); + builder.startObject("number").field("type", "long").endObject(); + }); + + createAliases(builder -> { + builder.startObject().startObject("add").field("index", "test1").field("alias", "alias1").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test1").field("alias", "alias2").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "alias2").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test2").field("alias", "alias3").endObject().endObject(); + builder.startObject().startObject("add").field("index", "test4").field("alias", "alias3").endObject().endObject(); + }); + + assertResultsForQuery("SYS COLUMNS", new String[][] { + {"alias1","id" ,"KEYWORD"}, + {"alias1","name" ,"KEYWORD"}, + {"alias1","number","LONG"}, + {"alias2","id" ,"KEYWORD"}, + {"alias2","number","LONG"}, + {"alias3","id" ,"KEYWORD"}, + {"test1" ,"id" ,"KEYWORD"}, + {"test1" ,"name" ,"KEYWORD"}, + {"test1" ,"number","LONG"}, + {"test2" ,"id" ,"KEYWORD"}, + {"test2" ,"name" ,"TEXT"}, + {"test3" ,"id" ,"KEYWORD"}, + {"test3" ,"name" ,"KEYWORD"}, + {"test3" ,"number","LONG"}, + {"test4" ,"id" ,"KEYWORD"}, + {"test4" ,"name" ,"TEXT"} + }); + } + + private static void createIndexWithMapping(String indexName, CheckedConsumer mapping) throws Exception { + ResultSetTestCase.createIndex(indexName); + updateMapping(indexName, mapping); + } + + private void doWithQuery(String query, CheckedConsumer consumer) throws SQLException { + try (Connection connection = esJdbc()) { + try (PreparedStatement statement = connection.prepareStatement(query)) { + try (ResultSet results = statement.executeQuery()) { + consumer.accept(results); + } + } + } + } + + private static void createAliases(CheckedConsumer definitions) throws Exception { + Request request = new Request("POST", "/_aliases"); + XContentBuilder createAliases = JsonXContent.contentBuilder().startObject(); + createAliases.startArray("actions"); + { + definitions.accept(createAliases); + } + createAliases.endArray(); + createAliases.endObject(); + request.setJsonEntity(Strings.toString(createAliases)); + client().performRequest(request); + } + + private void assertResultsForQuery(String query, String[][] rows) throws Exception { + doWithQuery(query, (results) -> { + for (String[] row : rows) { + results.next(); + assertEquals(row[0], results.getString(3)); // table name + assertEquals(row[1], results.getString(4)); // column name + assertEquals(row[2], results.getString(6)); // type name + } + assertFalse(results.next()); + }); + } +} diff --git a/x-pack/plugin/sql/qa/src/main/resources/single-node-only/command-sys.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/single-node-only/command-sys.csv-spec index 711df01849103..b11b4adb8bbaa 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/single-node-only/command-sys.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/single-node-only/command-sys.csv-spec @@ -89,35 +89,61 @@ SYS COLUMNS TABLE LIKE '%'; TABLE_CAT:s | TABLE_SCHEM:s| TABLE_NAME:s | COLUMN_NAME:s | DATA_TYPE:i | TYPE_NAME:s | COLUMN_SIZE:i| BUFFER_LENGTH:i|DECIMAL_DIGITS:i|NUM_PREC_RADIX:i | NULLABLE:i| REMARKS:s | COLUMN_DEF:s |SQL_DATA_TYPE:i|SQL_DATETIME_SUB:i|CHAR_OCTET_LENGTH:i|ORDINAL_POSITION:i|IS_NULLABLE:s|SCOPE_CATALOG:s|SCOPE_SCHEMA:s|SCOPE_TABLE:s|SOURCE_DATA_TYPE:sh|IS_AUTOINCREMENT:s|IS_GENERATEDCOLUMN:s ---------------+---------------+---------------+------------------+---------------+---------------+---------------+----------------+----------------+-----------------+-----------+---------------+---------------+---------------+------------------+-------------------+------------------+-------------+---------------+---------------+---------------+----------------+------------------+------------------ -integTest |null |logs |@timestamp |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO -integTest |null |logs |bytes_in |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |2 |YES |null |null |null |null |NO |NO -integTest |null |logs |bytes_out |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO -integTest |null |logs |client_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |4 |YES |null |null |null |null |NO |NO -integTest |null |logs |client_port |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |5 |YES |null |null |null |null |NO |NO -integTest |null |logs |dest_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |6 |YES |null |null |null |null |NO |NO -integTest |null |logs |id |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |7 |YES |null |null |null |null |NO |NO -integTest |null |logs |status |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |8 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |4 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |7 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |8 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO -integTest |null |test_emp |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |11 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO -integTest |null |test_emp_copy |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |YES |null |null |null |null |NO |NO +integTest |null |logs |@timestamp |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO +integTest |null |logs |bytes_in |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |2 |YES |null |null |null |null |NO |NO +integTest |null |logs |bytes_out |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO +integTest |null |logs |client_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |4 |YES |null |null |null |null |NO |NO +integTest |null |logs |client_port |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |5 |YES |null |null |null |null |NO |NO +integTest |null |logs |dest_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |6 |YES |null |null |null |null |NO |NO +integTest |null |logs |id |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |7 |YES |null |null |null |null |NO |NO +integTest |null |logs |status |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |8 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO +integTest |null |test_alias |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO +integTest |null |test_alias_emp |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |4 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |7 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |8 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO +integTest |null |test_emp |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |11 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO +integTest |null |test_emp_copy |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |YES |null |null |null |null |NO |NO ; 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 c52b6a3e81479..4c284c40872bd 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 @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.sql.analysis.index; import com.carrotsearch.hppc.cursors.ObjectCursor; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.ActionListener; @@ -16,12 +17,14 @@ import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; +import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions.Option; import org.elasticsearch.action.support.IndicesOptions.WildcardStates; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; @@ -40,9 +43,11 @@ import java.util.Collections; import java.util.Comparator; import java.util.EnumSet; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -155,7 +160,6 @@ public boolean equals(Object obj) { private final Client client; private final String clusterName; - public IndexResolver(Client client, String clusterName) { this.client = client; this.clusterName = clusterName; @@ -284,7 +288,7 @@ static IndexResolution mergedMappings(String indexPattern, String[] indexNames, } // merge all indices onto the same one - List indices = buildIndices(indexNames, null, fieldCaps, i -> indexPattern, (n, types) -> { + List indices = buildIndices(indexNames, null, fieldCaps, null, i -> indexPattern, (n, types) -> { StringBuilder errorMessage = new StringBuilder(); boolean hasUnmapped = types.containsKey(UNMAPPED); @@ -444,16 +448,32 @@ private static FieldCapabilitiesRequest createFieldCapsRequest(String index, boo public void resolveAsSeparateMappings(String indexWildcard, String javaRegex, boolean includeFrozen, ActionListener> listener) { FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, includeFrozen); - client.fieldCaps(fieldRequest, - ActionListener.wrap( - response -> listener.onResponse(separateMappings(indexWildcard, javaRegex, response.getIndices(), response.get())), - listener::onFailure)); + client.fieldCaps(fieldRequest, wrap(response -> { + client.admin().indices().getAliases(createGetAliasesRequest(response, includeFrozen), wrap(aliases -> + listener.onResponse(separateMappings(javaRegex, response.getIndices(), response.get(), aliases.getAliases())), + ex -> { + if (ex instanceof IndexNotFoundException || ex instanceof ElasticsearchSecurityException) { + listener.onResponse(separateMappings(javaRegex, response.getIndices(), response.get(), null)); + } else { + listener.onFailure(ex); + } + })); + }, + listener::onFailure)); } + + private GetAliasesRequest createGetAliasesRequest(FieldCapabilitiesResponse response, boolean includeFrozen) { + return new GetAliasesRequest() + .local(true) + .aliases("*") + .indices(response.getIndices()) + .indicesOptions(includeFrozen ? FIELD_CAPS_FROZEN_INDICES_OPTIONS : FIELD_CAPS_INDICES_OPTIONS); + } - static List separateMappings(String indexPattern, String javaRegex, String[] indexNames, - Map> fieldCaps) { - return buildIndices(indexNames, javaRegex, fieldCaps, Function.identity(), (s, cap) -> null); + public static List separateMappings(String javaRegex, String[] indexNames, + Map> fieldCaps, ImmutableOpenMap> aliases) { + return buildIndices(indexNames, javaRegex, fieldCaps, aliases, Function.identity(), (s, cap) -> null); } private static class Fields { @@ -466,15 +486,26 @@ private static class Fields { * each field. */ private static List buildIndices(String[] indexNames, String javaRegex, Map> fieldCaps, - Function indexNameProcessor, + ImmutableOpenMap> aliases, Function indexNameProcessor, BiFunction, InvalidMappedField> validityVerifier) { - if (indexNames == null || indexNames.length == 0) { + if ((indexNames == null || indexNames.length == 0) && (aliases == null || aliases.isEmpty())) { return emptyList(); } - final List resolvedIndices = asList(indexNames); - Map indices = new LinkedHashMap<>(resolvedIndices.size()); + Set resolvedAliases = new HashSet<>(); + if (aliases != null) { + Iterator>> iterator = aliases.iterator(); + while (iterator.hasNext()) { + for (AliasMetaData alias : iterator.next().value) { + resolvedAliases.add(alias.getAlias()); + } + } + } + + List resolvedIndices = new ArrayList<>(asList(indexNames)); + int mapSize = CollectionUtils.mapSize(resolvedIndices.size() + resolvedAliases.size()); + Map indices = new LinkedHashMap<>(mapSize); Pattern pattern = javaRegex != null ? Pattern.compile(javaRegex) : null; // sort fields in reverse order to build the field hierarchy @@ -494,6 +525,8 @@ private static List buildIndices(String[] indexNames, String javaRegex, // apply verification final InvalidMappedField invalidField = validityVerifier.apply(fieldName, types); + // apply verification for fields belonging to index aliases + Map invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases); // filter meta fields and unmapped FieldCapabilities unmapped = types.get(UNMAPPED); @@ -514,7 +547,7 @@ private static List buildIndices(String[] indexNames, String javaRegex, List concreteIndices = null; if (capIndices != null) { if (unmappedIndices.isEmpty() == true) { - concreteIndices = asList(capIndices); + concreteIndices = new ArrayList<>(asList(capIndices)); } else { concreteIndices = new ArrayList<>(capIndices.length); for (String capIndex : capIndices) { @@ -528,37 +561,62 @@ private static List buildIndices(String[] indexNames, String javaRegex, concreteIndices = resolvedIndices; } + // add to the list of concrete indices the aliases associated with these indices + Set uniqueAliases = new LinkedHashSet<>(); + if (aliases != null) { + for (String concreteIndex : concreteIndices) { + if (aliases.containsKey(concreteIndex)) { + List concreteIndexAliases = aliases.get(concreteIndex); + concreteIndexAliases.stream().forEach(e -> uniqueAliases.add(e.alias())); + } + } + concreteIndices.addAll(uniqueAliases); + } + // put the field in their respective mappings for (String index : concreteIndices) { - if (pattern == null || pattern.matcher(index).matches()) { - String indexName = indexNameProcessor.apply(index); + boolean isIndexAlias = uniqueAliases.contains(index); + if (pattern == null || pattern.matcher(index).matches() || isIndexAlias) { + String indexName = isIndexAlias ? index : indexNameProcessor.apply(index); Fields indexFields = indices.get(indexName); if (indexFields == null) { indexFields = new Fields(); indices.put(indexName, indexFields); } EsField field = indexFields.flattedMapping.get(fieldName); - if (field == null || (invalidField != null && (field instanceof InvalidMappedField) == false)) { + boolean createField = false; + if (isIndexAlias == false) { + if (field == null || (invalidField != null && (field instanceof InvalidMappedField) == false)) { + createField = true; + } + } + else { + if (field == null && invalidFieldsForAliases.get(index) == null) { + createField = true; + } + } + + if (createField) { int dot = fieldName.lastIndexOf('.'); /* * Looking up the "tree" at the parent fields here to see if the field is an alias. * When the upper elements of the "tree" have no elements in fieldcaps, then this is an alias field. But not * always: if there are two aliases - a.b.c.alias1 and a.b.c.alias2 - only one of them will be considered alias. */ - Holder isAlias = new Holder<>(false); + Holder isAliasFieldType = new Holder<>(false); if (dot >= 0) { String parentName = fieldName.substring(0, dot); if (indexFields.flattedMapping.get(parentName) == null) { // lack of parent implies the field is an alias if (fieldCaps.get(parentName) == null) { - isAlias.set(true); + isAliasFieldType.set(true); } } } createField(fieldName, fieldCaps, indexFields.hierarchicalMapping, indexFields.flattedMapping, s -> invalidField != null ? invalidField : createField(s, typeCap.getType(), emptyMap(), - typeCap.isAggregatable(), isAlias.get())); + typeCap.isAggregatable(), isAliasFieldType.get())); } } } @@ -573,4 +631,141 @@ private static List buildIndices(String[] indexNames, String javaRegex, foundIndices.sort(Comparator.comparing(EsIndex::name)); return foundIndices; } + + + /* + * Checks if the field is valid (same type and same capabilities - searchable/aggregatable) across indices belonging to a list + * of aliases. + * A field can look like the example below (generated by field_caps API). + * "name": { + * "text": { + * "type": "text", + * "searchable": false, + * "aggregatable": false, + * "indices": [ + * "bar", + * "foo" + * ], + * "non_searchable_indices": [ + * "foo" + * ] + * }, + * "keyword": { + * "type": "keyword", + * "searchable": false, + * "aggregatable": true, + * "non_aggregatable_indices": [ + * "bar", "baz" + * ] + * } + * } + */ + private static Map getInvalidFieldsForAliases(String fieldName, Map types, + ImmutableOpenMap> aliases) { + if (aliases == null || aliases.isEmpty()) { + return emptyMap(); + } + Map invalidFields = new HashMap<>(); + Map> typesErrors = new HashMap<>(); // map holding aliases and a list of unique field types across its indices + Map> aliasToIndices = new HashMap<>(); // map with aliases and their list of indices + + Iterator>> iter = aliases.iterator(); + while (iter.hasNext()) { + ObjectObjectCursor> index = iter.next(); + for (AliasMetaData aliasMetaData : index.value) { + String aliasName = aliasMetaData.alias(); + aliasToIndices.putIfAbsent(aliasName, new HashSet<>()); + aliasToIndices.get(aliasName).add(index.key); + } + } + + // iterate over each type + for (Entry type : types.entrySet()) { + String esFieldType = type.getKey(); + if (esFieldType == UNMAPPED) { + continue; + } + String[] indices = type.getValue().indices(); + // if there is a list of indices where this field type is defined + if (indices != null) { + // Look at all these indices' aliases and add the type of the field to a list (Set) with unique elements. + // A valid mapping for a field in an index alias should contain only one type. If it doesn't, this means that field + // is mapped as different types across the indices in this index alias. + for (String index : indices) { + List indexAliases = aliases.get(index); + if (indexAliases == null) { + continue; + } + for (AliasMetaData aliasMetaData : indexAliases) { + String aliasName = aliasMetaData.alias(); + if (typesErrors.containsKey(aliasName)) { + typesErrors.get(aliasName).add(esFieldType); + } else { + Set fieldTypes = new HashSet<>(); + fieldTypes.add(esFieldType); + typesErrors.put(aliasName, fieldTypes); + } + } + } + } + } + + for (String aliasName : aliasToIndices.keySet()) { + // if, for the same index alias, there are multiple field types for this fieldName ie the index alias has indices where the same + // field name is of different types + Set esFieldTypes = typesErrors.get(aliasName); + if (esFieldTypes != null && esFieldTypes.size() > 1) { + // consider the field as invalid, for the currently checked index alias + // the error message doesn't actually matter + invalidFields.put(aliasName, new InvalidMappedField(fieldName)); + } else { + // if the field type is the same across all this alias' indices, check the field's capabilities (searchable/aggregatable) + for (Entry type : types.entrySet()) { + if (type.getKey() == UNMAPPED) { + continue; + } + FieldCapabilities f = type.getValue(); + + // the existence of a list of non_aggregatable_indices is an indication that not all indices have the same capabilities + // but this list can contain indices belonging to other aliases, so we need to check only for this alias + if (f.nonAggregatableIndices() != null) { + Set aliasIndices = aliasToIndices.get(aliasName); + int nonAggregatableCount = 0; + // either all or none of the non-aggregatable indices belonging to a certain alias should be in this list + for (String nonAggIndex : f.nonAggregatableIndices()) { + if (aliasIndices.contains(nonAggIndex)) { + nonAggregatableCount++; + } + } + if (nonAggregatableCount > 0 && nonAggregatableCount != aliasIndices.size()) { + invalidFields.put(aliasName, new InvalidMappedField(fieldName)); + break; + } + } + + // perform the same check for non_searchable_indices list + if (f.nonSearchableIndices() != null) { + Set aliasIndices = aliasToIndices.get(aliasName); + int nonSearchableCount = 0; + // either all or none of the non-searchable indices belonging to a certain alias should be in this list + for (String nonSearchIndex : f.nonSearchableIndices()) { + if (aliasIndices.contains(nonSearchIndex)) { + nonSearchableCount++; + } + } + if (nonSearchableCount > 0 && nonSearchableCount != aliasIndices.size()) { + invalidFields.put(aliasName, new InvalidMappedField(fieldName)); + break; + } + } + } + } + } + + if (invalidFields.size() > 0) { + return invalidFields; + } + // everything checks + return emptyMap(); + } } \ No newline at end of file diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/InvalidMappedField.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/InvalidMappedField.java index 79f8eb1c20c1f..aa0957e556f19 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/InvalidMappedField.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/InvalidMappedField.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.sql.type; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; +import org.elasticsearch.xpack.sql.util.StringUtils; import java.util.Objects; @@ -25,6 +26,11 @@ public InvalidMappedField(String name, String errorMessage) { this.errorMessage = errorMessage; } + public InvalidMappedField(String name) { + super(name, DataType.UNSUPPORTED, emptyMap(), false); + this.errorMessage = StringUtils.EMPTY; + } + public String errorMessage() { return errorMessage; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/CollectionUtils.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/CollectionUtils.java index 24ed7b979f63c..098d7d0230fce 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/CollectionUtils.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/CollectionUtils.java @@ -72,4 +72,11 @@ public static List combine(Collection left, T... entries) { } return list; } + + public static int mapSize(int size) { + if (size < 2) { + return size + 1; + } + return (int) (size / 0.75f + 1f); + } } \ No newline at end of file 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 index 7092ea2da8971..456edf5a9cc1c 100644 --- 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 @@ -315,8 +315,8 @@ public static IndexResolution merge(EsIndex... indices) { } public static List separate(EsIndex... indices) { - return IndexResolver.separateMappings("*", null, Stream.of(indices).map(EsIndex::name).toArray(String[]::new), - fromMappings(indices)); + return IndexResolver.separateMappings(null, Stream.of(indices).map(EsIndex::name).toArray(String[]::new), + fromMappings(indices), null); } public static Map> fromMappings(EsIndex... indices) {