Skip to content

Commit

Permalink
Flight SQL Ratification Based On Community Feedback #7 (apache#98)
Browse files Browse the repository at this point in the history
* Remove scope from 'hamcrest' dependency on java/pom.xml

* Use flight top-level module on parent pom.xml instead of declaring each one

* Avoid using getStatement inside StatementContext methods

* Make StatementContext.getQuery() return String

* Minor fixes on pom.xml

* Move 'os-maven-plugin' to parent pom.xml

* Update protobuf generation on pom.xml files

* Use ClassLoader#getResource to get network.properties on TestFlightSql

* Bind to any ephemeral port on TestFlightSql

* Move JDBC-Arrow type default conversion from JdbcToArrowConfig to JdbcToArrowUtils

* Micro-optimization: initialize ArrayList with the right size

* Fix null-check on PreparedStatement#setParameters

* Avoid wrapping vector into a ImmutableList and then into an ArrayList on FlightSqlExample#getTablesRoot

* Remove null-check on VectorSchemaRoot on FlightSqlClient#setParameters()

* Remove the need of separate cache for ResultSets

* Add missing 'final' modifiers
  • Loading branch information
rafael-telles committed Oct 12, 2021
1 parent c6e4178 commit 2987e2a
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@

package org.apache.arrow.adapter.jdbc;

import static org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE;
import static org.apache.arrow.vector.types.FloatingPointPrecision.SINGLE;

import java.sql.Types;
import java.util.Calendar;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.util.Preconditions;
import org.apache.arrow.vector.types.DateUnit;
import org.apache.arrow.vector.types.TimeUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;

/**
Expand All @@ -56,66 +49,6 @@
*/
public final class JdbcToArrowConfig {

private static final BiFunction<JdbcFieldInfo, Calendar, ArrowType> DEFAULT_JDBC_TO_ARROW_TYPE_CONVERTER =
(fieldInfo, calendar) -> {
switch (fieldInfo.getJdbcType()) {
case Types.BOOLEAN:
case Types.BIT:
return new ArrowType.Bool();
case Types.TINYINT:
return new ArrowType.Int(8, true);
case Types.SMALLINT:
return new ArrowType.Int(16, true);
case Types.INTEGER:
return new ArrowType.Int(32, true);
case Types.BIGINT:
return new ArrowType.Int(64, true);
case Types.NUMERIC:
case Types.DECIMAL:
int precision = fieldInfo.getPrecision();
int scale = fieldInfo.getScale();
return new ArrowType.Decimal(precision, scale, 128);
case Types.REAL:
case Types.FLOAT:
return new ArrowType.FloatingPoint(SINGLE);
case Types.DOUBLE:
return new ArrowType.FloatingPoint(DOUBLE);
case Types.CHAR:
case Types.NCHAR:
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
case Types.CLOB:
return new ArrowType.Utf8();
case Types.DATE:
return new ArrowType.Date(DateUnit.DAY);
case Types.TIME:
return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
case Types.TIMESTAMP:
final String timezone;
if (calendar != null) {
timezone = calendar.getTimeZone().getID();
} else {
timezone = null;
}
return new ArrowType.Timestamp(TimeUnit.MILLISECOND, timezone);
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BLOB:
return new ArrowType.Binary();
case Types.ARRAY:
return new ArrowType.List();
case Types.NULL:
return new ArrowType.Null();
case Types.STRUCT:
return new ArrowType.Struct();
default:
// no-op, shouldn't get here
return null;
}
};
public static final int DEFAULT_TARGET_BATCH_SIZE = 1024;
public static final int NO_LIMIT_BATCH_SIZE = -1;
private final Calendar calendar;
Expand Down Expand Up @@ -218,16 +151,7 @@ public final class JdbcToArrowConfig {

// set up type converter
this.jdbcToArrowTypeConverter = jdbcToArrowTypeConverter != null ? jdbcToArrowTypeConverter :
jdbcFieldInfo -> getDefaultJdbcToArrowTypeConverter().apply(jdbcFieldInfo, calendar);
}

/**
* Gets the default JDBC-type-to-Arrow-type converter.
*
* @return the default converter.
*/
public static BiFunction<JdbcFieldInfo, Calendar, ArrowType> getDefaultJdbcToArrowTypeConverter() {
return DEFAULT_JDBC_TO_ARROW_TYPE_CONVERTER;
jdbcFieldInfo -> JdbcToArrowUtils.getArrowTypeFromJdbcType(jdbcFieldInfo, calendar);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.arrow.adapter.jdbc;

import static org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE;
import static org.apache.arrow.vector.types.FloatingPointPrecision.SINGLE;

import java.io.IOException;
import java.sql.Date;
import java.sql.ParameterMetaData;
Expand All @@ -25,6 +28,7 @@
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
Expand Down Expand Up @@ -71,6 +75,8 @@
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.types.DateUnit;
import org.apache.arrow.vector.types.TimeUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
Expand Down Expand Up @@ -119,7 +125,7 @@ public static Schema jdbcToArrowSchema(final ParameterMetaData parameterMetaData
throws SQLException {
Preconditions.checkNotNull(calendar, "Calendar object can't be null");
Preconditions.checkNotNull(parameterMetaData);
final List<Field> parameterFields = new ArrayList<>();
final List<Field> parameterFields = new ArrayList<>(parameterMetaData.getParameterCount());
for (int parameterCounter = 1; parameterCounter <= parameterMetaData.getParameterCount();
parameterCounter++) {
final int jdbcDataType = parameterMetaData.getParameterType(parameterCounter);
Expand All @@ -143,10 +149,65 @@ public static Schema jdbcToArrowSchema(final ParameterMetaData parameterMetaData
* @return a new {@link ArrowType}.
*/
public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo, final Calendar calendar) {
return JdbcToArrowConfig.getDefaultJdbcToArrowTypeConverter().apply(fieldInfo, calendar);
switch (fieldInfo.getJdbcType()) {
case Types.BOOLEAN:
case Types.BIT:
return new ArrowType.Bool();
case Types.TINYINT:
return new ArrowType.Int(8, true);
case Types.SMALLINT:
return new ArrowType.Int(16, true);
case Types.INTEGER:
return new ArrowType.Int(32, true);
case Types.BIGINT:
return new ArrowType.Int(64, true);
case Types.NUMERIC:
case Types.DECIMAL:
int precision = fieldInfo.getPrecision();
int scale = fieldInfo.getScale();
return new ArrowType.Decimal(precision, scale, 128);
case Types.REAL:
case Types.FLOAT:
return new ArrowType.FloatingPoint(SINGLE);
case Types.DOUBLE:
return new ArrowType.FloatingPoint(DOUBLE);
case Types.CHAR:
case Types.NCHAR:
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
case Types.CLOB:
return new ArrowType.Utf8();
case Types.DATE:
return new ArrowType.Date(DateUnit.DAY);
case Types.TIME:
return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
case Types.TIMESTAMP:
final String timezone;
if (calendar != null) {
timezone = calendar.getTimeZone().getID();
} else {
timezone = null;
}
return new ArrowType.Timestamp(TimeUnit.MILLISECOND, timezone);
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BLOB:
return new ArrowType.Binary();
case Types.ARRAY:
return new ArrowType.List();
case Types.NULL:
return new ArrowType.Null();
case Types.STRUCT:
return new ArrowType.Struct();
default:
// no-op, shouldn't get here
return null;
}
}


/**
* Create Arrow {@link Schema} object for the given JDBC {@link java.sql.ResultSetMetaData}.
*
Expand Down
5 changes: 2 additions & 3 deletions java/flight/flight-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>arrow-flight</artifactId>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-java-root</artifactId>
<version>6.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>flight-core</artifactId>
Expand Down Expand Up @@ -237,7 +237,6 @@
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${dep.protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<clearOutputDirectory>false</clearOutputDirectory>
Expand Down
1 change: 0 additions & 1 deletion java/flight/flight-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${dep.protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<clearOutputDirectory>false</clearOutputDirectory>
Expand Down
Loading

0 comments on commit 2987e2a

Please sign in to comment.