Skip to content

Commit 1050afe

Browse files
committed
SQL: fall back to using the field name for column label (elastic#38842)
(cherry picked from commit 0567bf2)
1 parent 4e94b89 commit 1050afe

File tree

6 files changed

+136
-8
lines changed

6 files changed

+136
-8
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Locale;
1414

1515
import static java.lang.String.format;
16+
import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY;
1617

1718
class JdbcResultSetMetaData implements ResultSetMetaData, JdbcWrapper {
1819

@@ -72,7 +73,8 @@ public int getColumnDisplaySize(int column) throws SQLException {
7273

7374
@Override
7475
public String getColumnLabel(int column) throws SQLException {
75-
return column(column).label;
76+
ColumnInfo info = column(column);
77+
return true == EMPTY.equals(info.label) ? info.name : info.label;
7678
}
7779

7880
@Override

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ColumnInfo.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import java.sql.SQLType;
99
import java.util.Objects;
1010

11+
import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY;
12+
1113
public class ColumnInfo {
14+
1215
public final String catalog;
1316
public final String schema;
1417
public final String table;
@@ -53,17 +56,17 @@ public int displaySize() {
5356
@Override
5457
public String toString() {
5558
StringBuilder b = new StringBuilder();
56-
if (false == "".equals(table)) {
59+
if (false == EMPTY.equals(table)) {
5760
b.append(table).append('.');
5861
}
5962
b.append(name).append("<type=[").append(type).append(']');
60-
if (false == "".equals(catalog)) {
63+
if (false == EMPTY.equals(catalog)) {
6164
b.append(" catalog=[").append(catalog).append(']');
6265
}
63-
if (false == "".equals(schema)) {
66+
if (false == EMPTY.equals(schema)) {
6467
b.append(" schema=[").append(schema).append(']');
6568
}
66-
if (false == "".equals(label)) {
69+
if (false == EMPTY.equals(label)) {
6770
b.append(" label=[").append(label).append(']');
6871
}
6972
return b.append('>').toString();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
7+
package org.elasticsearch.xpack.sql.jdbc.jdbc;
8+
9+
import org.elasticsearch.test.ESTestCase;
10+
import org.elasticsearch.xpack.sql.jdbc.net.protocol.ColumnInfo;
11+
12+
import java.sql.JDBCType;
13+
import java.sql.ResultSetMetaData;
14+
import java.sql.SQLException;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
import static org.elasticsearch.xpack.sql.client.StringUtils.EMPTY;
19+
20+
public class JdbcResultSetMetaDataTests extends ESTestCase {
21+
22+
private final List<ColumnInfo> columns = Arrays.asList(
23+
new ColumnInfo("test_keyword", JDBCType.VARCHAR, EMPTY, EMPTY, EMPTY, EMPTY, 0),
24+
new ColumnInfo("test_integer", JDBCType.INTEGER, EMPTY, EMPTY, EMPTY, EMPTY, 11),
25+
new ColumnInfo("test_double", JDBCType.DOUBLE, EMPTY, EMPTY, EMPTY, EMPTY, 25),
26+
new ColumnInfo("test_long", JDBCType.BIGINT, "test_table", "test", "schema", "custom_label", 20)
27+
);
28+
private final JdbcResultSetMetaData metaData = new JdbcResultSetMetaData(null, columns);
29+
30+
public void testColumnsProperties() throws SQLException {
31+
int maxColumnIndex = columns.size();
32+
assertEquals(false, metaData.isAutoIncrement(randomIntBetween(1, maxColumnIndex)));
33+
assertEquals(true, metaData.isCaseSensitive(randomIntBetween(1, maxColumnIndex)));
34+
assertEquals(true, metaData.isSearchable(randomIntBetween(1, maxColumnIndex)));
35+
assertEquals(false, metaData.isCurrency(randomIntBetween(1, maxColumnIndex)));
36+
assertEquals(ResultSetMetaData.columnNullableUnknown, metaData.isNullable(randomIntBetween(1, maxColumnIndex)));
37+
assertEquals(false, metaData.isSigned(1));
38+
assertEquals(true, metaData.isSigned(2));
39+
assertEquals(true, metaData.isSigned(3));
40+
assertEquals(true, metaData.isSigned(4));
41+
}
42+
43+
public void testColumnNamesAndLabels() throws SQLException {
44+
assertEquals("test_keyword", metaData.getColumnName(1));
45+
assertEquals("test_integer", metaData.getColumnName(2));
46+
assertEquals("test_double", metaData.getColumnName(3));
47+
assertEquals("test_long", metaData.getColumnName(4));
48+
49+
assertEquals("test_keyword", metaData.getColumnLabel(1));
50+
assertEquals("test_integer", metaData.getColumnLabel(2));
51+
assertEquals("test_double", metaData.getColumnLabel(3));
52+
assertEquals("custom_label", metaData.getColumnLabel(4));
53+
}
54+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
7+
package org.elasticsearch.xpack.sql.qa.single_node;
8+
9+
import org.elasticsearch.xpack.sql.qa.jdbc.ResultSetMetaDataTestCase;
10+
11+
public class JdbcResultSetMetaDataIT extends ResultSetMetaDataTestCase {
12+
13+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
7+
package org.elasticsearch.xpack.sql.qa.jdbc;
8+
9+
import org.elasticsearch.common.CheckedConsumer;
10+
11+
import java.sql.Connection;
12+
import java.sql.PreparedStatement;
13+
import java.sql.ResultSet;
14+
import java.sql.ResultSetMetaData;
15+
import java.sql.SQLException;
16+
17+
public class ResultSetMetaDataTestCase extends JdbcIntegrationTestCase {
18+
19+
private final String[] fieldsNames = new String[] {"test_byte", "test_integer", "test_long", "test_short",
20+
"test_double", "test_float", "test_keyword", "test_boolean", "test_date"};
21+
22+
public void testValidGetObjectCalls() throws Exception {
23+
ResultSetTestCase.createIndex("test");
24+
ResultSetTestCase.updateMapping("test", builder -> {
25+
for(String field : fieldsNames) {
26+
builder.startObject(field).field("type", field.substring(5)).endObject();
27+
}
28+
});
29+
30+
String q = "SELECT test_byte, test_integer, test_long, test_short, test_double, test_float, test_keyword, "
31+
+ "test_boolean, test_date FROM test";
32+
doWithQuery(q, (r) -> assertColumnNamesAndLabels(r.getMetaData(), fieldsNames));
33+
34+
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, "
35+
+ "test_keyword AS k, test_boolean AS bool, test_date AS dt FROM test";
36+
doWithQuery(q, (r) -> assertColumnNamesAndLabels(r.getMetaData(), new String[] {"b", "i", "l", "s", "d", "f", "k", "bool", "dt"}));
37+
}
38+
39+
private void doWithQuery(String query, CheckedConsumer<ResultSet, SQLException> consumer) throws SQLException {
40+
try (Connection connection = esJdbc()) {
41+
try (PreparedStatement statement = connection.prepareStatement(query)) {
42+
try (ResultSet results = statement.executeQuery()) {
43+
assertEquals(fieldsNames.length, results.getMetaData().getColumnCount());
44+
consumer.accept(results);
45+
}
46+
}
47+
}
48+
}
49+
50+
private void assertColumnNamesAndLabels(ResultSetMetaData metaData, String[] names) throws SQLException {
51+
for(int i = 0; i < fieldsNames.length; i++) {
52+
assertEquals(names[i], metaData.getColumnName(i + 1));
53+
assertEquals(names[i], metaData.getColumnLabel(i + 1));
54+
}
55+
}
56+
}

x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/ResultSetTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ private void doWithQueryAndTimezone(String query, String tz, CheckedConsumer<Res
12611261
}
12621262
}
12631263

1264-
private void createIndex(String index) throws Exception {
1264+
protected static void createIndex(String index) throws Exception {
12651265
Request request = new Request("PUT", "/" + index);
12661266
XContentBuilder createIndex = JsonXContent.contentBuilder().startObject();
12671267
createIndex.startObject("settings");
@@ -1285,7 +1285,7 @@ private void createIndex(String index) throws Exception {
12851285
client().performRequest(request);
12861286
}
12871287

1288-
private void updateMapping(String index, CheckedConsumer<XContentBuilder, IOException> body) throws Exception {
1288+
protected static void updateMapping(String index, CheckedConsumer<XContentBuilder, IOException> body) throws Exception {
12891289
Request request = new Request("PUT", "/" + index + "/_mapping/doc");
12901290
XContentBuilder updateMapping = JsonXContent.contentBuilder().startObject();
12911291
updateMapping.startObject("properties");
@@ -1463,7 +1463,7 @@ private Map<String,Number> createTestDataForNumericValueTypes(Supplier<Number> r
14631463
return map;
14641464
}
14651465

1466-
private void updateMappingForNumericValuesTests(String indexName) throws Exception {
1466+
private static void updateMappingForNumericValuesTests(String indexName) throws Exception {
14671467
updateMapping(indexName, builder -> {
14681468
for(String field : fieldsNames) {
14691469
builder.startObject(field).field("type", field.substring(5)).endObject();

0 commit comments

Comments
 (0)