Add displaySize to columnInfo

Original commit: elastic/x-pack-elasticsearch@ed1d265e98
This commit is contained in:
Costin Leau 2017-10-12 00:03:41 +03:00 committed by GitHub
parent c9a41e111e
commit 2026198dd4
11 changed files with 50 additions and 33 deletions

View File

@ -35,8 +35,8 @@ public class SqlActionIT extends AbstractSqlIntegTestCase {
assertThat(response.columns(), hasSize(2)); assertThat(response.columns(), hasSize(2));
int dataIndex = dataBeforeCount ? 0 : 1; int dataIndex = dataBeforeCount ? 0 : 1;
int countIndex = dataBeforeCount ? 1 : 0; int countIndex = dataBeforeCount ? 1 : 0;
assertEquals(new ColumnInfo("data", "text", JDBCType.VARCHAR), response.columns().get(dataIndex)); assertEquals(new ColumnInfo("data", "text", JDBCType.VARCHAR, 0), response.columns().get(dataIndex));
assertEquals(new ColumnInfo("count", "long", JDBCType.BIGINT), response.columns().get(countIndex)); assertEquals(new ColumnInfo("count", "long", JDBCType.BIGINT, 20), response.columns().get(countIndex));
assertThat(response.rows(), hasSize(2)); assertThat(response.rows(), hasSize(2));
assertEquals("bar", response.rows().get(0).get(dataIndex)); assertEquals("bar", response.rows().get(0).get(dataIndex));

View File

@ -13,9 +13,10 @@ import java.util.Objects;
public class ColumnInfo { public class ColumnInfo {
public String catalog, schema, table, label, name; public String catalog, schema, table, label, name;
public int displaySize;
public JDBCType type; public JDBCType type;
public ColumnInfo(String name, JDBCType type, String table, String catalog, String schema, String label) { public ColumnInfo(String name, JDBCType type, String table, String catalog, String schema, String label, int displaySize) {
if (name == null) { if (name == null) {
throw new IllegalArgumentException("[name] must not be null"); throw new IllegalArgumentException("[name] must not be null");
} }
@ -40,6 +41,7 @@ public class ColumnInfo {
this.catalog = catalog; this.catalog = catalog;
this.schema = schema; this.schema = schema;
this.label = label; this.label = label;
this.displaySize = displaySize;
} }
ColumnInfo(DataInput in) throws IOException { ColumnInfo(DataInput in) throws IOException {
@ -49,6 +51,7 @@ public class ColumnInfo {
catalog = in.readUTF(); catalog = in.readUTF();
schema = in.readUTF(); schema = in.readUTF();
label = in.readUTF(); label = in.readUTF();
displaySize = in.readInt();
} }
void writeTo(DataOutput out) throws IOException { void writeTo(DataOutput out) throws IOException {
@ -58,11 +61,12 @@ public class ColumnInfo {
out.writeUTF(catalog); out.writeUTF(catalog);
out.writeUTF(schema); out.writeUTF(schema);
out.writeUTF(label); out.writeUTF(label);
out.writeInt(displaySize);
} }
public int displaySize() { public int displaySize() {
// NOCOMMIT look at this one..... // 0 - means unknown
return -1; return displaySize;
} }
@Override @Override
@ -95,11 +99,12 @@ public class ColumnInfo {
&& table.equals(other.table) && table.equals(other.table)
&& catalog.equals(other.catalog) && catalog.equals(other.catalog)
&& schema.equals(other.schema) && schema.equals(other.schema)
&& label.equals(other.label); && label.equals(other.label)
&& displaySize == other.displaySize;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(name, type, table, catalog, schema, label); return Objects.hash(name, type, table, catalog, schema, label, displaySize);
} }
} }

View File

@ -14,15 +14,15 @@ import static org.elasticsearch.xpack.sql.test.RoundTripTestUtils.assertRoundTri
public class ColumnInfoTests extends ESTestCase { public class ColumnInfoTests extends ESTestCase {
static ColumnInfo varcharInfo(String name) { static ColumnInfo varcharInfo(String name) {
return new ColumnInfo(name, JDBCType.VARCHAR, "", "", "", ""); return new ColumnInfo(name, JDBCType.VARCHAR, "", "", "", "", 0);
} }
static ColumnInfo intInfo(String name) { static ColumnInfo intInfo(String name) {
return new ColumnInfo(name, JDBCType.INTEGER, "", "", "", ""); return new ColumnInfo(name, JDBCType.INTEGER, "", "", "", "", 11);
} }
static ColumnInfo doubleInfo(String name) { static ColumnInfo doubleInfo(String name) {
return new ColumnInfo(name, JDBCType.DOUBLE, "", "", "", ""); return new ColumnInfo(name, JDBCType.DOUBLE, "", "", "", "", 25);
} }
static Object randomValueFor(ColumnInfo info) { static Object randomValueFor(ColumnInfo info) {
@ -37,7 +37,7 @@ public class ColumnInfoTests extends ESTestCase {
static ColumnInfo randomColumnInfo() { static ColumnInfo randomColumnInfo() {
return new ColumnInfo(randomAlphaOfLength(5), randomFrom(JDBCType.values()), randomAlphaOfLength(5), randomAlphaOfLength(5), return new ColumnInfo(randomAlphaOfLength(5), randomFrom(JDBCType.values()), randomAlphaOfLength(5), randomAlphaOfLength(5),
randomAlphaOfLength(5), randomAlphaOfLength(5)); randomAlphaOfLength(5), randomAlphaOfLength(5), randomInt(25));
} }
public void testRoundTrip() throws IOException { public void testRoundTrip() throws IOException {
@ -46,9 +46,9 @@ public class ColumnInfoTests extends ESTestCase {
public void testToString() { public void testToString() {
assertEquals("test.doc.a<type=[VARCHAR] catalog=[as] schema=[ads] label=[lab]>", assertEquals("test.doc.a<type=[VARCHAR] catalog=[as] schema=[ads] label=[lab]>",
new ColumnInfo("a", JDBCType.VARCHAR, "test.doc", "as", "ads", "lab").toString()); new ColumnInfo("a", JDBCType.VARCHAR, "test.doc", "as", "ads", "lab", 0).toString());
assertEquals("test.doc.a<type=[VARCHAR]>", assertEquals("test.doc.a<type=[VARCHAR]>",
new ColumnInfo("a", JDBCType.VARCHAR, "test.doc", "", "", "").toString()); new ColumnInfo("a", JDBCType.VARCHAR, "test.doc", "", "", "", 0).toString());
assertEquals("string<type=[VARCHAR]>", varcharInfo("string").toString()); assertEquals("string<type=[VARCHAR]>", varcharInfo("string").toString());
assertEquals("int<type=[INTEGER]>", intInfo("int").toString()); assertEquals("int<type=[INTEGER]>", intInfo("int").toString());
assertEquals("d<type=[DOUBLE]>", doubleInfo("d").toString()); assertEquals("d<type=[DOUBLE]>", doubleInfo("d").toString());

View File

@ -1200,7 +1200,7 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper {
} }
// it's not, use the default and move on // it's not, use the default and move on
} }
columns.add(new ColumnInfo(name, type, tableName, "INFORMATION_SCHEMA", EMPTY, EMPTY)); columns.add(new ColumnInfo(name, type, tableName, "INFORMATION_SCHEMA", EMPTY, EMPTY, 0));
} }
else { else {
throw new JdbcException("Invalid metadata schema definition"); throw new JdbcException("Invalid metadata schema definition");

View File

@ -29,7 +29,7 @@ public class WeekOfWeekYear extends DateTimeFunction {
@Override @Override
protected ChronoField chronoField() { protected ChronoField chronoField() {
return ChronoField.ALIGNED_WEEK_OF_YEAR; // NOCOMMIT is this right? return ChronoField.ALIGNED_WEEK_OF_YEAR;
} }
@Override @Override

View File

@ -56,6 +56,7 @@ import java.util.regex.Pattern;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.elasticsearch.common.Strings.hasText; import static org.elasticsearch.common.Strings.hasText;
import static org.elasticsearch.rest.RestRequest.Method.POST; import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.xpack.sql.util.StringUtils.EMPTY;
public class RestSqlJdbcAction extends AbstractSqlProtocolRestAction { public class RestSqlJdbcAction extends AbstractSqlProtocolRestAction {
private final SqlLicenseChecker sqlLicenseChecker; private final SqlLicenseChecker sqlLicenseChecker;
@ -158,7 +159,7 @@ public class RestSqlJdbcAction extends AbstractSqlProtocolRestAction {
List<ColumnInfo> columns = new ArrayList<>(response.columns().size()); List<ColumnInfo> columns = new ArrayList<>(response.columns().size());
for (SqlResponse.ColumnInfo info : response.columns()) { for (SqlResponse.ColumnInfo info : response.columns()) {
types.add(info.jdbcType()); types.add(info.jdbcType());
columns.add(new ColumnInfo(info.name(), info.jdbcType(), "", "", "", "")); columns.add(new ColumnInfo(info.name(), info.jdbcType(), EMPTY, EMPTY, EMPTY, EMPTY, info.displaySize()));
} }
return new QueryInitResponse(System.nanoTime() - start, serializeCursor(response.cursor(), types), columns, return new QueryInitResponse(System.nanoTime() - start, serializeCursor(response.cursor(), types), columns,
new SqlResponsePayload(types, response.rows())); new SqlResponsePayload(types, response.rows()));
@ -174,7 +175,7 @@ public class RestSqlJdbcAction extends AbstractSqlProtocolRestAction {
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("error reading the cursor"); throw new IllegalArgumentException("error reading the cursor");
} }
SqlRequest sqlRequest = new SqlRequest("", SqlRequest.DEFAULT_TIME_ZONE, 0, cursor); SqlRequest sqlRequest = new SqlRequest(EMPTY, SqlRequest.DEFAULT_TIME_ZONE, 0, cursor);
long start = System.nanoTime(); long start = System.nanoTime();
return channel -> client.execute(SqlAction.INSTANCE, sqlRequest, toActionListener(request, channel, response -> { return channel -> client.execute(SqlAction.INSTANCE, sqlRequest, toActionListener(request, channel, response -> {
return new QueryPageResponse(System.nanoTime() - start, serializeCursor(response.cursor(), types), return new QueryPageResponse(System.nanoTime() - start, serializeCursor(response.cursor(), types),

View File

@ -170,17 +170,20 @@ public class SqlResponse extends ActionResponse implements ToXContentObject {
private final String name; private final String name;
private final String esType; private final String esType;
private final JDBCType jdbcType; private final JDBCType jdbcType;
private final int displaySize;
public ColumnInfo(String name, String esType, JDBCType jdbcType) { public ColumnInfo(String name, String esType, JDBCType jdbcType, int displaySize) {
this.name = name; this.name = name;
this.esType = esType; this.esType = esType;
this.jdbcType = jdbcType; this.jdbcType = jdbcType;
this.displaySize = displaySize;
} }
ColumnInfo(StreamInput in) throws IOException { ColumnInfo(StreamInput in) throws IOException {
name = in.readString(); name = in.readString();
esType = in.readString(); esType = in.readString();
jdbcType = JDBCType.valueOf(in.readVInt()); jdbcType = JDBCType.valueOf(in.readVInt());
displaySize = in.readVInt();
} }
@Override @Override
@ -188,6 +191,7 @@ public class SqlResponse extends ActionResponse implements ToXContentObject {
out.writeString(name); out.writeString(name);
out.writeString(esType); out.writeString(esType);
out.writeVInt(jdbcType.getVendorTypeNumber()); out.writeVInt(jdbcType.getVendorTypeNumber());
out.writeVInt(displaySize);
} }
@Override @Override
@ -220,6 +224,13 @@ public class SqlResponse extends ActionResponse implements ToXContentObject {
return jdbcType; return jdbcType;
} }
/**
* Used by JDBC
*/
public int displaySize() {
return displaySize;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || obj.getClass() != getClass()) { if (obj == null || obj.getClass() != getClass()) {
@ -228,12 +239,13 @@ public class SqlResponse extends ActionResponse implements ToXContentObject {
ColumnInfo other = (ColumnInfo) obj; ColumnInfo other = (ColumnInfo) obj;
return name.equals(other.name) return name.equals(other.name)
&& esType.equals(other.esType) && esType.equals(other.esType)
&& jdbcType.equals(other.jdbcType); && jdbcType.equals(other.jdbcType)
&& displaySize == other.displaySize;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(name, esType, jdbcType); return Objects.hash(name, esType, jdbcType, displaySize);
} }
@Override @Override

View File

@ -69,7 +69,7 @@ public class TransportSqlAction extends HandledTransportAction<SqlRequest, SqlRe
if (includeColumnMetadata) { if (includeColumnMetadata) {
columns = new ArrayList<>(cursor.schema().types().size()); columns = new ArrayList<>(cursor.schema().types().size());
for (Schema.Entry entry : cursor.schema()) { for (Schema.Entry entry : cursor.schema()) {
columns.add(new ColumnInfo(entry.name(), entry.type().esName(), entry.type().sqlType())); columns.add(new ColumnInfo(entry.name(), entry.type().esName(), entry.type().sqlType(), entry.type().displaySize()));
} }
columns = unmodifiableList(columns); columns = unmodifiableList(columns);
} }

View File

@ -69,10 +69,10 @@ abstract class JdbcUtils {
case FLOAT: case FLOAT:
case DOUBLE: return 25; case DOUBLE: return 25;
case VARCHAR: case VARCHAR:
case VARBINARY: return -1; case VARBINARY: return 0;
case TIMESTAMP: return 20; case TIMESTAMP: return 20;
default: default:
return -1; return 0;
} }
} }

View File

@ -18,11 +18,11 @@ import static org.hamcrest.Matchers.arrayWithSize;
public class CliFormatterTests extends ESTestCase { public class CliFormatterTests extends ESTestCase {
private final SqlResponse firstResponse = new SqlResponse(Cursor.EMPTY, 10, 5, private final SqlResponse firstResponse = new SqlResponse(Cursor.EMPTY, 10, 5,
Arrays.asList( Arrays.asList(
new ColumnInfo("foo", "string", JDBCType.VARCHAR), new ColumnInfo("foo", "string", JDBCType.VARCHAR, 0),
new ColumnInfo("bar", "long", JDBCType.BIGINT), new ColumnInfo("bar", "long", JDBCType.BIGINT, 15),
new ColumnInfo("15charwidename!", "double", JDBCType.DOUBLE), new ColumnInfo("15charwidename!", "double", JDBCType.DOUBLE, 25),
new ColumnInfo("superduperwidename!!!", "double", JDBCType.DOUBLE), new ColumnInfo("superduperwidename!!!", "double", JDBCType.DOUBLE, 25),
new ColumnInfo("baz", "keyword", JDBCType.VARCHAR)), new ColumnInfo("baz", "keyword", JDBCType.VARCHAR, 0)),
Arrays.asList( Arrays.asList(
Arrays.asList("15charwidedata!", 1, 6.888, 12, "rabbit"), Arrays.asList("15charwidedata!", 1, 6.888, 12, "rabbit"),
Arrays.asList("dog", 1.7976931348623157E308, 123124.888, 9912, "goat"))); Arrays.asList("dog", 1.7976931348623157E308, 123124.888, 9912, "goat")));

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.xpack.sql.execution.search.ScrollCursorTests; import org.elasticsearch.xpack.sql.execution.search.ScrollCursorTests;
import org.elasticsearch.xpack.sql.plugin.SqlPlugin; import org.elasticsearch.xpack.sql.plugin.SqlPlugin;
import org.elasticsearch.xpack.sql.plugin.sql.action.SqlResponse;
import org.elasticsearch.xpack.sql.plugin.sql.action.SqlResponse.ColumnInfo; import org.elasticsearch.xpack.sql.plugin.sql.action.SqlResponse.ColumnInfo;
import org.elasticsearch.xpack.sql.session.Cursor; import org.elasticsearch.xpack.sql.session.Cursor;
@ -36,7 +35,7 @@ public class SqlResponseTests extends AbstractStreamableTestCase<SqlResponse> {
if (randomBoolean()) { if (randomBoolean()) {
columns = new ArrayList<>(columnCount); columns = new ArrayList<>(columnCount);
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
columns.add(new ColumnInfo(randomAlphaOfLength(10), randomAlphaOfLength(10), randomFrom(JDBCType.values()))); columns.add(new ColumnInfo(randomAlphaOfLength(10), randomAlphaOfLength(10), randomFrom(JDBCType.values()), randomInt(25)));
} }
} }