From 846c6a510dcc423fde14de9f6e9e38a46a72c1d4 Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Thu, 10 Aug 2023 23:34:35 -0700 Subject: [PATCH 1/3] Add extra fields to JdbcFieldInfo --- .../arrow/adapter/jdbc/JdbcFieldInfo.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java index 3237c9bf97b..0a1b408ec06 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java @@ -17,6 +17,7 @@ package org.apache.arrow.adapter.jdbc; +import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; @@ -40,6 +41,8 @@ public class JdbcFieldInfo { private final int nullability; private final int precision; private final int scale; + private final String typeName; + private final int displaySize; /** * Builds a JdbcFieldInfo using only the {@link java.sql.Types} type. Do not use this constructor @@ -53,12 +56,13 @@ public JdbcFieldInfo(int jdbcType) { Preconditions.checkArgument( (jdbcType != Types.DECIMAL && jdbcType != Types.NUMERIC), "DECIMAL and NUMERIC types require a precision and scale; please use another constructor."); - this.column = 0; this.jdbcType = jdbcType; this.nullability = ResultSetMetaData.columnNullableUnknown; this.precision = 0; this.scale = 0; + this.typeName = ""; + this.displaySize = 0; } /** @@ -75,6 +79,8 @@ public JdbcFieldInfo(int jdbcType, int precision, int scale) { this.nullability = ResultSetMetaData.columnNullableUnknown; this.precision = precision; this.scale = scale; + this.typeName = ""; + this.displaySize = 0; } /** @@ -92,6 +98,8 @@ public JdbcFieldInfo(int jdbcType, int nullability, int precision, int scale) { this.nullability = nullability; this.precision = precision; this.scale = scale; + this.typeName = ""; + this.displaySize = 0; } /** @@ -115,14 +123,24 @@ public JdbcFieldInfo(ResultSetMetaData rsmd, int column) throws SQLException { this.nullability = rsmd.isNullable(column); this.precision = rsmd.getPrecision(column); this.scale = rsmd.getScale(column); + this.typeName = rsmd.getColumnTypeName(column); + this.displaySize = rsmd.getColumnDisplaySize(column); + } + + public JdbcFieldInfo(ResultSet rs) throws SQLException { + this.column = rs.getInt("ORDINAL_POSITION"); + this.jdbcType = rs.getInt("DATA_TYPE"); + this.nullability = rs.getInt("NULLABLE"); + this.precision = rs.getInt("COLUMN_SIZE"); + this.scale = rs.getInt("DECIMAL_DIGITS"); + this.typeName = rs.getString("TYPE_NAME"); + this.displaySize = rs.getInt("CHAR_OCTET_LENGTH"); } /** * The {@link java.sql.Types} type. */ - public int getJdbcType() { - return jdbcType; - } + public int getJdbcType() { return jdbcType; } /** * The nullability. @@ -151,4 +169,14 @@ public int getScale() { public int getColumn() { return column; } + + /** + * The type name as reported by the database. + */ + public String getTypeName() { return typeName; } + + /** + * The max number of characters for the column. + */ + public int getDisplaySize() { return displaySize; } } From 72d4f10f279bfd72e8d343a83d8e81b9b8bf605b Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Fri, 11 Aug 2023 00:05:34 -0700 Subject: [PATCH 2/3] . --- .../arrow/adapter/jdbc/JdbcFieldInfo.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java index 0a1b408ec06..97ca8f27ceb 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfo.java @@ -127,6 +127,13 @@ public JdbcFieldInfo(ResultSetMetaData rsmd, int column) throws SQLException { this.displaySize = rsmd.getColumnDisplaySize(column); } + /** + * Builds a JdbcFieldInfo from the corresponding row from a {@link java.sql.DatabaseMetaData#getColumns} + * ResulSet. + * + * @param rs The {@link java.sql.ResultSet} to get the field information from. + * @throws SQLException If the column information cannot be retrieved. + */ public JdbcFieldInfo(ResultSet rs) throws SQLException { this.column = rs.getInt("ORDINAL_POSITION"); this.jdbcType = rs.getInt("DATA_TYPE"); @@ -140,7 +147,9 @@ public JdbcFieldInfo(ResultSet rs) throws SQLException { /** * The {@link java.sql.Types} type. */ - public int getJdbcType() { return jdbcType; } + public int getJdbcType() { + return jdbcType; + } /** * The nullability. @@ -173,10 +182,14 @@ public int getColumn() { /** * The type name as reported by the database. */ - public String getTypeName() { return typeName; } + public String getTypeName() { + return typeName; + } /** * The max number of characters for the column. */ - public int getDisplaySize() { return displaySize; } + public int getDisplaySize() { + return displaySize; + } } From 4b73932c0d872d76d135c429f53634e07867beb8 Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Fri, 11 Aug 2023 00:32:02 -0700 Subject: [PATCH 3/3] Fix tests --- .../arrow/adapter/jdbc/ResultSetUtility.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtility.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtility.java index 339e120beea..c712741b51f 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtility.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtility.java @@ -308,6 +308,16 @@ public int isNullable(int column) throws SQLException { return columns.get(column - 1).isNullable(); } + @Override + public int getColumnDisplaySize(int column) throws SQLException { + return columns.get(column - 1).getDisplaySize(); + } + + @Override + public String getColumnTypeName(int column) throws SQLException { + return columns.get(column - 1).getTypeName(); + } + public static MockResultSetMetaData fromRows(ArrayList rows) throws SQLException { // Note: This attempts to dynamically construct ResultSetMetaData from the first row in a given result set. // If there are now rows, or the result set contains no columns, this cannot be dynamically generated and @@ -334,6 +344,8 @@ public static class MockColumnMetaData { private int scale; private int nullable; private String label; + private String typeName; + private int displaySize; private MockColumnMetaData() {} @@ -362,6 +374,14 @@ private int isNullable() { return nullable; } + private String getTypeName() { + return typeName; + } + + private int getDisplaySize() { + return displaySize; + } + public static MockColumnMetaData fromDataElement(MockDataElement element, int i) throws SQLException { return MockColumnMetaData.builder() .index(i) @@ -369,6 +389,8 @@ public static MockColumnMetaData fromDataElement(MockDataElement element, int i) .precision(element.getPrecision()) .scale(element.getScale()) .nullable(element.isNullable()) + .setTypeName("TYPE") + .setDisplaySize(420) .label("col_" + i) .build(); } @@ -410,6 +432,16 @@ public Builder nullable(int nullable) { return this; } + public Builder setTypeName(String typeName) { + this.columnMetaData.typeName = typeName; + return this; + } + + public Builder setDisplaySize(int displaySize) { + this.columnMetaData.displaySize = displaySize; + return this; + } + public MockColumnMetaData build() { return this.columnMetaData; }