Skip to content

Commit

Permalink
SQL: fall back to using the field name for column label (#38842)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0567bf2)
  • Loading branch information
astefan committed Feb 14, 2019
1 parent 103786e commit 4a051e6
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<type=[").append(type).append(']');
if (false == "".equals(catalog)) {
if (false == EMPTY.equals(catalog)) {
b.append(" catalog=[").append(catalog).append(']');
}
if (false == "".equals(schema)) {
if (false == EMPTY.equals(schema)) {
b.append(" schema=[").append(schema).append(']');
}
if (false == "".equals(label)) {
if (false == EMPTY.equals(label)) {
b.append(" label=[").append(label).append(']');
}
return b.append('>').toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JdbcColumnInfo> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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<ResultSet, SQLException> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ private void doWithQueryAndTimezone(String query, String tz, CheckedConsumer<Res
}
}

private void createIndex(String index) throws Exception {
protected static void createIndex(String index) throws Exception {
Request request = new Request("PUT", "/" + index);
XContentBuilder createIndex = JsonXContent.contentBuilder().startObject();
createIndex.startObject("settings");
Expand All @@ -1286,7 +1286,7 @@ private void createIndex(String index) throws Exception {
client().performRequest(request);
}

private void updateMapping(String index, CheckedConsumer<XContentBuilder, IOException> body) throws Exception {
protected static void updateMapping(String index, CheckedConsumer<XContentBuilder, IOException> body) throws Exception {
Request request = new Request("PUT", "/" + index + "/_mapping/doc");
XContentBuilder updateMapping = JsonXContent.contentBuilder().startObject();
updateMapping.startObject("properties");
Expand Down Expand Up @@ -1464,7 +1464,7 @@ private Map<String,Number> createTestDataForNumericValueTypes(Supplier<Number> 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();
Expand Down

0 comments on commit 4a051e6

Please sign in to comment.