forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: fall back to using the field name for column label (elastic#38842)
- Loading branch information
Showing
6 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...n/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ode/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcResultSetMetaDataIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...n/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetMetaDataTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters