From 4a051e6c68182161bc112ceaf21286e523a1a87f Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Thu, 14 Feb 2019 12:42:18 +0200 Subject: [PATCH] SQL: fall back to using the field name for column label (#38842) (cherry picked from commit 0567bf24957be477e7649cff94872b0e7dc4d284) --- .../xpack/sql/jdbc/JdbcColumnInfo.java | 10 ++-- .../xpack/sql/jdbc/JdbcResultSetMetaData.java | 4 +- .../sql/jdbc/JdbcResultSetMetaDataTests.java | 52 +++++++++++++++++ .../single_node/JdbcResultSetMetaDataIT.java | 13 +++++ .../qa/jdbc/ResultSetMetaDataTestCase.java | 56 +++++++++++++++++++ .../xpack/sql/qa/jdbc/ResultSetTestCase.java | 6 +- 6 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java create mode 100644 x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcResultSetMetaDataIT.java create mode 100644 x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetMetaDataTestCase.java diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java index 82b12be50c42e..9b1ff87596798 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java @@ -7,6 +7,8 @@ import java.util.Objects; +import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY; + class JdbcColumnInfo { public final String catalog; public final String schema; @@ -52,17 +54,17 @@ int displaySize() { @Override public String toString() { StringBuilder b = new StringBuilder(); - if (false == "".equals(table)) { + if (false == EMPTY.equals(table)) { b.append(table).append('.'); } b.append(name).append("').toString(); diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaData.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaData.java index 8a819143881b7..d860c6daaa1ab 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaData.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaData.java @@ -11,6 +11,7 @@ import java.util.Locale; import static java.lang.String.format; +import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY; class JdbcResultSetMetaData implements ResultSetMetaData, JdbcWrapper { @@ -70,7 +71,8 @@ public int getColumnDisplaySize(int column) throws SQLException { @Override public String getColumnLabel(int column) throws SQLException { - return column(column).label; + JdbcColumnInfo info = column(column); + return true == EMPTY.equals(info.label) ? info.name : info.label; } @Override diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java new file mode 100644 index 0000000000000..6c3b270b73140 --- /dev/null +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java @@ -0,0 +1,52 @@ +/* + * 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.jdbc; + +import org.elasticsearch.test.ESTestCase; + +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; + +import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY; + +public class JdbcResultSetMetaDataTests extends ESTestCase { + + private final List columns = Arrays.asList( + new JdbcColumnInfo("test_keyword", EsType.KEYWORD, EMPTY, EMPTY, EMPTY, EMPTY, 0), + new JdbcColumnInfo("test_integer", EsType.INTEGER, EMPTY, EMPTY, EMPTY, EMPTY, 11), + new JdbcColumnInfo("test_double", EsType.DOUBLE, EMPTY, EMPTY, EMPTY, EMPTY, 25), + new JdbcColumnInfo("test_long", EsType.LONG, "test_table", "test", "schema", "custom_label", 20) + ); + private final JdbcResultSetMetaData metaData = new JdbcResultSetMetaData(null, columns); + + public void testColumnsProperties() throws SQLException { + int maxColumnIndex = columns.size(); + assertEquals(false, metaData.isAutoIncrement(randomIntBetween(1, maxColumnIndex))); + assertEquals(true, metaData.isCaseSensitive(randomIntBetween(1, maxColumnIndex))); + assertEquals(true, metaData.isSearchable(randomIntBetween(1, maxColumnIndex))); + assertEquals(false, metaData.isCurrency(randomIntBetween(1, maxColumnIndex))); + assertEquals(ResultSetMetaData.columnNullableUnknown, metaData.isNullable(randomIntBetween(1, maxColumnIndex))); + assertEquals(false, metaData.isSigned(1)); + assertEquals(true, metaData.isSigned(2)); + assertEquals(true, metaData.isSigned(3)); + assertEquals(true, metaData.isSigned(4)); + } + + public void testColumnNamesAndLabels() throws SQLException { + assertEquals("test_keyword", metaData.getColumnName(1)); + assertEquals("test_integer", metaData.getColumnName(2)); + assertEquals("test_double", metaData.getColumnName(3)); + assertEquals("test_long", metaData.getColumnName(4)); + + assertEquals("test_keyword", metaData.getColumnLabel(1)); + assertEquals("test_integer", metaData.getColumnLabel(2)); + assertEquals("test_double", metaData.getColumnLabel(3)); + assertEquals("custom_label", metaData.getColumnLabel(4)); + } +} diff --git a/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcResultSetMetaDataIT.java b/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcResultSetMetaDataIT.java new file mode 100644 index 0000000000000..85b85f2b0a6d5 --- /dev/null +++ b/x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcResultSetMetaDataIT.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.ResultSetMetaDataTestCase; + +public class JdbcResultSetMetaDataIT extends ResultSetMetaDataTestCase { + +} diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetMetaDataTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetMetaDataTestCase.java new file mode 100644 index 0000000000000..b8ab74bd33f5c --- /dev/null +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetMetaDataTestCase.java @@ -0,0 +1,56 @@ +/* + * 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.common.CheckedConsumer; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; + +public class ResultSetMetaDataTestCase extends JdbcIntegrationTestCase { + + private final String[] fieldsNames = new String[] {"test_byte", "test_integer", "test_long", "test_short", + "test_double", "test_float", "test_keyword", "test_boolean", "test_date"}; + + public void testValidGetObjectCalls() throws Exception { + ResultSetTestCase.createIndex("test"); + ResultSetTestCase.updateMapping("test", builder -> { + for(String field : fieldsNames) { + builder.startObject(field).field("type", field.substring(5)).endObject(); + } + }); + + String q = "SELECT test_byte, test_integer, test_long, test_short, test_double, test_float, test_keyword, " + + "test_boolean, test_date FROM test"; + doWithQuery(q, (r) -> assertColumnNamesAndLabels(r.getMetaData(), fieldsNames)); + + q = "SELECT test_byte AS b, test_integer AS i, test_long AS l, test_short AS s, test_double AS d, test_float AS f, " + + "test_keyword AS k, test_boolean AS bool, test_date AS dt FROM test"; + doWithQuery(q, (r) -> assertColumnNamesAndLabels(r.getMetaData(), new String[] {"b", "i", "l", "s", "d", "f", "k", "bool", "dt"})); + } + + private void doWithQuery(String query, CheckedConsumer consumer) throws SQLException { + try (Connection connection = esJdbc()) { + try (PreparedStatement statement = connection.prepareStatement(query)) { + try (ResultSet results = statement.executeQuery()) { + assertEquals(fieldsNames.length, results.getMetaData().getColumnCount()); + consumer.accept(results); + } + } + } + } + + private void assertColumnNamesAndLabels(ResultSetMetaData metaData, String[] names) throws SQLException { + for(int i = 0; i < fieldsNames.length; i++) { + assertEquals(names[i], metaData.getColumnName(i + 1)); + assertEquals(names[i], metaData.getColumnLabel(i + 1)); + } + } +} diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java index 3cb870852583e..400a4ac80237c 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java @@ -1262,7 +1262,7 @@ private void doWithQueryAndTimezone(String query, String tz, CheckedConsumer body) throws Exception { + protected static void updateMapping(String index, CheckedConsumer body) throws Exception { Request request = new Request("PUT", "/" + index + "/_mapping/doc"); XContentBuilder updateMapping = JsonXContent.contentBuilder().startObject(); updateMapping.startObject("properties"); @@ -1464,7 +1464,7 @@ private Map createTestDataForNumericValueTypes(Supplier r return map; } - private void updateMappingForNumericValuesTests(String indexName) throws Exception { + private static void updateMappingForNumericValuesTests(String indexName) throws Exception { updateMapping(indexName, builder -> { for(String field : fieldsNames) { builder.startObject(field).field("type", field.substring(5)).endObject();