[
https://issues.apache.org/jira/browse/DRILL-4730?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15866111#comment-15866111
]
ASF GitHub Bot commented on DRILL-4730:
---------------------------------------
Github user sudheeshkatkam commented on a diff in the pull request:
https://github.com/apache/drill/pull/613#discussion_r100428330
--- Diff:
exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillMetaImpl.java ---
@@ -78,19 +111,237 @@ private MetaResultSet s(String s) {
}
}
+ /** Information about type mapping. */
+ private static class TypeInfo {
+ private static Map<Class<?>, TypeInfo> MAPPING =
ImmutableMap.<Class<?>, TypeInfo> builder()
+ .put(boolean.class, of(Types.BOOLEAN, "BOOLEAN"))
+ .put(Boolean.class, of(Types.BOOLEAN, "BOOLEAN"))
+ .put(Byte.TYPE, of(Types.TINYINT, "TINYINT"))
+ .put(Byte.class, of(Types.TINYINT, "TINYINT"))
+ .put(Short.TYPE, of(Types.SMALLINT, "SMALLINT"))
+ .put(Short.class, of(Types.SMALLINT, "SMALLINT"))
+ .put(Integer.TYPE, of(Types.INTEGER, "INTEGER"))
+ .put(Integer.class, of(Types.INTEGER, "INTEGER"))
+ .put(Long.TYPE, of(Types.BIGINT, "BIGINT"))
+ .put(Long.class, of(Types.BIGINT, "BIGINT"))
+ .put(Float.TYPE, of(Types.FLOAT, "FLOAT"))
+ .put(Float.class, of(Types.FLOAT, "FLOAT"))
+ .put(Double.TYPE, of(Types.DOUBLE, "DOUBLE"))
+ .put(Double.class, of(Types.DOUBLE, "DOUBLE"))
+ .put(String.class, of(Types.VARCHAR, "CHARACTER VARYING"))
+ .put(java.sql.Date.class, of(Types.DATE, "DATE"))
+ .put(Time.class, of(Types.TIME, "TIME"))
+ .put(Timestamp.class, of(Types.TIMESTAMP, "TIMESTAMP"))
+ .build();
+
+ private final int sqlType;
+ private final String sqlTypeName;
+
+ public TypeInfo(int sqlType, String sqlTypeName) {
+ this.sqlType = sqlType;
+ this.sqlTypeName = sqlTypeName;
+ }
+ private static TypeInfo of(int sqlType, String sqlTypeName) {
+ return new TypeInfo(sqlType, sqlTypeName);
+ }
- @Override
- protected <E> MetaResultSet createEmptyResultSet(Class<E> clazz) {
- return s(
- "SELECT '' AS `Interim zero-row result set` " // dummy row type
- + "FROM INFORMATION_SCHEMA.CATALOGS " // any table
- + "LIMIT 0" // zero rows
- );
+ public static TypeInfo get(Class<?> clazz) {
+ return MAPPING.get(clazz);
+ }
}
- @Override
- public MetaResultSet getTables(String catalog, final Pat schemaPattern,
final Pat tableNamePattern,
+ /** Metadata describing a column.
+ * Copied from Avatica with several fixes
+ * */
+ public static class MetaColumn implements Named {
+ public final String tableCat;
+ public final String tableSchem;
+ public final String tableName;
+ public final String columnName;
+ public final int dataType;
+ public final String typeName;
+ public final Integer columnSize;
+ public final Integer bufferLength = null;
+ public final Integer decimalDigits;
+ public final Integer numPrecRadix;
+ public final int nullable;
+ public final String remarks = null;
+ public final String columnDef = null;
+ public final Integer sqlDataType = null;
+ public final Integer sqlDatetimeSub = null;
+ public final Integer charOctetLength;
+ public final int ordinalPosition;
+ @NotNull
+ public final String isNullable;
+ public final String scopeCatalog = null;
+ public final String scopeSchema = null;
+ public final String scopeTable = null;
+ public final Short sourceDataType = null;
+ @NotNull
+ public final String isAutoincrement = "";
+ @NotNull
+ public final String isGeneratedcolumn = "";
+
+ public MetaColumn(
+ String tableCat,
+ String tableSchem,
+ String tableName,
+ String columnName,
+ int dataType,
+ String typeName,
+ Integer columnSize,
+ Integer decimalDigits,
+ Integer numPrecRadix,
+ int nullable,
+ Integer charOctetLength,
+ int ordinalPosition,
+ String isNullable) {
+ this.tableCat = tableCat;
+ this.tableSchem = tableSchem;
+ this.tableName = tableName;
+ this.columnName = columnName;
+ this.dataType = dataType;
+ this.typeName = typeName;
+ this.columnSize = columnSize;
+ this.decimalDigits = decimalDigits;
+ this.numPrecRadix = numPrecRadix;
+ this.nullable = nullable;
+ this.charOctetLength = charOctetLength;
+ this.ordinalPosition = ordinalPosition;
+ this.isNullable = isNullable;
+ }
+
+ @Override
+ public String getName() {
+ return columnName;
+ }
+ }
+
+ private static LikeFilter newLikeFilter(final Pat pattern) {
+ if (pattern == null || pattern.s == null) {
+ return null;
+ }
+
+ return LikeFilter.newBuilder().setPattern(pattern.s).build();
+ }
+
+ /**
+ * Quote the provided string as a LIKE pattern
+ *
+ * @param v the value to quote
+ * @return a LIKE pattern matching exactly v, or {@code null} if v is
{@code null}
+ */
+ private static Pat quote(String v) {
+ if (v == null) {
+ return null;
+ }
+
+ StringBuilder sb = new StringBuilder(v.length());
+ for(int index = 0; index<v.length(); index++) {
+ char c = v.charAt(index);
+ switch(c) {
+ case '%':
+ case '_':
+ case '\\':
+ sb.append('\\').append(c);
+ break;
+
+ default:
+ sb.append(c);
+ }
+ }
+
+ return Pat.of(sb.toString());
+ }
+
+ // Overriding fieldMetaData as Calcite version create ColumnMetaData
with invalid offset
+ protected static ColumnMetaData.StructType drillFieldMetaData(Class<?>
clazz) {
+ final List<ColumnMetaData> list = new ArrayList<>();
+ for (Field field : clazz.getFields()) {
+ if (Modifier.isPublic(field.getModifiers())
+ && !Modifier.isStatic(field.getModifiers())) {
+ NotNull notNull = field.getAnnotation(NotNull.class);
+ boolean notNullable = (notNull != null ||
field.getType().isPrimitive());
+ list.add(
+ drillColumnMetaData(
+ AvaticaUtils.camelToUpper(field.getName()),
+ list.size(), field.getType(), notNullable));
+ }
+ }
+ return ColumnMetaData.struct(list);
+ }
+
+
+ protected static ColumnMetaData drillColumnMetaData(String name, int
index,
+ Class<?> type, boolean notNullable) {
+ TypeInfo pair = TypeInfo.get(type);
+ ColumnMetaData.Rep rep =
+ ColumnMetaData.Rep.VALUE_MAP.get(type);
+ ColumnMetaData.AvaticaType scalarType =
+ ColumnMetaData.scalar(pair.sqlType, pair.sqlTypeName, rep);
+ return new ColumnMetaData(
+ index, false, true, false, false,
+ notNullable
+ ? DatabaseMetaData.columnNoNulls
+ : DatabaseMetaData.columnNullable,
+ true, -1, name, name, null,
+ 0, 0, null, null, scalarType, true, false, false,
+ scalarType.columnClassName());
+ }
+
+ abstract private class MetadataAdapter<CalciteMetaType, Response,
ResponseValue> {
+ private final Class<? extends CalciteMetaType> clazz;
+
+ public MetadataAdapter(Class<? extends CalciteMetaType> clazz) {
+ this.clazz = clazz;
+ }
+
+ MetaResultSet getMeta(DrillRpcFuture<Response> future) {
+ final Response response;
+ try {
+ response = future.get();
+ } catch (InterruptedException e) {
+ throw Throwables.propagate(e);
+ } catch (ExecutionException e) {
+ Throwable cause = e.getCause();
+ if (cause != null) {
+ throw Throwables.propagate(cause);
--- End diff --
This [may not be
right](https://github.com/google/guava/wiki/Why-we-deprecated-Throwables.propagate#encourages-unwrapping-exceptions-from-other-threads)..
> Update JDBC DatabaseMetaData implementation to use new Metadata APIs
> --------------------------------------------------------------------
>
> Key: DRILL-4730
> URL: https://issues.apache.org/jira/browse/DRILL-4730
> Project: Apache Drill
> Issue Type: Sub-task
> Components: Client - JDBC
> Reporter: Venki Korukanti
> Assignee: Laurent Goujon
>
> DRILL-4728 is going to add support for new metadata APIs. Replace the
> INFORMATION_SCHEMA queries used to get the metadata with the new APIs
> provided in Java client.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)